-1

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 cleary for me how to add the parameter to that prepared statement.

Any help?

Tq

Community
  • 1
  • 1
silverknightone
  • 436
  • 1
  • 3
  • 12
  • Um, yes it does. It calls `Parameters.Add` on the command and then it sets the `Value`. You can call `AddWithValue` to do both at once if you like. This is no different to any ADO.NET. – jmcilhinney Apr 17 '14 at 05:34
  • If you're talking about place-holder vs the parameter name then it's you who hasn't described the situation clearly. As the comment on the answer says, the place-holder format depends on the data source. What is your data source? If you don't use named parameters in the SQL code then the actual name you use when you add the parameter is irrelevant to the system, but should still be descriptive to the developer. – jmcilhinney Apr 17 '14 at 05:37

1 Answers1

2

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?

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137