Anti-Rich
Well-known member
hi all,
i have the following sub that executes an update/delete/insert statement on my sqlserver, i was just wondering if this is a "good programmers" (ie. most efficient) way of doing it.
if your wondering the reason i used an array for the fields and values its because i wanted a generic sub that can do it with the least fuss, but at the same time handling a dynamic amount of fields/values. im hoping this was a good way to do it.
any input/suggestions/improvements would be much appreciated
regards
adam
i have the following sub that executes an update/delete/insert statement on my sqlserver, i was just wondering if this is a "good programmers" (ie. most efficient) way of doing it.
VB.NET:
PublicSub NonQuery(ByVal sql AsString, ByRef cnn As SqlConnection, ByVal fields() AsString, ByVal values() AsString)
cmd = New SqlCommand
With cmd
.CommandType = CommandType.Text
.CommandText = sql
.Connection = cnn
For i AsInteger = 0 To fields.Length - 1
.Parameters.AddWithValue("@" & fields(i), values(i))
Next
Try
.Connection.Open()
.ExecuteNonQuery()
.Connection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
Finally
If .Connection.State = ConnectionState.Open Then
.Connection.Close()
EndIf
EndTry
EndWith
End Sub
any input/suggestions/improvements would be much appreciated
regards
adam
Last edited by a moderator: