日期:2014-05-18  浏览次数:20492 次

求一段简单明了的ASP.NET 连接 ACCESS 数据库的代码
求一段简单明了的ASP.NET 连接 ACCESS 数据库的代码,
需要代码和Web.config的配置信息

------解决方案--------------------
C# code
string connection="Provider=Microsoft.Jet.OLEDB.4.0;Server=MyMDB.mdb"; 
string sqlConnection="SECLET * FROM ***DB"; 

System.Data.OleDb.OleDbConnection oleDbConnection=new System.Data.OleDb.OleDbConnection(connection); 

DataSet dataset=DataSet(); 

System.Data.OleDb.OleDataAdapter oleDataAdapter=new System.Data.OleDb.OleDataAdapter(sqlConnection,oleDbConnection); 

oleDbConnection.Open(); 
oleDataAdapter.Fill(dataSet,"***DB"); 
dataGridView.DataSource=dataSet.Tables["***DB"];

------解决方案--------------------
Public conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & System.Web.HttpContext.Current.Server.MapPath("数据库路经")
dim intcount as int=0
 dim str1 as string = "select count(*) as ID from 数据库表名"
Dim conn As New OleDbConnection(conStr)
Dim cmd As New OleDbCommand(str1, conn)
conn.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader
If dr.Read() Then
intcount = dr("ID")
Else
intcount = 0
End If
dr.Close()
conn.Close()
conn.Dispose()
------解决方案--------------------
C# code
//连接的access
string connection="Provider=Microsoft.Jet.OLEDB.4.0;Server=MyMDB.mdb"; 
//查询的access里面的表
string sqlConnection="SECLET * FROM ***DB"; 
//连接
System.Data.OleDb.OleDbConnection oleDbConnection=new System.Data.OleDb.OleDbConnection(connection); 

DataSet dataset=DataSet(); 

System.Data.OleDb.OleDataAdapter oleDataAdapter=new System.Data.OleDb.OleDataAdapter(sqlConnection,oleDbConnection); 
//打开连接
oleDbConnection.Open(); 
//填充数据到DataSet
oleDataAdapter.Fill(dataSet,"***DB"); 
//在dataGridView显示出查询出来的数据
dataGridView.DataSource=dataSet.Tables["***DB"];

------解决方案--------------------
web.config
<connectionStrings>
<add name="DBPath" connectionString="~/#taohaodb#/#xinfor#.mdb"/>
<!--数据库路径,前面的~必须保留-->
</connectionStrings>
连接类
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.ConnectionStrings["DbPath"].ToString())