本人新手想做一个网页查询功能
一个数据表,我想按不同的字段查询,我用vs2005开发 显示主要是gridview
自己写了一段代码,老是用不好,请问大家怎么写这个分类查询 protected void bind()
{
SqlConnection con = DB.createconnection();
con.Open();
string dm1 = "";
if (TextBox1.Text == "")
{
dm1 = "select * from js_zlssbk";
}
else
{
if (DropDownList1.Text == "水表代码")
{
dm1 = "select * from js_zlssbk where 代码 =" + TextBox1.Text;
}
if (DropDownList1.Text == "水表户名")
{
dm1 = "select * from js_zlssbk where 户名 like '%" + TextBox1.Text + "%'";
}
if (DropDownList1.Text == "水表位置")
{
dm1 = "select * from js_zlssbk where 位置 like '%" + TextBox1.Text + "%'";
}
} SqlDataAdapter sda = new SqlDataAdapter(dm1, con);
DataSet ds = new DataSet();
sda.Fill(ds, "js_zlssbk");
GridView1.DataSource = ds.Tables["js_zlssbk"];
GridView1.AllowPaging = true;
GridView1.PageSize = 20;
GridView1.DataBind();
一个数据表,我想按不同的字段查询,我用vs2005开发 显示主要是gridview
自己写了一段代码,老是用不好,请问大家怎么写这个分类查询 protected void bind()
{
SqlConnection con = DB.createconnection();
con.Open();
string dm1 = "";
if (TextBox1.Text == "")
{
dm1 = "select * from js_zlssbk";
}
else
{
if (DropDownList1.Text == "水表代码")
{
dm1 = "select * from js_zlssbk where 代码 =" + TextBox1.Text;
}
if (DropDownList1.Text == "水表户名")
{
dm1 = "select * from js_zlssbk where 户名 like '%" + TextBox1.Text + "%'";
}
if (DropDownList1.Text == "水表位置")
{
dm1 = "select * from js_zlssbk where 位置 like '%" + TextBox1.Text + "%'";
}
} SqlDataAdapter sda = new SqlDataAdapter(dm1, con);
DataSet ds = new DataSet();
sda.Fill(ds, "js_zlssbk");
GridView1.DataSource = ds.Tables["js_zlssbk"];
GridView1.AllowPaging = true;
GridView1.PageSize = 20;
GridView1.DataBind();
解决方案 »
- checkboxlist怎么追加选项呢
- 检察字符串前是否有"http://"(急)
- 关于XML读取问题(急)
- ??高难度问题,在线等待。在ASPNET中如何实现网上邻居的文件存储功能???
- .net中在前台代码设置输入文本框非空的问题!
- Encoding的一个问题,搞了一下午,急死我了!大家帮忙啊!
- 上传网页的问题
- 怎样用循环语句给100个TextBox的text属性赋值?
- Nhibernate缓存问题
- 如何控制BUTTON控件中文本的显示位置
- 请问各位有详细设计的模板吗?请提供一个,谢谢
- 请教:我的机子是VS2005+IE7,写的JS语句在IE7下运行正常,但是在IE6和FF中却出错。请教如何用IE6和FF调试
{
dm1 = "select * from js_zlssbk where 1=1";
if (DropDownList1.Text == "水表代码")
{
dm1 =dm1 + " and 代码 =" + TextBox1.Text;
}
if (DropDownList1.Text == "水表户名")
{
dm1 =dm1 + " and 户名 like '%" + TextBox1.Text + "%'";
}
if (DropDownList1.Text == "水表位置")
{
dm1 =dm1 + " and 位置 like '%" + TextBox1.Text + "%'";
}
}
protected void bind()
{
SqlConnection con = DB.createconnection();
con.Open();
string dm1 = "";
if (TextBox1.Text == "")
{
dm1 = " select * from js_zlssbk where 1=1 ";
}
else
{
if (DropDownList1.Text == "水表代码")
{
dm1 += " and 代码 =" + TextBox1.Text;
}
if (DropDownList1.Text == "水表户名")
{
dm1+ = " and 户名 like '%" + TextBox1.Text + "%' ";
}
if (DropDownList1.Text == "水表位置")
{
dm1 += " and 位置 like '%" + TextBox1.Text + "%'";
}
}
SqlDataAdapter sda = new SqlDataAdapter(dm1, con);
DataSet ds = new DataSet();
sda.Fill(ds, "js_zlssbk");
GridView1.DataSource = ds.Tables["js_zlssbk"];
GridView1.AllowPaging = true;
GridView1.PageSize = 20;
GridView1.DataBind();