private void button2_Click(object sender, EventArgs e)
{
List<string> strwhere = new List<string>();
SqlParameter[] p = new SqlParameter[4];
if (!string.IsNullOrEmpty(textCusId.Text))
{
strwhere.Add("CusId like @CusId");
p[0] = (new SqlParameter("@CusId", "%" + textCusId.Text + "%"));
}
if (!string.IsNullOrEmpty(textCusName.Text))
{
strwhere.Add("CusName like @CusName");
p[1] = (new SqlParameter("@CusName", "%" + textCusName.Text + "%"));
}
if (!string.IsNullOrEmpty(textCusType.Text))
{
strwhere.Add("CusType like @CusType");
p[2] = (new SqlParameter("@CusType", "%" + textCusType.Text + "%"));
}
if (!string.IsNullOrEmpty(textTel.Text))
{
strwhere.Add("Tel like @Tel");
p[3] = (new SqlParameter("@Tel", "%" + textTel.Text + "%"));
}
string sql;
if (strwhere.Count != 0)
{
string strwheres = string.Join(" and ", strwhere);
sql = "select CusId as 客户编号 from Customer where " + strwheres+"";
}
else
{
sql = "select CusId as 客户编号 from Customer";
}
SqlCommand cmd = new SqlCommand(sql, cn);
if(p!=null)
{
foreach (SqlParameter parm in p)
if(parm!=null)
{
cmd.Parameters.Add(parm);
}
}
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
cn.Close();
}
{
List<string> strwhere = new List<string>();
SqlParameter[] p = new SqlParameter[4];
if (!string.IsNullOrEmpty(textCusId.Text))
{
strwhere.Add("CusId like @CusId");
p[0] = (new SqlParameter("@CusId", "%" + textCusId.Text + "%"));
}
if (!string.IsNullOrEmpty(textCusName.Text))
{
strwhere.Add("CusName like @CusName");
p[1] = (new SqlParameter("@CusName", "%" + textCusName.Text + "%"));
}
if (!string.IsNullOrEmpty(textCusType.Text))
{
strwhere.Add("CusType like @CusType");
p[2] = (new SqlParameter("@CusType", "%" + textCusType.Text + "%"));
}
if (!string.IsNullOrEmpty(textTel.Text))
{
strwhere.Add("Tel like @Tel");
p[3] = (new SqlParameter("@Tel", "%" + textTel.Text + "%"));
}
string sql;
if (strwhere.Count != 0)
{
string strwheres = string.Join(" and ", strwhere);
sql = "select CusId as 客户编号 from Customer where " + strwheres+"";
}
else
{
sql = "select CusId as 客户编号 from Customer";
}
SqlCommand cmd = new SqlCommand(sql, cn);
if(p!=null)
{
foreach (SqlParameter parm in p)
if(parm!=null)
{
cmd.Parameters.Add(parm);
}
}
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
cn.Close();
}