Search code examples
mysqlvb.netparametersprepared-statement

Add Parameter to Prepared Statement For ODBC in VB.net


I know someone have asked question about using prepared statement for ODBC at VB.NET at here (Prepared Statements For ODBC in VB.net). But it doesn't describe clearly for me how to add the parameter to that prepared statement.

Any help?

Tq


Solution

  • You can try to add command parameter the way shown in the post you referred :

    Dim cmd As String = "insert into sites(field1, field2) values(?,?)"
    Dim odcmd As New OdbcCommand
    
    odcmd.CommandText = cmd
    
    odcmd.Parameters.Add("@field1", OdbcType.Int)
    odcmd.Parameters("@field1").Value = 5
    odcmd.Parameters.Add("@field2", OdbcType.Int)
    odcmd.Parameters("@field2").Value = 8
    

    But there is an important point to note, that isn't explained there :

    The order in which OdbcParameter objects are added to the OdbcParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.

    Related question : Can ODBC parameter place holders be named?