asp.net(c#)连接ACCESS数据库

来源:互联网 发布:全景天窗的优缺点 知乎 编辑:程序博客网 时间:2024/04/30 12:52
<%@ 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>