【发布时间】:2015-08-04 12:51:14
【问题描述】:
在我的 代码后面 的 aspx 页面中,我在 sql 查询中传递 Parameters 的值时遇到问题。
我使用 MySql 数据库。
第 1 步:
我在 List 中添加查询的输出:
while (reader.Read())
{
idcolor = reader["idcolor"].ToString();
colorList.Add(idcolor.ToString());
}
ns = string.Join("','", colorList.ToArray());
在debug中,输出为:
ns = red','green
第 2 步:
我需要在 sql 查询中使用 string ns 的值。
并在parameters中传递string ns的值:
str = null;
str = ns == null ? "" : ns.ToString();
sql = @" SELECT * FROM Experience WHERE Colors IN (?); ";
DataSet dsColors = new DataSet();
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand(sql, cn))
{
cmd.Parameters.AddWithValue("param1", Server.UrlDecode(str.ToString()));
OdbcDataAdapter adapter = new OdbcDataAdapter(cmd);
adapter.Fill(dsColors);
}
}
return dsColors;
第 3 步:
如果在查询中使用:
sql = @" SELECT * FROM Experience WHERE Colors IN (?); ";
dataset 中的输出为空。
如果在查询中使用:
sql = @" SELECT * FROM Experience WHERE Colors IN ( '" + Server.UrlDecode(str.ToString()) + "' ); ";
dataset 中的输出是对的。
有人知道我该如何解决吗?
你能推荐一下吗?
你能帮帮我吗?
提前谢谢你。
【问题讨论】:
标签: c# mysql sql parameters