C# [MySQL][ODBC 5.1驱动程序][mysqld-5.1.51-community]SQLBindParameter未用于所有参数
我对MySQL数据库的这个查询有问题:C# [MySQL][ODBC 5.1驱动程序][mysqld-5.1.51-community]SQLBindParameter未用于所有参数,c#,mysql,C#,Mysql,我对MySQL数据库的这个查询有问题: SELECT * FROM dotable WHERE CO IN ('AAA','BBB','CCC') 如果在MySQL中启动,则此查询可以工作 如果在c#中尝试,我有以下错误: 错误[07001][MySQL][ODBC 5.1 驱动程序][mysqld-5.1.51-community]SQLBindParameter未用于所有 参数 你能帮我解决这个问题吗 提前谢谢 下面是我的代码 private DataSet RetrieveProduct
SELECT * FROM dotable WHERE CO IN ('AAA','BBB','CCC')
如果在MySQL中启动,则此查询可以工作
如果在c#中尝试,我有以下错误:
错误[07001][MySQL][ODBC 5.1
驱动程序][mysqld-5.1.51-community]SQLBindParameter未用于所有
参数
你能帮我解决这个问题吗
提前谢谢
下面是我的代码
private DataSet RetrieveProducts()
{
if (Request.Cookies["username"] != null)
{
sql = string.Format("SELECT * FROM dotable WHERE CO IN ({0}); ", ns);
DataSet dsProducts = new DataSet();
dt1 = new DataTable();
DataTable dtProducts = new DataTable();
dsProducts = new DataSet();
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand(sql, cn))
{
foreach (var co in colorList)
{
cmd.Parameters.AddWithValue(co, co);
OdbcDataAdapter adapter = new OdbcDataAdapter(cmd);
adapter.Fill(dsProducts);
if (dsProducts.Tables.Count > 0)
{
dt1 = dsProducts.Tables[0];
}
dtProducts = dt1;
}
}
}
return dsProducts;
}
else
{
return null;
}
}
protected void Username()
{
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
string sql_aut = @" SELECT * FROM WHERE UPPER(username) = ? AND aut = 1; ";
using (OdbcCommand command =
new OdbcCommand(sql_aut, myConnectionString))
{
try
{
if (Request.Cookies["username"] != null)
{
command.Parameters.AddWithValue("param1", Server.UrlDecode(Request.Cookies["username"].Value.ToUpper()));
command.Connection.Open();
using (OdbcDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
CO = reader["CO"].ToString();
aut = Convert.ToInt32(reader["aut"].ToString());
colorList.Add(idDTES.ToString());
}
ns = "";
ns = string.Join(",", (from co in colorList select "?").ToList());
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('KO1.');window.location='http://...';", true);
}
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('KO2.');window.location='http://...';", true);
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
}
请更改此项:
using (OdbcCommand cmd = new OdbcCommand(sql, cn))
{
foreach (var co in colorList)
{
cmd.Parameters.AddWithValue(co, co);
OdbcDataAdapter adapter = new OdbcDataAdapter(cmd);
adapter.Fill(dsProducts);
if (dsProducts.Tables.Count > 0)
{
dt1 = dsProducts.Tables[0];
}
dtProducts = dt1;
}
}
致:
当您在代码中到达这一行时,
ns
的值是多少?在RetrieveProducts方法中没有参数(就查询而言),因此不需要调用AddWithValue。ns的值是?,?,?
using (OdbcCommand cmd = new OdbcCommand(sql, cn))
{
foreach (var co in colorList)
{
cmd.Parameters.AddWithValue(co, co);
}
OdbcDataAdapter adapter = new OdbcDataAdapter(cmd);
adapter.Fill(dsProducts);
if (dsProducts.Tables.Count > 0)
{
dt1 = dsProducts.Tables[0];
}
dtProducts = dt1;
}