使用OleDBCommand相关操作类需要引入System.Data.OleDb命名空间。
通过连接一个带密码的access数据库 读取其中的数据并置入表格中显示,验证连接和命令使用正确性。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
publicpartialclass _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//数据库连接,记录集的获取
//注意更改路径
String sqlconn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = E:/pro/web/access_test/app_data/Northwind.mdb;Jet OLEDB:Database Password=111111";
OleDbConnection myConnection = new OleDbConnection(sqlconn);
myConnection.Open();
OleDbCommand myCommand = new OleDbCommand("select * from 运货商", myConnection);
OleDbDataReader myReader;
myReader = myCommand.ExecuteReader();
//读取记录集
Response.Write("<table border=1 cellspacing=0 cellpadding=2>");
Response.Write("<tr bgcolor=#DAB4B4>");
for(inti = 0; i < myReader.FieldCount; i++)
Response.Write("<td>"+ myReader.GetName(i) +"</td>");
Response.Write("</tr>");
while (myReader.Read())
{
Response.Write("<tr>");
for(inti = 0; i < myReader.FieldCount; i++)
Response.Write("<td>"+ myReader[i].ToString ()+"</td>");
Response.Write("</tr>");
}
Response.Write("</table>");
//关闭记录集和连接
myReader.Close();
myConnection.Close();
}
}
|