![](/img/trans.png)
[英]C# : Cancelling MySqlCommand using CancellationToken giving NULLReferenceException
[英]How to use the wildcard% in mysqlcommand using c#
请帮助我,我的教授以前做过,但是我忘了怎么做。 如果可能,我现在需要它。 如何在此代码中使用通配符%? 提前致谢!!
MySqlCommand SelectCommand = new MySqlCommand("select * from sms.members where memberFName +' '+ memberLName like'" +cmbmemsched.Text+ "';", myconn);
您最好使用参数化查询来避免SQL注入:
MySqlCommand selectCommand = new MySqlCommand(
"SELECT * FROM sms.members WHERE memberFName LIKE @memberFName;",
myconn
);
selectCommand.Parameters.AddWithValue(@memberFName, "%" + cmbmemsched.Text + "%");
在此示例中,LIKE语句将在值中间的任何位置查找搜索词组。 如果要查找以指定过滤器开头或结尾的记录,则需要调整参数中的%
。
我还强烈建议您在using
语句中包装IDisposable
资源(例如SQL命令),以确保即使抛出某些异常也可以正确处理它们:
using (MySqlCommand selectCommand = new MySqlCommand("SELECT * FROM sms.members WHERE memberFName LIKE @memberFName;", myconn))
{
selectCommand.Parameters.AddWithValue(@memberFName, "%" + cmbmemsched.Text + "%");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.