想用一个gridview,一个dropdownlist,一个textbox,两个button完成查询与在结果中查询,求在结果中查询解答
查询的代码如下protectedvoidbtnSearch_Click(objectsender,EventArgse){stringb=ddlSearch.Select...
查询的代码如下
protected void btnSearch_Click(object sender, EventArgs e)
{
string b = ddlSearch.SelectedValue;
string sqlstr2 = "select * from db_test";
if (txtSearch.Text =="")
{
sqlstr2 = "select * from db_test";
}
else
{
if (b =="文件名")
{
sqlstr2 = "select * from db_test where File_Name like '%"+txtSearch.Text.ToString()+"%'";
}
if (b =="文件ID")
{
sqlstr2 = "select * from db_test where FileID like '%"+txtSearch.Text.ToString()+"%'";
}
if (b =="部门")
{
sqlstr2 = "select * from db_test where Department like '%"+txtSearch.Text.ToString()+"%'";
}
}
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr2, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "db_test");
DataView view = myds.Tables["db_test"].DefaultView;
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
view.Sort = sort;
GridView1.DataKeyNames = new string[] { "File_Name" };
GridView1.DataSource = view;
GridView1.DataBind();
sqlcon.Close();
ViewState["sqlstr2"] = sqlstr2;
}
本来在结果中查询,想在查询条件中加一个and,但如果两次查询的字段相同的话不知道该怎么办 展开
protected void btnSearch_Click(object sender, EventArgs e)
{
string b = ddlSearch.SelectedValue;
string sqlstr2 = "select * from db_test";
if (txtSearch.Text =="")
{
sqlstr2 = "select * from db_test";
}
else
{
if (b =="文件名")
{
sqlstr2 = "select * from db_test where File_Name like '%"+txtSearch.Text.ToString()+"%'";
}
if (b =="文件ID")
{
sqlstr2 = "select * from db_test where FileID like '%"+txtSearch.Text.ToString()+"%'";
}
if (b =="部门")
{
sqlstr2 = "select * from db_test where Department like '%"+txtSearch.Text.ToString()+"%'";
}
}
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr2, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "db_test");
DataView view = myds.Tables["db_test"].DefaultView;
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
view.Sort = sort;
GridView1.DataKeyNames = new string[] { "File_Name" };
GridView1.DataSource = view;
GridView1.DataBind();
sqlcon.Close();
ViewState["sqlstr2"] = sqlstr2;
}
本来在结果中查询,想在查询条件中加一个and,但如果两次查询的字段相同的话不知道该怎么办 展开
2个回答
展开全部
protected void btnSearch_Click(object sender, EventArgs e)
{
string b = ddlSearch.SelectedValue;
string sqlstr2 = "select * from db_test";
if (txtSearch.Text =="")
{
sqlstr2 = "select * from db_test";
}
else
{
if (b =="文件名")
{
sqlstr2 = "select * from db_test where File_Name like '%"+txtSearch.Text.ToString()+"%'";
}
if (b =="文件ID")
{
sqlstr2 = "select * from db_test where FileID like '%"+txtSearch.Text.ToString()+"%'";
}
if (b =="部门")
{
sqlstr2 = "select * from db_test where Department like '%"+txtSearch.Text.ToString()+"%'";
}
}
这么改
string b = ddlSearch.SelectedValue;
string sqlstr2 = "select * from db_test where 1=1";
string cond1="";
string cond2="";
string cond3="";
if (b =="文件名")
{
cond1=" and File_Name like '%"+txtSearch.Text.ToString()+"%'"
}
if (b =="文件ID")
{
cond1= " and FileID like '%"+txtSearch.Text.ToString()+"%'";
}
if (b =="部门")
{
cond3 = " and Department like '%"+txtSearch.Text.ToString()+"%'";
}
sqlstr2 +=sqlstr2 +cond1+cond2+cond3;
下面就一样了
{
string b = ddlSearch.SelectedValue;
string sqlstr2 = "select * from db_test";
if (txtSearch.Text =="")
{
sqlstr2 = "select * from db_test";
}
else
{
if (b =="文件名")
{
sqlstr2 = "select * from db_test where File_Name like '%"+txtSearch.Text.ToString()+"%'";
}
if (b =="文件ID")
{
sqlstr2 = "select * from db_test where FileID like '%"+txtSearch.Text.ToString()+"%'";
}
if (b =="部门")
{
sqlstr2 = "select * from db_test where Department like '%"+txtSearch.Text.ToString()+"%'";
}
}
这么改
string b = ddlSearch.SelectedValue;
string sqlstr2 = "select * from db_test where 1=1";
string cond1="";
string cond2="";
string cond3="";
if (b =="文件名")
{
cond1=" and File_Name like '%"+txtSearch.Text.ToString()+"%'"
}
if (b =="文件ID")
{
cond1= " and FileID like '%"+txtSearch.Text.ToString()+"%'";
}
if (b =="部门")
{
cond3 = " and Department like '%"+txtSearch.Text.ToString()+"%'";
}
sqlstr2 +=sqlstr2 +cond1+cond2+cond3;
下面就一样了
追问
在第二个button里试了下,查询出来的总是整个数据库,能告诉我string sqlstr2 = "select * from db_test where 1=1"中where后面是什么意思吗?应该就是这里出错了
追答
select * from db_test where 1=1
其实 等于是select * from db_test
只是拼装语句的时候比较方便
条件都是 "and 列名=值" 这样的格式
如果没有 where 1=1,那还需要判断第一个条件,不加and
查询出来总是整个数据库?条件进去了吗?
你调试一下,看看SQL语句
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询