欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET连接 Access数据库的几种方法

程序员文章站 2023-12-09 23:21:51
web.config 第一种方法: <...

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数据库的几种方法,希望对大家有所帮助