使用多个文本框窗口搜索c#

问题描述 投票:0回答:1
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: '

请帮我修复异常。

c# sql-server windows-forms-designer
1个回答
2
投票

看来您的查询字符串在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";  
        }  
    } 
© www.soinside.com 2019 - 2024. All rights reserved.