Web.config and Access config
web.config :
<configuration>
<appSettings>
<!--連接ACCESS数据库-->
<add key="ConnStr" value="Provider=Microsoft.jet.OleDB.4.0;data Source="/>
<add key="DbPath" value="./App_Data/database_file.mdb"/>
</appSettings>
<connectionStrings/>
class:
public static OleDbConnection connAccess()
{
OleDbConnection conn = new OleDbConnection(GetConnString ());
return conn;
}
private static string GetConnString()
{
return System.Configuration.ConfigurationSettings.AppSettings["ConnStr"] + System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["DbPath"]);
}
page file:
protected void Button1_Click(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection conn1 = Conn.connAccess();
conn1.Open();
if (conn1.State == ConnectionState.Open )
{
Label1.Text = "连接成功!";
}
else
{
Label1.Text = "连接不成功!";
}
}
//end
如果您的ACCESS数据文件有密码,请将Web.config改成下面的样式:
<configuration>
<appSettings>
<!--連接ACCESS数据库-->
<add key="ConnStr" value="Provider=Microsoft.jet.OleDB.4.0;Jet OLEDB:database Password=*****;data Source="/>
<add key="DbPath" value="./App_Data/database_file.mdb"/> </appSettings>
<connectionStrings/>