如何编写自定义MaskedTextBox的搜索查询

时间:2019-03-23 14:31:00

标签: c# sql sql-server winforms

Using (SqlConnection sqlconn = new SqlConnection(@"Data Source=DESKTOP-IIBSL6N;Initial Catalog=sales_management;Integrated Security=True"))
{
    SqlDataAdapter sqlad = new SqlDataAdapter("select * From Vehicle", sqlconn);
    DataTable dtbl = new DataTable();
    sqlad.Fill(dtbl);
    DataView dv = dtbl.DefaultView;
    dv.RowFilter = string.Format("Plate_Number like '%__ - _ - ___ - __%'", searchcompany.Text);
    vehiclegrid.DataSource = dv.ToTable();
} 

我遇到以下问题:"Plate_Number like '%__ - _ - ___ - __%'"

我不知道这是否正确:00 - 000 - a - 00自定义格式。
如果正确,该怎么做才能在DataGridView中搜索具有自定义屏蔽格式的列?

2 个答案:

答案 0 :(得分:0)

Sql接受必需的表达式(但有一些限制),您可以执行以下操作:

Plate_Number like '%[0-9][0-9] - [0-9] - [0-9][0-9][0-9] - [0-9][0-9]%'

所以:

SqlDataAdapter sqlad = new SqlDataAdapter("select * From Vehicle where Plate_Number like '%[0-9][0-9] - [0-9] - [0-9][0-9][0-9] - [0-9][0-9]%'", sqlconn); 

这里是LIVE DEMO

答案 1 :(得分:0)

using (SqlConnection sqlconn = new SqlConnection(@"Data Source=DESKTOP-IIBSL6N;Initial Catalog=sales_management;Integrated Security=True"))
        {
            if(searchplate.MaskFull==true)
            {
                SqlDataAdapter sqlad = new SqlDataAdapter("select * From Vehicle  ", sqlconn);
                DataTable dtbl = new DataTable();
                sqlad.Fill(dtbl);
                DataView dv = dtbl.DefaultView;
                dv.RowFilter = string.Format("Plate_Number like '%{0}%'", searchplate.Text);
                vehiclegrid.DataSource = dv.ToTable();  }
            }

问题在于,与普通文本框不同,对于maskedtextbox而言,必须设置条件才能进行搜索。