<%@ Page Language="c#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
OleDbConnection objConnection;
OleDbDataAdapter objCommand;
string strConnect;
string strCommand;
DataSet DataSet1 = new DataSet();
strConnect = @"Provider=Microsoft.Jet.OLEDB.4.0;";
//If you don't have the grocertogo.mdb database, then you will have to
//change the following three lines to a different database
strConnect += @"Data Source=D:/aspx/db";
strConnect += @"/grocertogo.mdb;";
strConnect += "Persist Security Info=False";
strCommand = "SELECT ProductName, UnitPrice FROM products";
objConnection = new OleDbConnection(strConnect);
objCommand = new OleDbDataAdapter(strCommand, objConnection);
objCommand.Fill(DataSet1, "products");
DataGrid1.DataSource=DataSet1.Tables["Products"].DefaultView;
DataGrid1.DataBind();
}
</script>
<html>
<head>
<title>Data Grid Control example</title>
</head>
<body>
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</body>
</html>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
OleDbConnection objConnection;
OleDbDataAdapter objCommand;
string strConnect;
string strCommand;
DataSet DataSet1 = new DataSet();
strConnect = @"Provider=Microsoft.Jet.OLEDB.4.0;";
//If you don't have the grocertogo.mdb database, then you will have to
//change the following three lines to a different database
strConnect += @"Data Source=D:/aspx/db";
strConnect += @"/grocertogo.mdb;";
strConnect += "Persist Security Info=False";
strCommand = "SELECT ProductName, UnitPrice FROM products";
objConnection = new OleDbConnection(strConnect);
objCommand = new OleDbDataAdapter(strCommand, objConnection);
objCommand.Fill(DataSet1, "products");
DataGrid1.DataSource=DataSet1.Tables["Products"].DefaultView;
DataGrid1.DataBind();
}
</script>
<html>
<head>
<title>Data Grid Control example</title>
</head>
<body>
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</body>
</html>
C#连接Access数据库并绑定DataGrid示例
此博客展示了使用C#语言连接Access数据库的代码示例。通过OleDbConnection和OleDbDataAdapter,从数据库中查询数据并填充到DataSet,最后将数据绑定到ASP.NET的DataGrid控件上,实现数据的展示。
792

被折叠的 条评论
为什么被折叠?



