【发布时间】:2019-07-15 08:48:05
【问题描述】:
我的代码如下:
if (!string.IsNullOrEmpty(Mpdue.TheObjectPropertyNameNs))
{
string sql = @String.Format(" SELECT * FROM doTable WHERE dOCode IN ('" + Mpdue.TheObjectPropertyNameNs + "'); ");
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand command =
new OdbcCommand(sql, myConnectionString))
{
try
{
command.Connection.Open();
using (OdbcDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
Response.Write(reader["dOCode"].ToString() + "<br />");
}
}
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
}
输出:
D410
D420
D430
D440
现在我尝试在MySql 查询中使用command.Parameters.AddWithValue。
我一直在尝试找不到错误,使用command.Parameters.AddWithValue时输出为空,为什么?
if (!string.IsNullOrEmpty(Mpdue.TheObjectPropertyNameNs))
{
string sql = @String.Format(" SELECT * FROM doTable WHERE dOCode IN (?); ");
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand command =
new OdbcCommand(sql, myConnectionString))
{
try
{
command.Connection.Open();
command.Parameters.AddWithValue("param1", Mpdue.TheObjectPropertyNameNs);
using (OdbcDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
Response.Write(reader["dOCode"].ToString() + "<br />");
}
}
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
}
编辑#2
var parameters = new string[Mpdue.TheObjectPropertyNameNs.Length];
var paramValue = string.Join(", ", parameters);
string sql = string.Format("SELECT * FROM doTable WHERE dOCode IN ({0})", string.Join(", ", parameters.Select(x => "?")));
int index = 0;
foreach (var param in parameters)
{
command.Parameters.AddWithValue("param{index}", param);
index++;
}
编辑#1
我的新代码如下:
if (!string.IsNullOrEmpty(Mpdue.TheObjectPropertyNameNs))
{
var parameters = new string[Mpdue.TheObjectPropertyNameNs.Length];
string sql = @String.Format(" SELECT * FROM doTable WHERE dOCode IN ({0})", string.Join(", ", parameters));
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand command =
new OdbcCommand(sql, myConnectionString))
{
try
{
command.Connection.Open();
for (int i = 0; i < Mpdue.TheObjectPropertyNameNs.TrimStart('\'').TrimEnd('\'').Length; i++)
{
parameters[i] = string.Format("param1{0}", i);
command.Parameters.AddWithValue(parameters[i], Mpdue.TheObjectPropertyNameNs.TrimStart('\'').TrimEnd('\'')[i]);
}
using (OdbcDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
Response.Write(reader["dOCode"].ToString() + "<br />");
}
}
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
}
错误:
ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.5.24-log]你有一个 SQL 语法错误;检查与您对应的手册 MySQL 服务器版本,用于在 ' , , , , , , , 附近使用正确的语法 , , , , , , , , , , , , , , , , , , ) 在第 1 行
【问题讨论】:
-
问题可能是您在第一种方法中插入了一个值列表,例如
IN ('A', 'B'),而在使用AddWithValue时,您将所有值都插入到一个以IN ('A. B')结尾的参数中。也许这个问题有帮助:stackoverflow.com/q/13116042/642579 -
您在母版页中执行此操作的事实仅表示您在错误的位置执行此操作,与您的问题无关
-
我不知道 ODBC 对象是否会与 SQL 对象一样工作,但这可能会有所帮助:stackoverflow.com/questions/2377506/…
-
考虑改用 Dapper。 dapper-tutorial.net/parameter-list