【问题标题】:How to retrieve and display values in GridView from SQL Server database in ASP.NET using C#如何使用 C# 从 ASP.NET 中的 SQL Server 数据库中检索和显示 GridView 中的值
【发布时间】:2020-10-14 21:54:02
【问题描述】:

我正在 Microsoft Visual Studio 2010 中创建一个 Web 应用程序。我想从 SQL Server 数据库中检索数据并使用 C# 在 ASP.NET 中的 GridView 中显示它。

我想检索按钮 onclick 事件的数据。

protected void submit_click(object sender, EventArgs e) {

connection1.open();

SqlCommand sq= new SqlCommand("select student_id , student_name 来自学生);

SqlDataReader dr= new SqlDataReader();

...

// 网格视图

}

【问题讨论】:

标签: c# asp.net sql-server visual-studio visual-studio-2010


【解决方案1】:

参考说明:Microsoft Visual Studio-how-to-retrieve-and-display-values-from-database-in -ASP.NET-using-C#.

protected void Button1_Click(object sender, EventArgs e) {

//连接字符串

SqlConnection con = new SqlConnection("Data Source=.\SQLEXPRESS; AttachDbFilename='C:\Users\HP\Documents\Visual Studio 2010\项目 \assignment\assignment\App_Data\pokedex.mdf';综合 安全=真;用户实例=True");

con.Open();

SqlCommand cmd = new SqlCommand("Select * from pokedex;");

SqlDataAdapter sda = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();

sda.Fill(dt);

GridView1.DataSource = dt;

GridView1.DataBind();

con.Close();

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 2013-03-04
    • 1970-01-01
    • 2014-11-09
    • 2021-03-03
    相关资源
    最近更新 更多