1. 程式人生 > >Access資料庫在Web.Config檔案中的配置

Access資料庫在Web.Config檔案中的配置

在開發中用MSSQL 的多,而Access資料庫的少,下面簡單說下Access資料庫的連線設定:

web.config配置


<configuration>
 <appSettings/>
  <connectionStrings>
    <add name="MyJetConn" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|/skdb.mdb; Persist Security Info=False;" providerName="System.Data.OleDb" />
  </connectionStrings>

用於測試連線狀態的程式碼:

 public string OpenDB()
    {
        string msg;
        OleDbConnection MyOleDB = new OleDbConnection();
        MyOleDB.ConnectionString = WebConfigurationManager.ConnectionStrings["MyJetConn"].ConnectionString;
        try
        {
            MyOleDB.Open();
            msg = "server vision:" + MyOleDB.ServerVersion + "connection is " + MyOleDB.State.ToString();
            return (msg);
        }
        catch (Exception err)
        {
            msg = err.Message;
            return (msg);
        }
        finally {
            MyOleDB.Close();
        }
    }