大众计算机学习网

专门提供各种电脑教程

net2.0连接access代码

Admin | 2011-10-30 5:26:22 | 被阅次数 | 4210

  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();