asp.net 串連 Access 的幾種方法

來源:互聯網
上載者:User

 

web.config

1

 

<?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>

 

 

2

 

<?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();
            }

        }
    }
}

 

 

connStr1 ,connStr2 ,connStr3

用這三個連接字串哪 個都可以。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.