<html>
<head>
<title>Begin Asp.net Databases using C#</title>
<head>
<body>
<h4>First Example:Listing data from the Employee table</h4>
<asp:DataGrid id="dgNameList"
runat="server"
GridLines="None"
BackColor="LightBlue"
CellPadding="5"
BorderWidth="2"
ToolTip="Includes only those employees who are at HQ"/>
<body>
<html>
<script language="c#" runat="server">
private void Page_Load(object sender,System.EventArgs e)
{
String strConnection="server=(local)\\NetSDK,database=Northwind;
integrated security=true;";
SqlConnection objConnection=new SqlConnection(strConnection);
String strSQL="SELECT FirstName,LastName,Country"+"FROM Employees";
SqlCommand objCommand=new SqlCommand(strSQL,objConnection);
objConnection.Open();
dgNameList.DataSource=objCommand.ExecuteReader();
daNameList.DataBind();
objConnection.Close();
}
</script>
2 Access是个很大的数据库使用广泛但是它并不支持10或20个以上的用户,大于时性能降低。JET是它的数据引擎。处理OLEDB.NET数据连接时,至少包含两部分:
要使用的提供者的类型和Access文件的名称,强调:在连接Access数据库时必须指定存储数据的文件,若想连接到不同的Access数据库上必须创建一个新的Connection对象,而其它企业级的数据库就可以在一个连接中切换不同的数据库(如SQL ORACLE DB2)
找到northwind.mdb 复制到C:\aaa\datastores
创建Access_connection.aspx
<html>
<head>
<title>Connecting to access Databases using C#</title>
<head>
<body>
<h3>Connecting to access Databases</h3>
<asp:DataGrid id="dgSuppliers" runat="server"/>
<body>
<html>
<script language="c#" runat="server">
private void Page_Load(object sender,System.EventArgs e)
{
String strConnection="Provider=Microsoft,Jet.OleDb.4.0;
data source=C:\\aaa\\datastores\\northwind.mdb;";
OleDbConnection objConnection=new OleDbConnection(strConnection);
String strSQL="SELECT SupplierID,CompanyName FROM Suppliers";
OleDbCommand objCommand=new OleDbCommand(strSQL,objConnection);
objConnection.Open();
dgNameList.DataSource=objCommand.ExecuteReader();
daNameList.DataBind();
objConnection.Close();
}
</script>
3 使用Excel
ADO.NET可以很容易连接并使用电子表格数据,因为excel不是数据库,处理excel数据时必须注意下面内容:
a 工作表清楚设法除去单元格之间的注释,以及行和列中的间隔
b 连接电子表格时必须连接到excel的指定区域。不可以使用excel单元定址语法,例如A1: C3
c 必须使用JET提供者
d SQL语句把excel行看作记录 列看作字段
如下例:
创建C:\aaa\datastores\inventory.xls
| |
A |
B |
C |
D |
| 1 |
itemNo |
Description |
Source |
Note |
| 2 |
1001 |
CPU |
Dell |
P4 |
| 3 |
1002 |
Monitor |
NEC |
17 inch |
| 4 |
1003 |
KeyBoard |
MicroTex |
PS2 plug |
将A1:D4命名为Items 检查在选择A1:D4时,Items是否出现在公式栏左下边的框中,关闭工作簿不用关闭excel
创建Excel_connection.aspx 添加代码
<html>
<head>
<title>Reading from an excel workbook using C#</title>
<head>
<body>
<h3>Reading from an excel workbook</h3>
<asp:DataGrid id="dgInventory" runat="server"/>
<body>
<html>
<script language="c#" runat="server">
private void Page_Load(object sender,System.EventArgs e)
{
String strConnection="Provider=Microsoft,Jet.OleDb.4.0;
data source=C:\\aaa\\datastores\\Inventory.xls;
Extended Properties=Excel 8.0;";
OleDbConnection objConnection=new OleDbConnection(strConnection);
String strSQL="SELECT * FROM Items WHERE ItemNo=1002";
OleDbCommand objCommand=new OleDbCommand(strSQL,objConnection);
objConnection.Open();
dgNameList.DataSource=objCommand.ExecuteReader();
daNameList.DataBind();
objConnection.Close();
}
</script>