ASP.NET访问Access的连接字符串配置

由于Access是文件数据库,所以在ASP.NET需要能映射访问到Access文件:

方式一:appSettings中设置连接字符串

web.config的配置

<appSettings>
  < add key = " provider " value = " Provider=Microsoft.Jet.OLEDB.4.0;data source= " />
  < add key = " dbPath " value = " ~/db\data.mdb " /> // 注意必须加~/,代表根目录
</appSettings>
 

 

通过Server.Map()拼接数据库连接字符串

  /// <summary>
/// 连接字符串
/// </summary>
private String connectionString =
     System.Configuration.ConfigurationManager.AppSettings[ " provider " ].ToString() +
     System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings[ " dbPath " ]) + " ; " ;

 

方式二:SqlDataSource连接access

web.config的配置

< connectionStrings >
   
< add name = " WebSiteConnectionString " connectionString = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|aisino_data.mdb " providerName = " System.Data.OleDb " />    // 注意:DataDirectory是不可以更改的否则会出现"文件名无效错误"! 这就说明它不能被修改,你的数据库必须放在App_Data中

 

 这样,通过ConfigurationManager.ConnectionStrings["WebSiteConnectionString"].ConnectionString访问即可。

你可能感兴趣的:(asp.net)