string where="1=1"; if(textbox1.Text!="") { where +="a='"+textbox1.Text+"'"; } if(textbox2.Text!="") { where +="a='"+textbox2.Text+"'"; } if(textbox3.Text!="") { where +="a='"+textbox3.Text+"'"; }......... 这样不行吗
string where="1=1"; if(textbox1.Text!="") { where +="and a='"+textbox1.Text+"'"; } if(textbox2.Text!="") { where +=" and a='"+textbox2.Text+"'"; } if(textbox3.Text!="") { where +=" and a='"+textbox3.Text+"'"; }刚写错了额,。
if(textbox1.Text!="")
{
where +="a='"+textbox1.Text+"'";
}
if(textbox2.Text!="")
{
where +="a='"+textbox2.Text+"'";
}
if(textbox3.Text!="")
{
where +="a='"+textbox3.Text+"'";
}.........
这样不行吗
if(textbox1.Text!="")
{
where +="and a='"+textbox1.Text+"'";
}
if(textbox2.Text!="")
{
where +=" and a='"+textbox2.Text+"'";
}
if(textbox3.Text!="")
{
where +=" and a='"+textbox3.Text+"'";
}刚写错了额,。
/// <summary>
/// 根据日期,资源名称,资源描述查询资源信息
/// </summary>
/// <returns></returns>
public DataSet FN_SerchByDateAndType(Guid FolderId, NRModel.File model, string createdate, string endate)
{
string strSql = "select * from t_File where 1 =1 and FolderId=@FolderId";
string strWhere = "";
if (!string.IsNullOrEmpty(model.FileNam))
{
strWhere += " and FileNam like @FileNam";
}
//if (!string.IsNullOrEmpty(model.Decription)k)
//{
// strWhere += " and Decription like @Decription";
//}
if (!string.IsNullOrEmpty(createdate) || !string.IsNullOrEmpty(endate))
{
strWhere += " and CreateOn between @createdate and @endate order by ModefyOn desc";
}
strSql += strWhere;
SqlParameter[] parameters = {
new SqlParameter("@FolderId", SqlDbType.UniqueIdentifier),
new SqlParameter("@FileNam", SqlDbType.NVarChar, 256),
new SqlParameter("@createdate", SqlDbType.NVarChar),
new SqlParameter("@endate", SqlDbType.NVarChar)
};
parameters[0].Value = FolderId;
parameters[1].Value = "%" + model.FileNam + "%";
//parameters[1].Value = "%" + model.Decription + "%";
parameters[2].Value = createdate;
parameters[3].Value = endate;
return DbHelperSQL.Query(strSql, parameters);
//SqlParameter[] parameters = new SqlParameter[4];
//parameters[0] = new SqlParameter("@FileNam", model.FileNam);
//parameters[1] = new SqlParameter("@stardate", createdate);
//parameters[2] = new SqlParameter("@enddate", endate);
////执行存储过程
//return DbHelperSQL.RunProcedure("P_UserSerch", parameters, "t_File");
}
string sql = "select * from table ";
string where = "";
foreach(Control ctl in panel1.Controls)
{
if(ctl is TextBox)
{
where += ','' + (ctl as TextBox).Text + ''';
}
}
if(where != "")
{
sql = sql + "where a in (" + where.SubString(1) + ")";
}
//执行sql语句...略
如果是不同字段,那就乖乖的if语句判断拼接吧。
引号写错了,改下:
where += ",'" + (ctl as TextBox).Text + "'";
conn1.Open();
string strwhere = "1=1";
if (TextBox2.Text != "")
{
strwhere += "LawFJ='" + TextBox2.Text + "'";
}
if (TextBox3.Text != "")
{
strwhere += "LawZD='" + TextBox3.Text + "'";
}
if (TextBox4.Text != "")
{
strwhere += "Yyqxdw='" + TextBox4.Text + "'";
}
string sqlstr = "SELECT basic.*,[check].*,View_main.* FROM basic INNER JOIN [check] ON basic.cId = [check].CId INNER JOIN View_main ON basic.cId = View_main.CId where + strWhere ";
SqlDataAdapter da1 = new SqlDataAdapter(sqlstr, conn1);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
this.GridView2.DataSource = ds1;
this.GridView2.DataBind();