欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

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

时间:2020-12-09 20:40:12|栏目:.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();
}
}
}
}

上一篇:C#简单数字图像处理程序

栏    目:.NET代码

下一篇:在ASP.NET 2.0中操作数据之三十九:在DataList的编辑界面里添加验证控件

本文标题:ASP.NET连接 Access数据库的几种方法

本文地址:http://www.codeinn.net/misctech/30963.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有