怎么連接access數(shù)據(jù)庫?
你好!
步驟如下:
1。在web.config中添加鏈接字符串
例如:
//數(shù)據(jù)庫名DB_GD
2。在C#代碼中獲取連接字符串
例如:
stringConnectString=System.Configuration.ConfigurationManager.AppSettings["dns"].ToString()+System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbpath"]+";");
3。使用連接字符串操作數(shù)據(jù)庫
例如:publicstaticintExecuteSql(stringSQLString)
{
using(OleDbConnectioncn=newOleDbConnection(ConnectString))
{
using(OleDbCommandcm=newOleDbCommand(SQLString,cn))
{
try
{
cn.Open();
introws=cm.ExecuteNonQuery();
returnrows;
}
catch(System.Data.OleDb.OleDbExceptione)
{
throwe;
}
finally
{
cm.Dispose();
cn.Close();
}
}
}
}