sql:
select * from yourtable where 编号=isnull(@编号,编号)给参数赋值时做一下判断: if(传过来编号的值==string.Empty())
{
编号参数 = DBNull.Value;
}
else
{
编号参数=传过来的值;}
select * from yourtable where 编号=isnull(@编号,编号)给参数赋值时做一下判断: if(传过来编号的值==string.Empty())
{
编号参数 = DBNull.Value;
}
else
{
编号参数=传过来的值;}
private void BindGridS()
{
String sql="select * from mainout where 1=1";
if(TextBox1.Text!="")
{
sql=sql+" and outnumber like @outnumber";
}
if(TextBox2.Text!="")
{
sql=sql+" and account like @account";
}
SqlConnection cn=new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
SqlCommand cm=new SqlCommand(sql,cn);
cm.Parameters.Add(new SqlParameter("@outnumber",SqlDbType.NVarChar,50));
cm.Parameters["@outnumber"].Value="%"+TextBox3.Text+"%";
cm.Parameters.Add(new SqlParameter("@account",SqlDbType.NVarChar,50));
cm.Parameters["@account"].Value="%"+TextBox4.Text+"%";
SqlDataAdapter da=new SqlDataAdapter(cm);
DataSet ds=new DataSet();
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}与此同理
麻烦帮我解释一下,谢谢了
if(编号.text!="")
{
strSQL=strSQL+" and 编号字段名='"+编号.text+"'";
}
if(名称.text!="")
{
strSQL=strSQL+" and 名称字段='"+名称.text+"'";
}
以下一样解决。其中1=1是为true ,为以后加and 符号做好充分的准备。明白了吗?
首先获得要查询的条件,以及所对应的字段,如编号=“”;所属公司编号=“”;
其次要获得条件的逻辑关系,如符合逻辑关系,则设定条件的关系
解下来根据条件和逻辑关系生成相应的SQL语句,再执行该就行了。
如果写上where 1=1就可以把后面的SQL语句统一为and...
可能说得不清楚,你仔细想想就明白了.
如果你有固定的where条件,不论哪项组合都要用到同一个条件,就可以用这个条件替换,如果没有固定的一个条件就用where 1=1方便后面一句SQL的统一.
组合查询
private void BindGridS()
{
String sql="select * from mainout where 1=1";
if(TextBox1.Text!="")
{
sql=sql+" and outnumber like @outnumber";
}
if(TextBox2.Text!="")
{
sql=sql+" and account like @account";
}
SqlConnection cn=new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
SqlCommand cm=new SqlCommand(sql,cn);
cm.Parameters.Add(new SqlParameter("@outnumber",SqlDbType.NVarChar,50));
cm.Parameters["@outnumber"].Value="%"+TextBox1.Text+"%";
cm.Parameters.Add(new SqlParameter("@account",SqlDbType.NVarChar,50));
cm.Parameters["@account"].Value="%"+TextBox2.Text+"%";
SqlDataAdapter da=new SqlDataAdapter(cm);
DataSet ds=new DataSet();
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
你可以为string strSQL="select * from yourtable where id>0";