Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I indicate where my MySQL parameter should go more meaningfully than just having a ? to mark the position - using ODBC connectors and .NET
    text
    copied!<p>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:</p> <pre><code>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"); } </code></pre> <p>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.</p> <p>I'd like to provide a name to the parameter in a manner something like this:</p> <pre><code>SELECT Firstname, Address, Postcode FROM customers WHERE Surname = ?surname ", </code></pre> <p>When I try this it just chokes.</p> <p>The following code</p> <pre><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; } } </code></pre> <p>is from <a href="http://www.programmingado.net/a-389/MySQL-NET-parameters-in-query.aspx" rel="nofollow noreferrer">http://www.programmingado.net/a-389/MySQL-NET-parameters-in-query.aspx</a> and includes the fragment</p> <pre><code>where id=?id </code></pre> <p>Which would be ideal.</p> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload