I am new to vb.net and one of the functions I wrote is below. The idea behind it is to just send a dictionary and table name to the function and get the result i din return.
Is this good practice or just silly?
Public Function dbInsert(ByRef EntryDetails As Dictionary(Of String, String),ByRef table as String) As Integer
Dim command As new SqlCeCommand
' Loop over entries.
Dim fieldNames() As String = {}
Dim fieldsValues() As String = {}
Dim pair As KeyValuePair(Of String, String)
For Each pair In EntryDetails
ReDim Preserve fieldNames(0 To UBound(fieldNames) + 1)
fieldNames(UBound(fieldNames)) = pair.Key
command.Parameters.AddWithValue("@" + pair.Key, pair.Value)
Next
command = New SqlCeCommand("INSERT INTO "+table+" (" + String.Join(",", fieldNames) + ")", connection.DbPublic)
Dim newProdID As Int32 = 0
Try
command.ExecuteNonQuery()
Dim cmd As New SqlCeCommand("SELECT @@IDENTITY", conn)
newProdID = Convert.ToInt32(cmd.ExecuteScalar())
'Console.WriteLine("added: {0}", newProdID)
Return newProdID
Catch e As Exception
Console.WriteLine("Unable to insert: {0}", e.Message)
Return 0
End Try
End Function
byref
parameters when not needed is bad practice. – Guffa Mar 6 '12 at 17:09