Wajih-ur-Rehman said:
Thanx Jon, You have always been so helpful. Here is the code that is causing
problem with parameterized queries (I dont want to use SqlCommand object
since in my whole application, i have used OdbcCommand object):
OdbcCommand cmd = new OdbcCommand();
OdbcConnection cn = new OdbcConnection("dsn=test;uid=;pwd=");
string query = "insert into Person (name,phone) values(?name,?phone)";
cmd.CommandType = CommandType.Text;
cmd.Connection = cn;
cmd.CommandText = query;
OdbcParameter param1 = new OdbcParameter();
param1.DbType = DbType.String;
param1.ParameterName = "?name";
param1.Value = "Test";
cmd.Parameters.Add(param1);
<snip>
I believe the problem is one which is slightly obscurely documented, in
OdbcCommand.Parameters:
<quote>
When CommandType is set to Text, the .NET Framework Data Provider for
ODBC does not support passing named parameters to an SQL statement or
to a stored procedure called by an OdbcCommand. In either of these
cases, use the question mark (?) placeholder. For example:
SELECT * FROM Customers WHERE CustomerID = ?
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.
</quote>
In other words, you should use:
"insert into Person (name, phone) values (?, ?)"
At that point, it doesn't matter what names you give the parameters,
but they have to be in the right order.