asp.net sqlserver 使用sqlDataSet将表tb_stu的内容绑定到GridView1上显示,写出Buton1的点击事件
时间: 2023-11-22 11:52:08 浏览: 50
下面是一个简单的ASP.NET和SQL Server代码示例,将表tb_stu的内容绑定到GridView1上显示:
```
protected void Button1_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword";
string query = "SELECT * FROM tb_stu";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "tb_stu");
GridView1.DataSource = dataSet.Tables["tb_stu"];
GridView1.DataBind();
}
}
```
在这个示例中,我们首先定义了一个连接字符串,该字符串包含连接到SQL Server所需的信息。然后,我们定义了一个查询字符串,该字符串包含要检索的数据。接下来,我们使用SqlDataAdapter类创建一个适配器对象,并使用Fill方法将数据填充到DataSet对象中。最后,我们将DataSet对象中的数据绑定到GridView1控件上,并使用DataBind方法刷新GridView1。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)