I want to pass an array of strings as a SQL parameter, but when I execute the command, it throws an exception that says “Object must implement IConvertible”. Here is the code (all three columns are nvarchar):
Public Sub sendsms(messageBodies As String(), ByVal recipientNumbers As String(), Optional ByVal chkMsgIds As string() = Nothing)
Dim con As New SqlConnection
con.ConnectionString = connection.Sms
Dim cmd As New SqlCommand("insert into sentmessage (text,id,number) values (@text,@id,@number)", con)
cmd.Parameters.Add("@text", Data.SqlDbType.NVarChar, n).Value = messageBodies
cmd.Parameters.Add("@id", Data.SqlDbType.NVarChar, n).Value = chkMsgIds
cmd.Parameters.Add("@number", Data.SqlDbType.NVarChar, n).Value = recipientNumbers
con.Open()
cmd.ExecuteNonQuery()
con.Close()
cmd.Dispose()
End Sub
I created this routine to insert huge amounts of records into the database, what is correct approach?
Typically, you would just execute multiple insert commands, one per row, such as:
However, in SQL Server 2008, they added support for a new multi-row insert syntax which is explained here:
http://blog.sqlauthority.com/2008/07/02/sql-server-2008-insert-multiple-records-using-one-insert-statement-use-of-row-constructor/
For instance, you could execute a command such as: