c# - 使用多文本框窗口形式搜索 C#
问题描述
public void filter()
{
using (SqlConnection sqlconn = new SqlConnection(@"Data Source=DESKTOP-IIBSL6N;Initial Catalog=sales_management;Integrated Security=True"))
{
SqlDataAdapter sqlad = new SqlDataAdapter("select * From Customer", sqlconn);
DataTable dtbl = new DataTable();
sqlad.Fill(dtbl);
DataView dv = dtbl.DefaultView;
dv.RowFilter = string.Format("Name like '%{0}%' and Address like '%{0}% and office_number like '" + searchoffice.Text + "%'and phone_number like '" + searchphone.Text + "%' and acount_name like '%{0}%'", searchname.Text,searchaddress.Text,searchoffice.Text,searchphone.Text,searchaccountname.Text);
customergrid.DataSource = dv.ToTable();
dtbl.DefaultView.Sort = "[Name] DESC";
}
}
textbox.textchange()
当我在-EventHandler中运行此方法时,我得到以下异常:
The expression contains an invalid string constant: '
请帮我修复异常。
解决方案
似乎您的查询字符串在 附近缺少一个空格office_number like '" + searchoffice.Text + "%' and phone_number
,还缺少一个 '-字符并且 String.Format- 参数计数不匹配。
所以尝试以下:
public void filter()
{
using (SqlConnection sqlconn = new SqlConnection(@"Data Source=DESKTOP-IIBSL6N;Initial Catalog=sales_management;Integrated Security=True"))
{
SqlDataAdapter sqlad = new SqlDataAdapter("select * From Customer", sqlconn);
DataTable dtbl = new DataTable();
sqlad.Fill(dtbl);
DataView dv = dtbl.DefaultView;
dv.RowFilter = string.Format("Name like '%{0}%' and Address like ‘%{1}%’ and office_number like '" + searchoffice.Text + "%' and phone_number like '" + searchphone.Text + "%' and acount_name like '%{0}%'", searchname.Text,searchaddress.Text);
customergrid.DataSource = dv.ToTable();
dtbl.DefaultView.Sort = "[Name] DESC";
}
}
推荐阅读
- wordpress - Woocommerce Checkout - 某些国家/地区未显示“状态”字段 - 导致付款错误
- node.js - 如何使用 React/Node.js/Express 框架异步提交 POST 请求?
- azure-devops - 无法从 Azure DevOps Artifact Feed 访问 Chocolatey 包
- javascript - 如何在 php 中捕获 JSON 数据以将其发送到我的电子邮件
- mysql - MySql 8.0 分区插件启用失败
- xamarin.forms - ListView 不显示来自 Xaml 中 ListView.ItemSource 绑定的文本
- r - 如何在绘制 SVM 模型时确定切片函数中变量的常数
- mysql - 如何在一个查询中过滤重复和分组?
- c# - 使用文本框检查数据库中的重复值
- python - 通过集合列表过滤出现次数