62,074
社区成员
发帖
与我相关
我的任务
分享
if(!IsPostBack)
{
// sql语句查询信息
String queryString =
"Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]";
//调用GetDate方法(将sql语句作为参数传进去),返回一个DataSet
DataSet ds = GetData(queryString);
//如果ds中存在信息的话
if (ds.Tables.Count > 0)
{
//就把数据给GridView这个控件,就是给这个空间指定数据源
AuthorsGridView.DataSource = ds;
//指定了数据源之后,就是要将信息显示出来的
AuthorsGridView.DataBind();
}
else
{
Message.Text = "Unable to connect to the database.";
}
}
DataSet GetData(String queryString)
{
//这个是webconfig中的配置与数据库连接相等于(server=localhost;uid=sa;pwd=sa;database=数据库名)
String connectionString = ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString;
DataSet ds = new DataSet();
try
{
//与数据库建立连接
SqlConnection connection = new SqlConnection(connectionString);
创建一个暑假适配器(支持增加,删除,查询,修改操作)
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
//此例的目的是查询信息,将查询的信息给ds
// Fill the DataSet.
adapter.Fill(ds);
}
catch(Exception ex)
{
// The connection failed. Display an error message.
Message.Text = "Unable to connect to the database.";
}
return ds;
}
<%@ Page language="C#" %>
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
// This example uses Microsoft SQL Server and connects
// to the Northwind sample database. The data source needs
// to be bound to the GridView control only when the
// page is first loaded. Thereafter, the values are
// stored in view state.
if(!IsPostBack)
{
// Declare the query string.
String queryString =
"Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]";
// Run the query and bind the resulting DataSet
// to the GridView control.
DataSet ds = GetData(queryString);
if (ds.Tables.Count > 0)
{
AuthorsGridView.DataSource = ds;
AuthorsGridView.DataBind();
}
else
{
Message.Text = "Unable to connect to the database.";
}
}
}
DataSet GetData(String queryString)
{
// Retrieve the connection string stored in the Web.config file.
String connectionString = ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString;
DataSet ds = new DataSet();
try
{
// Connect to the database and run the query.
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
// Fill the DataSet.
adapter.Fill(ds);
}
catch(Exception ex)
{
// The connection failed. Display an error message.
Message.Text = "Unable to connect to the database.";
}
return ds;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView DataBind Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView DataBind Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<asp:gridview id="AuthorsGridView"
autogeneratecolumns="true"
runat="server">
</asp:gridview>
</form>
</body>
</html>