我可以指出我的 MySQL 参数应该放在哪里比仅仅使用 更有意义吗?标记位置 - 使用 ODBC 连接器和 .NET
我有一段代码,可以通过 ODBC 连接使用参数将信息传递到 MySQL 命令中。 显示使用字符串 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");
}
我想知道的是我是否可以指示我的参数应该放在哪里比仅仅使用 更有意义?标记位置 - 因为我可以看到如果有多个参数被替换,这将变得很难调试。
我想以如下方式为参数提供一个名称:
SELECT Firstname, Address, Postcode FROM customers WHERE Surname = ?surname ",
当我尝试这样做时,它只是窒息。
以下代码
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;
}
}
来自 http://www.programmingado .net/a-389/MySQL-NET-parameters-in-query.aspx 片段
where id=?id
并包括理想的
。这是否只能通过 .Net 连接器而不是 ODBC 获得? 如果可以使用 ODBC,我需要如何更改代码片段才能启用此功能?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 ODBC 有此功能就好了,但恐怕它没有。我在这里谈论的是 C API 本身 - 我不知道是否有 C# 层添加了它。不过,编写自己的包装函数来实现它的简单版本并不是太困难。
It would be nice if ODBC had this feature but it doesn't, I'm afraid. I'm speaking of the C API itself here - I don't know if there is a C# layer that adds it. It's not too difficult to write your own wrapper function to implement a simple version of it though.