我在WINFORM的FORM1有各种条件,比如产品编号,类型之类的,FORM3中有个DATAGRIDVIEW,想把搜索的结果都填充进去DATAGRIDVIEW
private void button1_Click(object sender, EventArgs e)//点击查询按钮
{
Form3 form3 = new Form3();
DataGridView dataGridView1 = new DataGridView();
form3.Controls.Add(dataGridView1);
string whereText=" WHERE 1=1 ";if(String.String.IsNullOrEmpty(cardid.Text))
{
whereText+="And 车辆型号 LIKE '%"+cardid.Text.Trim()+"%' ";
}
if(String.String.IsNullOrEmpty(carclass.Text))
{
whereText+="And 车辆名称 LIKE '%"+carclass.Text.Trim()+"%' ";
}
if(String.String.IsNullOrEmpty(carnum.Text))
{
whereText+="And 产品号 LIKE '%"+carnum.Text.Trim()+"%' ";
}
string sql = string.Format("SELECT * FROM [clcp$]"+whereText); //这里只是其中的3个条件
SqlDataAdapter dataAapter = new SqlDataAdapter(sql, DBhelper.connection);
DBhelper.connection.Open();
dataset.Clear();
dataAapter.Fill(dataset, "clcp$");
form3.dataGridView1.DataSource = dataset.Tables["clcp$"];
DBhelper.connection.Close();
form3.ShowDialog();
}
class DBhelper
{ //连接字符串
private static string ConnString = "Data Source=00CFDBBC00324BF\\GONGGAO; Initial Catalog=123; Integrated Security=true";
//连接Connection对象 public static SqlConnection connection = new SqlConnection(ConnString);
}
现在我代码写进去,结果搜出来的是数据库的全部内容,请问如何修改才能真正达到模糊搜索的效果··
private void button1_Click(object sender, EventArgs e)//点击查询按钮
{
Form3 form3 = new Form3();
DataGridView dataGridView1 = new DataGridView();
form3.Controls.Add(dataGridView1);
string whereText=" WHERE 1=1 ";if(String.String.IsNullOrEmpty(cardid.Text))
{
whereText+="And 车辆型号 LIKE '%"+cardid.Text.Trim()+"%' ";
}
if(String.String.IsNullOrEmpty(carclass.Text))
{
whereText+="And 车辆名称 LIKE '%"+carclass.Text.Trim()+"%' ";
}
if(String.String.IsNullOrEmpty(carnum.Text))
{
whereText+="And 产品号 LIKE '%"+carnum.Text.Trim()+"%' ";
}
string sql = string.Format("SELECT * FROM [clcp$]"+whereText); //这里只是其中的3个条件
SqlDataAdapter dataAapter = new SqlDataAdapter(sql, DBhelper.connection);
DBhelper.connection.Open();
dataset.Clear();
dataAapter.Fill(dataset, "clcp$");
form3.dataGridView1.DataSource = dataset.Tables["clcp$"];
DBhelper.connection.Close();
form3.ShowDialog();
}
class DBhelper
{ //连接字符串
private static string ConnString = "Data Source=00CFDBBC00324BF\\GONGGAO; Initial Catalog=123; Integrated Security=true";
//连接Connection对象 public static SqlConnection connection = new SqlConnection(ConnString);
}
现在我代码写进去,结果搜出来的是数据库的全部内容,请问如何修改才能真正达到模糊搜索的效果··
去掉1=1如何?