在使用C#连接mdb数据库或accdb数据库时,程序需要
1、添加加两个数据库操作引用
using System.Data;
using System.Data.OleDb;
2、C#连接mdb数据库或accdb数据库连接代码
public OleDbConnection getConn()
{
string connstr = “”;
//连接mdb数据库代码
connstr = “Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=” + Server.MapPath(“.”) + “\\App_Data\\DataBase1.mdb”;
//连接accdb数据库代码
connstr = “Provider = Microsoft.ACE.OLEDB.12.0;;Data Source=” + Server.MapPath(“.”) + “\\App_Data\\DataBase1.accdb”;
OleDbConnection tempconn = new OleDbConnection(connstr);
return (tempconn);
}
3、使用方法
OleDbConnection conn = getConn();
string nowtime = DateTime.Now.ToString(“yyyy-MM-dd hh:mm:ss”);
string strCom = “S elect * from table where uid = 1”;
OleDbCommand myCommand = new OleDbCommand(strCom, conn);
conn.Open();
OleDbDataReader reader;
reader = myCommand.ExecuteReader(); //执行command并得到相应的DataReader
//下面把得到的值赋给tempnote对象
if (reader.Read())
{
userid = reader[“id”].ToString();
}