c# - [MySQL][ODBC 5.1 驱动程序][mysqld-5.1.51-community]SQLBindParameter 未用于所有参数

标签 c# mysql

我在 MySQL 数据库上的查询遇到问题:

SELECT * FROM dotable WHERE CO IN ('AAA','BBB','CCC')

如果在 MySQL 中启动此查询,则该查询有效。

如果在 C# 中尝试,我会遇到此错误:

ERROR [07001] [MySQL][ODBC 5.1 Driver][mysqld-5.1.51-community]SQLBindParameter not used for all parameters

你能帮我解决这个问题吗?

提前致谢。

下面是我的代码。

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;
                }
            }

至:

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;

            }

关于c# - [MySQL][ODBC 5.1 驱动程序][mysqld-5.1.51-community]SQLBindParameter 未用于所有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36420906/

相关文章:

c# - 在ViewModel、ASP.net MVC中定义 "Model"类型的属性好不好

php - 自动完成电影名称

c# - 更改带有圆角器的颜色按钮

c# - Parallel.For 循环卡住

c# - 从 Asp.Net WebApi 返回字符串后的 JsonConvert DeserializeObject 异常

c# - 我什么时候应该更喜欢属性而不是方法?

php - MySQL PHP PDO 获取列中值为 Yes 的所有用户

python - 使用 Django 将 Unicode 字符存储到 MySQL 时出现问题

mysql - 分割地址

Windows 7 上的 Mysql 时区未更新