您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“C#用相對路徑寫法連接Access數(shù)據(jù)庫”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
好多人的C#相對路徑寫法web.config中的寫法如下:
<appSettings> <add key="OLEDBCONNECTIONSTRING" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:"web"App_Data"Data.mdb)"></add> </appSettings>
C#相對路徑寫法這樣寫:
MyConn = new OleDbConnection(System.Configuration.ConfigurationManager.AppSettings["OLEDBCONNECTIONSTRING"]); // 注釋一下:VS2005和VS2003中的ConfigurationSettings寫法不一樣,具體區(qū)別自己查吧
這樣程序運(yùn)行時經(jīng)常提示諸如以下的錯誤:
'C:"WINDOWS"system32"~"App_Data"Data.mdb'不是一個有效的路徑。 確定路徑名稱拼寫是否正確,以及是否連接到文件存放的服務(wù)器。 Data Source=~"App_Data"Data.mdb
就算用絕對路徑正確,那么移植程序時還要去修改web.config,所以比較麻煩。
也有在web.config中使用象ASP那樣的Server.MapPath取數(shù)據(jù)庫路徑的,但web.config不認(rèn)識Server.MapPath,此方法也行不通。
后來通過摸索、參考其它程序,總結(jié)出如下方法,可以方便的C#相對路徑寫法而不必再去修改ACCESS數(shù)據(jù)庫路徑。
我在web.config中的寫法如下:
<appSettings> <add key="SQLConnString" value="provider=microsoft.jet.oledb.4.0;data source="/> <add key="dbPath" value="~/App_Data/mydata.mdb"/> </appSettings>
程序中的數(shù)據(jù)訪問類中我把"SQLConnString"和"dbPath"取出來連接成一個字符串"CONN_STRING_NON_DTC"
public static readonly string CONN_STRING_NON_DTC = System.Configuration.ConfigurationManager.AppSettings["SQLConnString"].ToString() + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbPath"]) + ";";
End of《ASP.NET連接ACCESS數(shù)據(jù)庫web.config內(nèi)路徑***寫法
帖子二web.config 配置access的相對路徑
本人使用的是VS 2005,經(jīng)過調(diào)試成功,具體如何實(shí)現(xiàn),代碼如下(VB.Net示例,C#方法一樣,在這里就不用說了)
首先在web.config文件<appSettings>節(jié)點(diǎn)中加入如下代碼:
<!--數(shù)據(jù)源--> <add key="myds" value="Provider=Microsoft.Jet.OLEDB.4.0;Data source="/> <!--數(shù)據(jù)庫相對路徑--> <add key="myconn" value="App_Data"VinikeData.mdb"/> 然后,寫個類文件,調(diào)用上面的定義,代碼如下: '定義一個連接字符串,難點(diǎn)就在這里,網(wǎng)上很多用到了Server.MapPath,但是還是不行,注意這里用到了Request.MapPath(相對asp來說,這是asp所沒用的) Public connstr As String = ConfigurationSettings.AppSettings("myds") + HttpContext.Current.Request.MapPath("~") + (ConfigurationSettings.AppSettings("myconn").Trim()) using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; /// <summary> /// Connection 的摘要說明 /// </summary> public class Connection { public Connection() { // // TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } 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"]; } } using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; public partial class test_sss_Defauldddt : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { System.Data.OleDb.OleDbConnection conn1 = Connection.connAccess(); conn1.Open(); if (conn1.State == ConnectionState.Open) { Label1.Text = "連接成功!"; } else { Label1.Text = "連接不成功!"; } } }
帖子三c#2.0中web.config中調(diào)用Access數(shù)據(jù)庫語句
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <appSettings/> <connectionStrings> <add cconnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:"c#"book"App_Data"book.mdb" providerName="System.Data.OleDb" /> </connectionStrings> 調(diào)用數(shù)據(jù)庫類: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; //因?yàn)橐褂肁ccess數(shù)據(jù)庫,所以這里得引入命名空間 using System.Data.OleDb; /// <summary> /// 這里主要是留言本會用到的數(shù)據(jù)庫連接類,因?yàn)閿?shù)據(jù)操作并不多,所以把常規(guī)的操作類也一并放這里了 /// </summary> public class odb { public string name; public string email; public string qq; public string msn; public string url; public string title; public string concent; public string face; public string ip; public string pwd; public string uid; public string pwda; public DateTime dtt; public odb() { // // TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } public static OleDbConnection con() {//數(shù)據(jù)庫連接類 OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["book"].ConnectionString); return con; } public static bool insert(string que) { //根據(jù)傳進(jìn)來的SQL語句執(zhí)行插入/刪除/更新等操作 OleDbConnection con = odb.con(); con.Open(); OleDbCommand cmd = new OleDbCommand(que,con); int count = Convert.ToInt32(cmd.ExecuteNonQuery()); if (count > 0) return true; else return false; } public static DataTable ds(string que) {//返回一個裝載了SQL制定留言的數(shù)據(jù)表, OleDbConnection con = odb.con(); OleDbDataAdapter oda = new OleDbDataAdapter(); oda.SelectCommand=new OleDbCommand(que,con); DataSet ds = new DataSet(); oda.Fill(ds,"thc"); return ds.Tables["thc"]; } public static bool img(string que) {//根據(jù)傳來的條件查詢該項(xiàng)是否有內(nèi)容,有就返回true OleDbConnection con = odb.con(); con.Open(); OleDbCommand cmd = new OleDbCommand(que,con); if (cmd.ExecuteScalar().ToString() != "") return true; else return false; con.Close(); } public static string scr(string que) {//同樣是根據(jù)傳來的SQL語句返回一個字段的值,一般應(yīng)該把SQL語句做在類中,這里沒有放 OleDbConnection con = odb.con(); con.Open(); OleDbCommand cmd = new OleDbCommand(que,con); return cmd.ExecuteScalar().ToString(); } public static int num(string mm) {//根據(jù)要求返回一個一個顯示條目數(shù) return Convert.ToInt32(odb.scr("select ["+mm+"] from [config]")); } }
帖子四asp.net 做登錄界面如何連接access數(shù)據(jù)庫進(jìn)行驗(yàn)證
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; public partial class login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //以下產(chǎn)生隨機(jī)的驗(yàn)證碼,并在label1顯示 Random ro = new Random(); if (!IsPostBack) { this.Label1.Text = ro.Next(1000, 9999).ToString(); } } protected void Button1_Click(object sender, EventArgs e) { if (this.name.Text != "")//判斷用戶名是否未空 { if (this.pwd.Text != "")//判斷密碼是否未空 { if (this.yanzhen1.Text != "")//判斷驗(yàn)證碼是否未空 { if (this.yanzhen1.Text == this.Label1.Text)//判斷驗(yàn)證碼是否相等 { string sql; sql = "select count(*) from userinfo where username='" + this.name.Text + "' and pwd='" + this.pwd.Text + "'";//建立sql查詢語句 try { OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("./app_data /db.mdb"));//建立數(shù)據(jù)庫連接 conn.Open(); OleDbCommand cmd = new OleDbCommand(sql, conn); int state =Convert.ToInt32( cmd.ExecuteScalar());//執(zhí)行sql語句,并返回獲得值 if (state == 0 || state > 1)//如果數(shù)據(jù)中沒有記錄或有多條記錄則抱錯 { this.Label2.Text = "用戶不存在,請檢測用戶名和密碼是否正確!"; } else { this.Label2.Text = "登入成功!" ; } conn.Close(); } catch (Exception a) { Response.Writea.Message); } } else { this.Label2.Text = "驗(yàn)證碼不正確,請重新輸入!"; } } else { this.Label2.Text = "驗(yàn)證碼沒有填寫!"; } } else { this.Label2.Text = "密碼沒有填寫!"; } } else { this.Label2.Text = "用戶名沒有填寫!"; } } }
“C#用相對路徑寫法連接Access數(shù)據(jù)庫”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。