net2.0连接任何数据库,一般都是在web.config文件中设置好连接字符串,之后在程序中进行调用。
一、web.config的连接串的写法
<appSettings>
<add key="AccessConnString" value="provider=microsoft.jet.oledb.4.0;data source="E:/MyDatabase/myaccessdata.mdb;Persist Security Info=False"/>
</appSettings>
二、在程序中调用net2.0连接access代码的字符串
OleDbConnection objConnection;
OleDbDataAdapter objCommand;
string strConnect;
string strCommand;
DataSet DataSet1 = new DataSet();
strCommand = "SELECT ProductName, UnitPrice FROM MyTable";
objConnection = new OleDbConnection(System.Configuration.ConfigurationManager.AppSettings["AccessConnString"].ToString());
objCommand = new OleDbDataAdapter(strCommand, objConnection);
objCommand.Fill(DataSet1, "MyTable");
DataGrid1.DataSource=DataSet1.Tables["Table1"].DefaultView;
DataGrid1.DataBind();