sql,access连接数据库,读数据库数据
1.SqlAccessConnection.aspx前台
1

2
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SqlAccessConnection.aspx.cs" Inherits="SqlAccessConnection" %>3

4
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">5

6
<html xmlns="http://www.w3.org/1999/xhtml" >7
<head runat="server">8
<title>无标题页</title>9
</head>10
<body>11
<form id="form1" runat="server">12
<div>13
1、<asp:Button ID="Button1" runat="server" Text="sql连接3个样子的写法,配置文件1,配置文件2" OnClick="Button1_Click"/><br /><br />14
2、<asp:Button ID="Button2" runat="server" Text="sql连接,绑定,DataTable--->gridview" OnClick="Button2_Click"/>15
<asp:GridView ID="gvTest" runat="server"></asp:GridView>16
3、sql,配置文件2,调用ConnectionString(access没有这一项功能)<br />17
18
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"19
AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Height="100px">20
<Columns>21
<asp:BoundField DataField="name" HeaderText="姓名" SortExpression="name" />22
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age" />23
<asp:BoundField DataField="sex" HeaderText="性别" SortExpression="sex" />24
</Columns>25
</asp:GridView>26
27
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 28
ConnectionString="<%$ ConnectionStrings:SqlConnectionStrings %>" 29
SelectCommand="SELECT * FROM [students]">30
</asp:SqlDataSource>31
4、access,配置文件1,配置文件2,AccessAppSettings<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Button" /><br />32
<br />33
34
35
36
5、access用vs配置数据源,自定义sql37
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" AllowSorting="True"38
AutoGenerateColumns="False" DataSourceID="AccessDataSource1" Height="100px">39
<Columns>40
<asp:BoundField DataField="name" HeaderText="姓名" SortExpression="name" />41
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age" />42
<asp:BoundField DataField="sex" HeaderText="性别" SortExpression="sex" />43
</Columns>44
</asp:GridView>45
<asp:AccessDataSource ID="AccessDataSource1" runat="server" 46
DataFile="~/App_Data/test.mdb" 47
SelectCommand="select * from student" > 48
</asp:AccessDataSource>49
50
6、access用vs配置数据源,指定来自表(原来和4一样的)51
<asp:GridView ID="GridView3" runat="server" AllowPaging="True" AllowSorting="True"52
AutoGenerateColumns="False" DataSourceID="AccessDataSource1" Height="100px">53
<Columns>54
<asp:BoundField DataField="name" HeaderText="姓名" SortExpression="name" />55
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age" />56
<asp:BoundField DataField="sex" HeaderText="性别" SortExpression="sex" />57
</Columns>58
</asp:GridView>59
<asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="~/App_Data/test.mdb"60
SelectCommand="SELECT * FROM [student]"></asp:AccessDataSource>61
62
</div>63
</form>64
</body>65
</html>66

67

2.后台:
![]()
Code
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.Data.SqlClient;
12
using System.Data.OleDb;
13
14
public partial class SqlAccessConnection : System.Web.UI.Page
15

{
16
protected string CONN_STRING_NON_DTC = System.Web.Configuration.WebConfigurationManager.AppSettings["SqlAppSettings"];
17
18
19
protected void Page_Load(object sender, EventArgs e)
20
{ }
21
protected void Button1_Click(object sender, EventArgs e)
22
{
23
//连接字符串3个样子
24
string str1="server=(local);Database=test;Integrated Security=true;";//连接字符串1
25
string str2="Data Source=(local);Initial Catalog=test;Integrated Security=True;User Id=sa;Password=sa"; //连接字符串2
26
string str3 = System.Web.Configuration.WebConfigurationManager.AppSettings["SqlAppSettings"]; //使用Web.Config文件中的
27
string str4 = ConnectionString;
28
SqlConnection conn = new SqlConnection(ConnectionString);//-------str1,str2,str3,str4,都可以
29
conn.Open();
30
31
//cmd两种写法
32
33
//SqlCommand cmd = new SqlCommand();
34
//cmd.CommandText = "Select top 3 * from students";
35
//cmd.Connection = conn;
36
SqlCommand cmd = new SqlCommand("Select * from students", conn);
37
38
SqlDataReader reader = cmd.ExecuteReader();
39
while (reader.Read())
40
{
41
Response.Write(reader["name"] + " ");
42
}
43
conn.Close();
44
}
45
46
protected void Button2_Click(object sender, EventArgs e)
47
{
48
SqlConnection conn = new SqlConnection();
49
conn.ConnectionString = "Server=(local);Database=test;Integrated Security=true;";
50
SqlCommand cmd = new SqlCommand("Select top 3 * from students", conn);
51
SqlDataAdapter adapter = new SqlDataAdapter();
52
adapter.SelectCommand = cmd; //SqlDataAdapter adapter = new SqlDataAdapter(cmd);
53
54
conn.Open();
55
DataTable table = new DataTable();
56
DataSet dsDataSet = new DataSet();
57
adapter.Fill(dsDataSet, "table");
58
conn.Close();
59
table = dsDataSet.Tables["table"];
60
this.gvTest.DataSource = table.DefaultView;
61
this.gvTest.DataBind();
62
}
63
64
protected void Button3_Click(object sender, EventArgs e)
65
{
66
//两种方式都可以
67
string CONN_STRING_NON_DTC_access = System.Web.Configuration.WebConfigurationManager.AppSettings["AccessAppSettings"];//数据库连接字定义
68
string ConnectionString_access = ConfigurationManager.ConnectionStrings["AccessConnectionStrings"].ConnectionString;
69
70
OleDbConnection objConnection = null; // 连接对象
71
OleDbCommand selectCommand = null; //OleDbCommand对象
72
OleDbDataReader selectReader = null; // OleDbDataReader对象
73
string selectSqlString1 = null; //声明查询语句
74
selectSqlString1 = "select * from student";//定义查询语句
75
76
77
78
//两种方式都可以
79
objConnection = new OleDbConnection(ConnectionString_access); //建立数据链接对象objConnection
80
//objConnection = new OleDbConnection(CONN_STRING_NON_DTC_access); //建立数据链接对象objConnection
81
82
83
selectCommand = new OleDbCommand(selectSqlString1, objConnection);//创建OleDbCommand 对象并保存sql语句
84
objConnection.Open();//打开数据连接
85
selectReader = selectCommand.ExecuteReader();//创建OleDbDataReader对象并执行sql语句;
86
while(selectReader.Read()) //读取查询的结果并且显示在页面上
87
{
88
Response.Write(selectReader["name"].ToString());
89
}
90
objConnection.Close();
91
}
92
}
3.配置文件:
1
<?xml version="1.0"?>2
<configuration>3
<connectionStrings>4
<add name="SqlConnectionStrings" connectionString="Data Source=(local);Initial Catalog=test;Integrated Security=True" providerName="System.Data.SqlClient"/>5
<add name="AccessConnectionStrings" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\test.mdb;Persist Security Info=True" providerName="System.Data.OleDb"/>6
</connectionStrings>7
<location>8
<appSettings>9
<add key="SqlAppSettings" value="Data Source=(local);Initial Catalog=test;Integrated Security=True;User Id=sa;Password=sa"/>10
<add key="AccessAppSettings" value="provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|\test.mdb;"/>11
</appSettings>12
</location>13
<system.web>14
<compilation debug="true"/>15
</system.web>16
</configuration>
浙公网安备 33010602011771号