c#中如何显示查询数据库中的内容

来源:百度知道 编辑:UC知道 时间:2024/06/16 18:48:21
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=companydb;Integrated Security=True");//连接数据库
conn.Open();//打开数据库;
string sqlstr = "select * from buyer_yyb where buyernumber='" + TextBox1.Text+ "'";
DataSet ds = new DataSet();//数据在内存中的缓存
SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
da.Fill(ds);
GridView1_gktzb.DataSource = ds.Tables[0];
GridView1_gktzb.DataBind();//将GridView绑定到DataSet上
conn.Close();
}
gridview的格式时我自己编辑的
当执行的时候在textbox输入数据后点击查询button后gridview中只出现各个列的属性名
却没有查询内容
我确定我输入的内容在数据库中时存在的

da.Fill(ds); //这儿C#自动生成了一个名为"table"的数据表
GridView1_gktzb.DataSource = ds.Tables[0]; //这儿sd.Tables[0]是错误的,方括号里应该填入数据表名

da.Fill(ds,"mytable");
GridView1_gktzb.DataSource = ds.Tables["mytable"];
这样就可以了.你试试.

GridView1_gktzb.DataSource = ds.Tables[0].DefaultView试试

AutoGenerateColumns="true"
你把gridview加上这个属性试试
表里有数据吗?

你设置断点看一下TextBox1.Text里面的值呢,估计不对吧