asp.net 连接access数据库如何写 125
以下是连接代码,高手帮忙指点.------------------------------------------------publicclassDB{publicDB...
以下是连接代码,高手帮忙指点.
------------------------------------------------
public class DB
{
public DB()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static string ConnectionString
{
//get { return "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "App_Data\\db.mdb;Provider=Microsoft.Jet.OLEDB.4.0"; }
// get { return ConfigurationManager.AppSettings["connString"]; }//
get { return ConfigurationManager.ConnectionStrings[1].ConnectionString; }
}
非常感谢大家的热心回答,发给我的解决方法和我的代码好象不一样,我对asp.net懂一点点,望谅解,如果有懂的朋友,留个邮箱,我把连接代码发到邮箱中,在这里,无法贴全部代码. 展开
------------------------------------------------
public class DB
{
public DB()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static string ConnectionString
{
//get { return "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "App_Data\\db.mdb;Provider=Microsoft.Jet.OLEDB.4.0"; }
// get { return ConfigurationManager.AppSettings["connString"]; }//
get { return ConfigurationManager.ConnectionStrings[1].ConnectionString; }
}
非常感谢大家的热心回答,发给我的解决方法和我的代码好象不一样,我对asp.net懂一点点,望谅解,如果有懂的朋友,留个邮箱,我把连接代码发到邮箱中,在这里,无法贴全部代码. 展开
5个回答
展开全部
把ACCESS做好,设置好密码,再改一下名字后缀为bat或者cab,不要以mdb形式存储,批处理一下或者加个壳,就很小了,不占用程序资源空间,把ACCESS批处理文件放在exe文件启动目录下,或者是ASP程序default.aspx相同目录下作为相对路径,在程序开始引用 using System.Data.OleDb; 代码如下:
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "/DB.cab;Jet OLEDB:DataBase Password=密码";
cn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter("Select * from 表名称", cn);
oda.Fill(ds);
cn.Close();
基本上我觉得ASP用相对路径是无关紧要的,应用程序一定要用相对路径,制作安装包要给资源文件加壳
展开全部
<?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>
以上为webconfig
以下为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 应用程序的详细消息,请访问
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>
以上为webconfig
以下为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();
}
}
}
}
追问
无法贴出全部代码,你有E-mail吗?我可以把连接代码发给你.
如何是copy过来的答案就不需要了.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
webconfig
<connectionStrings>
<add name="conn"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=D:\xx.mdb" providerName="System.Data.OleDb" />
</connectionStrings>
链接时
string connStr = ConfigurationManager.ConnectionStrings["conn"].ToString();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public string myConnstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+HttpContext.Current.Server.MapPath("data.mdb");
OleDbConnection MyConnection;
MyConnection = new OleDbConnection(myConnstring);
strInsert="";
OleDbCommand MyCommand = new OleDbCommand(strInsert,MyConnection);
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
OleDbConnection MyConnection;
MyConnection = new OleDbConnection(myConnstring);
strInsert="";
OleDbCommand MyCommand = new OleDbCommand(strInsert,MyConnection);
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询