Can I indicate where my MySQL parameter should go more meaningfully than just having a ? to mark the

Posted by Paul H on Stack Overflow See other posts from Stack Overflow or by Paul H
Published on 2010-03-22T10:45:31Z Indexed on 2010/03/22 10:51 UTC
Read the original article Hit count: 450

Filed under:
|
|
|
|

I've got a chunk of code where I can pass info into a MySQL command using parameters through an ODBC connection. Example code showing surname passed in using string surnameToLookFor:

using (OdbcConnection DbConn = new OdbcConnection( connectToDB ))
{

   OdbcDataAdapter cmd = new OdbcDataAdapter(
   "SELECT Firstname, Address, Postcode FROM customers WHERE Surname = ?", DbConn);

    OdbcParameter odbcParam = new OdbcParameter("surname", surnameToLookFor);
    cmd.SelectCommand.Parameters.Add(odbcParam);

    cmd.Fill(dsCustomers, "customers");
}

What I'd like to know is whether I can indicate where my parameter should go more meaningfully than just having a ? to mark the position - as I could see this getting quite hard to debug if there are multiple parameters being replaced.

I'd like to provide a name to the parameter in a manner something like this:

SELECT Firstname, Address, Postcode FROM customers WHERE Surname = ?surname ", 

When I try this it just chokes.

The following code

    public System.Data.DataSet Customer_Open(string sConnString, long ld) 
    { 
        using (MySqlConnection oConn = new MySqlConnection(sConnString)) 
        { 
            oConn.Open(); 

            MySqlCommand oCommand = oConn.CreateCommand(); 
            oCommand.CommandText = "select * from cust_customer where id=?id"; 

            MySqlParameter oParam = oCommand.Parameters.Add("?id", MySqlDbType.Int32); 
            oParam.Value = ld; 

            oCommand.Connection = oConn; 
            DataSet oDataSet = new DataSet(); 
            MySqlDataAdapter oAdapter = new MySqlDataAdapter(); 
            oAdapter.SelectCommand = oCommand; 
            oAdapter.Fill(oDataSet); 
            oConn.Close(); 
            return oDataSet; 
        } 
    }

is from http://www.programmingado.net/a-389/MySQL-NET-parameters-in-query.aspx and includes the fragment

where id=?id

Which would be ideal.

Is this only available through the .Net connector rather than the ODBC? If it is possible to do using ODBC how would I need to change my code fragment to enable this?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about odbc