程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> ASP.NET連接 Access數據庫的幾種方法

ASP.NET連接 Access數據庫的幾種方法

編輯:ASP.NET基礎

web.config

第一種方法:

<?xml version="1.0" encoding="utf-8"?>
<!--
有關如何配置 ASP.NET 應用程序的詳細消息,請訪問
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="AccessConnString" value="provider=microsoft.jet.oledb.4.0;data source="/>
<add key="AccessDbPath" value="~/App_Data/guestbook.mdb"/>
</appSettings>
<connectionStrings>
<add name="AccessConnectionString" connectionString="Provider=Microsoft.Jet.OleDb.4.0;Data Source=" providerName="System.Data.OleDb"/>
<add name="Access_Path" connectionString="~/App_Data/guestbook.mdb"/>
<add name="SqlConnectionString" connectionString="Data Source=localhost;Initial Catalog=guestbook;User ID=sa;password=123456;" providerName="System.Data.SqlClient"/>
<add name="OracleConnectionString" connectionString="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=xp2008)(PORT=1521)))(CONNECT_DATA=(SID=guandb)(SERVER=DEDICATED)));User ID=system;Password=k2763826;Unicode=True" providerName="System.Data.OracleClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>

第二種方法:

<?xml version="1.0" encoding="utf-8"?>
<!--
有關如何配置 ASP.NET 應用程序的詳細消息,請訪問
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="AccessConnString" value="provider=microsoft.jet.oledb.4.0;data source="/>
<add key="AccessDbPath" value="~/App_Data/guestbook.mdb"/>
</appSettings>
<connectionStrings>
<add name="AccessConnectionString" connectionString="Provider=Microsoft.Jet.Oledb.4.0;data source="/>
<add name="Access_Path" connectionString="~/App_Data/guestbook.mdb"/>
<add name="SqlConnectionString" connectionString="Data Source=localhost;Initial Catalog=HuaRunDb;User ID=sa;password=123456;" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>

後台 aspx.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Configuration;
using System.Data.OleDb;
using System.Data;
namespace TestOracle
{
public partial class TestConnect : System.Web.UI.Page
{
public static readonly string connStr1 = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString+ HttpContext.Current.Server.MapPath(WebConfigurationManager.ConnectionStrings["Access_Path"].ConnectionString);
public static readonly string connStr2 = System.Configuration.ConfigurationManager.AppSettings["AccessConnString"].ToString() + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["AccessDbPath"]) + ";";
public static readonly string connStr3 = "Provider = Microsoft.Jet.OLEDB.4.0 ;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/guestbook.mdb");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection(connStr1);
try
{
conn.Open();
string sql = "select * from GtContent";
OleDbDataAdapter myadapter = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
myadapter.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
this.Label1.Text = "數據庫連接成功!";
}
catch (Exception ee)
{
this.Label1.Text = ee.ToString();
}
}
}
}

以上所述是小編給大家介紹的ASP.NET連接 Access數據庫的幾種方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved