很少使用Acess数据库,突然要用,一时连字符串怎么写都忘了,查了一些资料,是这样写的:
PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=C:\MobileDB.mdb,比较简单,但是怎样放到web.config
中,以方便配置呢?
在web.config中加入:
<appSettings>
<add key="OleDB" value="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source="/>
<!--ACESS数据库连接字符串-->
<add key="DBName" value="MobileDB.mdb"/>
<!--ACESS数据库文件名-->
</appSettings>
然后在C#代码中可按如下代码创建连接:
string conStr = ConfigurationManager.AppSettings.Get("OleDB")+Server.MapPath(ConfigurationManager.AppSettings.Get("DBName"));
OleDbConnection con = new OleDbConnection(conStr);