[学习]ASP.NET连接ACCESS数据库web.config内路径最优秀写法

 

 

众爱人被ACCESS数据库在.net程序中相对路径的题材找麻烦,搞得每次活动程序都使错过修改web.config
遇数据库连接字符串的数据库路径。

有的是丁的web.config中的写法如下:

<appSettings>
<add key=”OLEDBCONNECTIONSTRING”
value=”Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=E:\web\App_Data\Data.mdb)”></add>
</appSettings>

先后中如此描绘:

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,此方式也没用。

新生通过查找、参考其他程序,总结出如下方法,可以方便的移植程序路径而不要再夺修改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 name=”book”
connectionString=”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 = “用户称从未填写!”;
}
}
}

 

 

Source:http://www.cnblogs.com/sxjrcool/archive/2008/08/16/1269462.html

相关文章