asp 数据库查询后如何显示在gridview中
usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingS...
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WMS
{
public partial class test1 : System.Web.UI.Page
{
DataSet GetDataSet(string name)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=WWW-76912774763;Initial Catalog=sxhj;Integrated Security=True";
conn.Open();
SqlCommand selectCMD = new SqlCommand("SELECT Uid,Upower FROM Users where Uid like '"+TextBox1.Text.Trim()+"' ", conn);
SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = selectCMD;
DataSet custDS = new DataSet();
custDA.Fill(custDS);
GridView1.DataSource = custDS;
conn.Close();
return custDS;
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = TextBox1.Text.ToString();
DataSet ds = GetDataSet(name);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
程序可以运行,但是我希望在textbox中进行的查询无法实现,点击按钮没有反应,请高手帮忙查找下原因 展开
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WMS
{
public partial class test1 : System.Web.UI.Page
{
DataSet GetDataSet(string name)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=WWW-76912774763;Initial Catalog=sxhj;Integrated Security=True";
conn.Open();
SqlCommand selectCMD = new SqlCommand("SELECT Uid,Upower FROM Users where Uid like '"+TextBox1.Text.Trim()+"' ", conn);
SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = selectCMD;
DataSet custDS = new DataSet();
custDA.Fill(custDS);
GridView1.DataSource = custDS;
conn.Close();
return custDS;
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = TextBox1.Text.ToString();
DataSet ds = GetDataSet(name);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
程序可以运行,但是我希望在textbox中进行的查询无法实现,点击按钮没有反应,请高手帮忙查找下原因 展开
5个回答
展开全部
首先,你这个代码有问题,sql语句like查询的时候要加%符号,虽然不加%语法没有错误,但是查询不到任何结果
然后GetDataSet方法里面不需要绑定,GridView1.DataSource = custDS可以省掉;
最后,祝你成功
然后GetDataSet方法里面不需要绑定,GridView1.DataSource = custDS可以省掉;
最后,祝你成功
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
GridView1.DataSource = custDS;
删了
删了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
改了 你的GetDataSet
DataSet GetDataSet(string name)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=WWW-76912774763;Initial Catalog=sxhj;Integrated Security=True";
SqlCommand selectCMD = new SqlCommand("SELECT Uid,Upower FROM Users where Uid like '%" + name + "%' ", conn);
SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = selectCMD;
DataSet custDS = new DataSet();
custDA.Fill(custDS);
return custDS;
}
DataSet GetDataSet(string name)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=WWW-76912774763;Initial Catalog=sxhj;Integrated Security=True";
SqlCommand selectCMD = new SqlCommand("SELECT Uid,Upower FROM Users where Uid like '%" + name + "%' ", conn);
SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = selectCMD;
DataSet custDS = new DataSet();
custDA.Fill(custDS);
return custDS;
}
追问
详细看了您的修改,运行的过程还是空白,甚至将SQL语句简化为SELECT * FROM Users 依然不行,请教一下还可能在哪里出问题。
追答
你打断点看看ds 有没有数据
在看看GridView1是否被隐藏
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
DataSet GetDataSet(string name)
{
private static strCon= "Data Source=WWW-76912774763;Initial Catalog=sxhj;Integrated Security=True";
SqlConnection conn = new SqlConnection(strCon);
conn.Open();
string sql=string.Format(" SELECT Uid,Upower FROM Users where Uid like '%{0}% '", name);
SqlDataAdapter custDA = new SqlDataAdapter(sql,cn);
DataSet custDS = new DataSet()
custDA.Fill(custDS);
conn.Close();
return custDS;
{
private static strCon= "Data Source=WWW-76912774763;Initial Catalog=sxhj;Integrated Security=True";
SqlConnection conn = new SqlConnection(strCon);
conn.Open();
string sql=string.Format(" SELECT Uid,Upower FROM Users where Uid like '%{0}% '", name);
SqlDataAdapter custDA = new SqlDataAdapter(sql,cn);
DataSet custDS = new DataSet()
custDA.Fill(custDS);
conn.Close();
return custDS;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没找到结果
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询