准备具有中文字符的语句

时间:2016-12-16 07:19:14

标签: c# winforms

我有一个winform和一个文本框,它会将值传递给像这样的预备语句

searchKey = "member_chinese_name";
field_name = "member_chinese_name";
daoQuery = "SELECT * FROM member where member_chinese_name like @" + searchKey;

sqlCmd = new MySqlCommand(daoQuery, databaseConnection);
MessageBox.Show(field_name + " " + field_value1);
sqlCmd.Parameters.Add(new MySqlParameter("@"+field_name , field_value1 + "%"));
sqlCmd.CommandTimeout = 60;
sqlCmd.ExecuteNonQuery();

adapter.SelectCommand = sqlCmd;
adapter.Fill(ds);

整个查询是(从成员中选择*,其中member_chinese_name喜欢中文字%;)

查询在我的winform中没有运行结果,但我在phpmyadmin中运行sql(select * from member其中member_chinese_name如'中文字%')有效

任何人都知道这是什么问题?

备注(搜索英语是好的)

1 个答案:

答案 0 :(得分:0)

问题可能是您要为搜索发送的参数。它应该是@searchKey而不是@" + searchKey;,您也可以选择sqlCmd.Parameters.AddWithValue而不是sqlCmd.Parameters.Add(new MySqlParameter,因此代码看起来像

searchKey = "member_chinese_name";
field_name = "member_chinese_name";
daoQuery = "SELECT * FROM member where member_chinese_name like @sKey";
sqlCmd = new MySqlCommand(daoQuery, databaseConnection);
MessageBox.Show(field_name + " " + field_value1);
//not sure which variable stores 中文字
sqlCmd.Parameters.AddWithValue("@sKey", field_value1+"%");