好多人的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写法不一样,具体区别自己查吧
这样程序运行时经常提示诸如以下的错误:
'C:"WINDOWS"system32"~"App_Data"Data.mdb'不是一个有效的路径。 确定路径名称拼写是否正确,以及是否连接到文件存放的服务器。 Data Source=~"App_Data"Data.mdb
就算用绝对路径正确,那么移植程序时还要去修改web.config,所以比较麻烦。
也有在web.config中使用象ASP那样的Server.MapPath取数据库路径的,但web.config不认识Server.MapPath,此方法也行不通。
后来通过摸索、参考其它程序,总结出如下方法,可以方便的C#相对路径写法而不必再去修改ACCESS数据库路径。
我在web.config中的写法如下:
- <appSettings>
- <add key="SQLConnString" value="provider=microsoft.jet.oledb.4.0;data source="/>
- <add key="dbPath" value="~/App_Data/mydata.mdb"/>
- </appSettings>
程序中的数据访问类中我把"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数据库web.config内路径***写法
帖子二web.config 配置access的相对路径
本人使用的是VS 2005,经过调试成功,具体如何实现,代码如下(VB.Net示例,C#方法一样,在这里就不用说了)
首先在web.config文件<appSettings>节点中加入如下代码:
- <!--数据源-->
- <add key="myds" value="Provider=Microsoft.Jet.OLEDB.4.0;Data source="/>
- <!--数据库相对路径-->
- <add key="myconn" value="App_Data"VinikeData.mdb"/>
- 然后,写个类文件,调用上面的定义,代码如下:
- '定义一个连接字符串,难点就在这里,网上很多用到了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: 在此处添加构造函数逻辑
- //
- }
- 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中调用Access数据库语句
- <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>
- 调用数据库类:
- 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;
- //因为要使用Access数据库,所以这里得引入命名空间
- using System.Data.OleDb;
- /// <summary>
- /// 这里主要是留言本会用到的数据库连接类,因为数据操作并不多,所以把常规的操作类也一并放这里了
- /// </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: 在此处添加构造函数逻辑
- //
- }
- public static OleDbConnection con()
- {//数据库连接类
- OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["book"].ConnectionString);
- return con;
- }
- public static bool insert(string que)
- { //根据传进来的SQL语句执行插入/删除/更新等操作
- 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制定留言的数据表,
- 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)
- {//根据传来的条件查询该项是否有内容,有就返回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)
- {//同样是根据传来的SQL语句返回一个字段的值,一般应该把SQL语句做在类中,这里没有放
- OleDbConnection con = odb.con();
- con.Open();
- OleDbCommand cmd = new OleDbCommand(que,con);
- return cmd.ExecuteScalar().ToString();
- }
- public static int num(string mm)
- {//根据要求返回一个一个显示条目数
- return Convert.ToInt32(odb.scr("select ["+mm+"] from [config]"));
- }
- }
帖子四asp.net 做登录界面如何连接access数据库进行验证
- 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)
- {
- //以下产生随机的验证码,并在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 != "")//判断验证码是否未空
- {
- if (this.yanzhen1.Text == this.Label1.Text)//判断验证码是否相等
- {
- 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"));//建立数据库连接
- conn.Open();
- OleDbCommand cmd = new OleDbCommand(sql, conn);
- int state =Convert.ToInt32( cmd.ExecuteScalar());//执行sql语句,并返回获得值
- if (state == 0 || state > 1)//如果数据中没有记录或有多条记录则抱错
- {
- this.Label2.Text = "用户不存在,请检测用户名和密码是否正确!";
- }
- else
- {
- this.Label2.Text = "登入成功!" ;
- }
- conn.Close();
- }
- catch (Exception a)
- {
- Response.Writea.Message);
- }
- }
- else
- {
- this.Label2.Text = "验证码不正确,请重新输入!";
- }
- }
- else
- {
- this.Label2.Text = "验证码没有填写!";
- }
- }
- else
- {
- this.Label2.Text = "密码没有填写!";
- }
- }
- else
- {
- this.Label2.Text = "用户名没有填写!";
- }
- }
- }
【编辑推荐】