ManicCW
Well-known member
Hi I have writen code for inserting, updating and deleting records from access database (can be used in sql with little change). I've put this code in module and I call subs as I need them. Here is the code:
This is for inserting (code in module)
This is where I call it in app:
This works great. BUT, I allways want to do it very simple with less code. Can I use here multidimensional arrays. Suggest me improvements. TNX
This is for inserting (code in module)
VB.NET:
Public Sub Inserting(ByVal InsertTable As String, ByVal InsertCols As Array, ByVal InsertParams As Array)
Dim cmd As New OleDbCommand
Dim i As Integer
With cmd
'insert command text
.CommandText = "INSERT INTO " & InsertTable & " ("
For i = 0 To InsertCols.Length - 1 Step 1
.CommandText &= InsertCols(i)
If i < InsertCols.Length - 1 Then
.CommandText &= ", "
End If
Next
.CommandText &= ") VALUES ("
For i = 0 To InsertParams.Length - 1 Step 1
.CommandText &= "?"
If i < InsertParams.Length - 1 Then
.CommandText &= ", "
End If
Next
.CommandText &= ")"
'parameters
For i = 0 To InsertParams.Length - 1 Step 1
.Parameters.Add(InsertCols(i), InsertParams(i))
Next
.Connection = cn
End With
cmd.ExecuteNonQuery()
End Sub
This is where I call it in app:
VB.NET:
Dim InsertTable As String = "TimeZones"
Dim InsertCols(2) As String
InsertCols(0) = "ZoneName"
InsertCols(1) = "HourCorrection"
InsertCols(2) = "MinuteCorrection"
Dim InsertParams(2) As String
InsertParams(0) = Me.txtZoneName.Text.Trim
InsertParams(1) = Me.txtHourCorrection.Text.Trim
InsertParams(2) = Me.txtMinuteCorrection.Text.Trim
cn.Open()
Inserting(InsertTable, InsertCols, InsertParams)
BindGrid()
cn.Close()
This works great. BUT, I allways want to do it very simple with less code. Can I use here multidimensional arrays. Suggest me improvements. TNX