2007資料庫
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.accdb");
2003資料庫
con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"資料庫.mdb\";Jet OLEDB:Database Password=aking");
********************
private static void openConnection()
{
if (conn.State == ConnectionState.Closed )
{
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.ConnectionStrings["DbPath"].ToString());
comm.Connection = conn;
try
{
conn.Open();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}
<connectionStrings>
<add name="DBPath" connectionString="~/App_Data/Web.mdb"/>
<!--資料庫路徑,前面的~必須保留-->
</connectionStrings>
改為ACCESS後怎麼寫呢?
<connectionStrings>
<add name="DBPath" connectionString="~/App_Data/Web.mdb"/>
<!--資料庫路徑,前面的~必須保留-->
</connectionStrings>
private static void openConnection()
{
if (conn.State == ConnectionState.Closed )
{
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.ConnectionStrings["DbPath"].ToString());
comm.Connection = conn;
try
{
conn.Open();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}
這兩個 加一塊 就行了,前面 那個 在web.config中寫
<appSettings>
<add key="OLEDBCONNECTIONSTRING" value="provider=microsoft.jet.oledb.4.0;data source="/>
<add key="dbPath" value="~/App_Data/Data.mdb"/>
</appSettings>
***********************************************
public static OleDbConnection CreateDB()
{
return new OleDbConnection(ConfigurationManager.AppSettings["OLEDBCONNECTIONSTRING"].ToString() + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbPath"]));
}