我对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 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();
}
}
}
}
答案 0 :(得分:1)
请更改此内容:
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;
}
}
为:
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;
}