--可以用永真条件,如:1=1等类似表达式 private void button1_Click(object sender, System.EventArgs e)
{
strSQL = "select * from Info where 1=1 ";
if (tbnum.Text.ToString() != "")
strSQL += " and 工资号='" + tbnum.Text.ToString() + "' ";
if (tbname.Text.ToString() != "")
strSQL += " and 姓名='" + tbname.Text.ToString() + "' ";
if (tbdepart.Text.ToString() != "")
strSQL += " and 部门='" + tbdepart.Text.ToString() + "' ";
if (tbtype.Text.ToString() != "")
strSQL += " and 人员分类='" + tbtype.Text.ToString() + "' ";
if (tbyear.Text.ToString() != "")
strSQL += " and 年='" + tbyear.Text.ToString() + "' ";
if (tbmonth.Text.ToString() != "")
strSQL += " and 月='" + tbmonth.Text.ToString() + "' "; cmd = new SqlCommand(strSQL, conn);
conn.Open(); cmd.ExecuteNonQuery();
SqlDataAdapter da;
da = new SqlDataAdapter(cmd);
da.Fill(myData);
dg1.SetDataBinding(myData, myData.Tables[0].ToString());
conn.Close();
}
{
strSQL = "select * from Info where 1=1 ";
if (tbnum.Text.ToString() != "")
strSQL += " and 工资号='" + tbnum.Text.ToString() + "' ";
if (tbname.Text.ToString() != "")
strSQL += " and 姓名='" + tbname.Text.ToString() + "' ";
if (tbdepart.Text.ToString() != "")
strSQL += " and 部门='" + tbdepart.Text.ToString() + "' ";
if (tbtype.Text.ToString() != "")
strSQL += " and 人员分类='" + tbtype.Text.ToString() + "' ";
if (tbyear.Text.ToString() != "")
strSQL += " and 年='" + tbyear.Text.ToString() + "' ";
if (tbmonth.Text.ToString() != "")
strSQL += " and 月='" + tbmonth.Text.ToString() + "' "; cmd = new SqlCommand(strSQL, conn);
conn.Open(); cmd.ExecuteNonQuery();
SqlDataAdapter da;
da = new SqlDataAdapter(cmd);
da.Fill(myData);
dg1.SetDataBinding(myData, myData.Tables[0].ToString());
conn.Close();
}
--随便可以COPY它到查询分析器,看语句能否执行,这样先排除是SQL语句出错的问题后,再看你程序有没问题
cmd=new SqlCommand(strSQL,conn);