ADO.NET最佳实践(上)

** 概述: **

本文在微软站点资源的基础上加工整理而成,意在介绍在你的ADO.NET应用程序中执行和完成性能优化、稳定性和功能性方面提供最佳的解决方案;同时也包含在ADO.NET中运用已有的数据对象进行开发的最佳实践和帮助你怎样设计ADO.NET应用程序提供建议。

本文包含以下内容:

1..NET框架中的data providers;

2.对照DataSet和DataReader,分别介绍他们的最佳用途;

3.如何使用DataSet、Commands和Connections;

4.结合XML;

5.如果你是ADO程序员,也不妨看看ADO.NET与ADO的区别和联系;

6.结合一些FAQ,更深一步讨论ADO.NET观点和使用技巧。

** 介绍: **

A ..NET框架中的data providers

Data providers在应用程序和数据库之间扮演一个桥梁的角色,它使得你可以从一个数据库返回查询结果、执行命令以及对数据集的更新等。

B .几种data provider的介绍

下面表格中数据表明各种data provider以及最佳适用数据库对象

提供者

|

描述

---|---

SQL Server.NET Data Provider

|

在 .NET框架中使用System.Data.SqlClient命名空间;

建议在中间层应用程序中使用 SQL Server7.0或以后版本;

建议在独立的应用程序中使用 MSDE或SQL Server7.0或更高版本;

SQL Server6.5或更早版本,必须使用OLE DB.NET Data Provider中的OLE DB Provider For SQL Server。

OLE DB.NET Data Provider

|

在 .NET框架中使用System.Data.OleDb命名空间;

建议在中间层应用程序中使用 SQL Server6.5或以前版本,或者任何在.NET框架SDK中指出的支持OLE DB接口清单的OLE DB Provider,OLE DB接口清单将在后面列出;

建议在独立的应用程序中使用 Access,中间层应用程序不建议使用Access;

不再支持为 ODBC的OLE DB Provider,要访问ODBC,使用ODBC.NET Data Provider。

ODBC.NET Data Provider

|

在 .NET框架中使用System.Data.Odbc命名空间;

提供对使用 ODBC驱动连接的数据库的访问;

.NET Data Provider For Oracle

|

在 .NET框架中使用System.Data.OracleClient命名空间;

提供对 Oracle数据库的访问。

Custom.NET Data Provider

|

提供一套接口,让你可以自定义一个 Data Provider;

SQLXML Managed Classes

|

包含 SQLXML Managed Classes的最新版SQLXML3.0,使得你可以访问SQL Server2000或以后版本的XML功能性扩展,比如执行XML模板文件、执行XPath查询和使用Updategrams或Diffgrams更新数据等;在SQLXML 3.0中存储过程和XML模板将会通过SOAP作为一种WEB服务。

表格中提到的OLE DB接口清单,在这里把它列出

OLE DB 对象

|

接口

---|---

OLE DB Services

|

IdataInitilize

DataSource

|

IDBInitialize
IDBCreateSession
IDBProperties
IPersist
IDBInfo*

Session

|

ISessionProperties
IOpenRowset
IDBSchemaRowset*
ITransactionLocal*
IDBCreateCommand*

Command

|

IcommandText
ICommandProperties
ICommandWithParameters*
IAccessor (only required if ICommandWithParameters is supported)
ICommandPrepare*

MultipleResults

|

ImultipleResults

RowSet

|

Irowset
IAccessor
IColumnsInfo
IColumnsRowset*
IRowsetInfo (only required if DBTYPE_HCHAPTER is supported)

Row

|

IRow*

Error

|

IerrorInfo
IErrorRecords
ISQLErrorInfo*

C .连接SQL Server7.0或更高版本

使用SQL Server.NET Data Provider连接SQL Server7.0或更高版本是最好的方式,在于它建立与SQL Server的直接连接而中间不需要任何的技术层衔接。如下图一展示了各种访问SQL Server7.0或更高版本的技术比较:

图一(连接访问 SQL Server7.0或更高版本的各种技术比较)

以下例子演示怎样创建和打开一个到SQL Server7.0或更高版本数据库的连接:

‘Visual Basic

Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;" & _ "Initial Catalog=northwind")

nwindConn.Open()

‘C#

SqlConnection nwindConn = new SqlConnection("Data Source=localhost; Integrated Security=SSPI;" +

"Initial Catalog=northwind");

nwindConn.Open();

D .连接ODBC数据源

ODBC.NET Data Provider,使用System.Data.Odbc命名空间,拥有为SQL Server和OLE DB的.NET Data Porvider一样的结构,使用ODBC前缀(比如OdbcConnetion)和标准的ODBC连接字符。下面例子演示怎样创建和打开一个到ODBC数据源的连接:

‘Visual Basic

Dim nwindConn As OdbcConnection = New OdbcConnection("Driver={SQL Server};Server=localhost;" & _ "Trusted_Connection=yes;Database=northwind")

nwindConn.Open()

‘C#

OdbcConnection nwindConn = new OdbcConnection("Driver={SQL Server};Server=localhost;" +

"Trusted_Connection=yes;Database=northwind");

nwindConn.Open();

E .使用DataReaders、DataSets、DataAdapters和DataViews

ADO.NET使用DataSet和DataReader对象读取数据并存储。DataSet就好比是数据库的直系亲属,拥有数据库的所有表、顺序和数据库的约束(比如表间关系)。DataReader则从数据库读取快速的、只进的的和只读的数据流。使用DataSet,你将会经常使用DataAdapter(或者CommandBuilder)与你的数据库打交道,同时,你也许会使用DataView去排序和过滤数据,DataSet还允许你可以创建一个继承于DataSet的子对象来表现数据中的表、行和列。下面图二显示DataSet对象模型:

图二( DataSet对象模型)

下面将要介绍在什么时候使用 DataSet或DataReader最恰当,同时也将说明如何使用DataAdapter(包括CommandBuilder)和DataView最优化对数据的访问。

F .DataSet和DataReader的比较

在设计你的应用程序时决定究竟使用DataSet还是使用DataReader,主要看在你的应用程序中要实现的功能性级别。

使用DataSet可以在你的应用程序中做以下事情:

I .在多个离散的结果表之间导航;

一个DataSet可以包含多个结果表,这些结果表是不连续的。你可以分开处理这些表,也可以把这些表当作父子关系进行处理。

II .操作多个数据源(比如从XML文件和电子数据表等不只一个数据库得到的混合数据);

下面的例子演示从SQL Server2000的Northwind数据库读取一个customers表的清单和从Access2000的Northwind数据库读取一个orders表的清单,然后使用DataRelation在两个表之间建立一个对应关系:

‘Visual Basic

Dim custConn As SqlConnection= New SqlConnection("Data Source=localhost;Integrated Security=SSPI;" & _

"Initial Catalog=northwind;")

Dim custDA As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Customers", custConn)

Dim orderConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=c:\Program Files\Microsoft Office" & _ "Office\Samples\northwind.mdb;")

Dim orderDA As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Orders", orderConn)

custConn.Open()

orderConn.Open()

Dim custDS As DataSet = New DataSet()

custDA.Fill(custDS, "Customers")

orderDA.Fill(custDS, "Orders")

custConn.Close()

orderConn.Close()

Dim custOrderRel As DataRelation = custDS.Relations.Add("CustOrders", _ custDS.Tables("Customers").Columns("CustomerID"), _ custDS.Tables("Orders").Columns("CustomerID"))

Dim pRow, cRow As DataRow

For Each pRow In custDS.Tables("Customers").Rows

Console.WriteLine(pRow("CustomerID").ToString())

For Each cRow In pRow.GetChildRows(custOrderRel)

Console.WriteLine(vbTab & cRow("OrderID").ToString())

Next

Next

‘C#

SqlConnection custConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind;");

SqlDataAdapter custDA = new SqlDataAdapter("SELECT * FROM Customers", custConn);

OleDbConnection orderConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=c:\\Program Files\\Microsoft Office\\Office\\Samples\\northwind.mdb;");

OleDbDataAdapter orderDA = new OleDbDataAdapter("SELECT * FROM Orders", orderConn);

custConn.Open();

orderConn.Open();

DataSet custDS = new DataSet();

custDA.Fill(custDS, "Customers");

orderDA.Fill(custDS, "Orders");

custConn.Close();

orderConn.Close();

DataRelation custOrderRel = custDS.Relations.Add("CustOrders", custDS.Tables["Customers"].Columns["CustomerID"], custDS.Tables["Orders"].Columns["CustomerID"]);

foreach (DataRow pRow in custDS.Tables["Customers"].Rows)

{

Console.WriteLine(pRow["CustomerID"]);

foreach (DataRow cRow in pRow.GetChildRows(custOrderRel))

Console.WriteLine("\t" + cRow["OrderID"]);

}

III .层中交换数据或者使用一个XML WEB服务,与DataReader不一样的是DataSet可以被传递给一个远程的客户端;

下面的例子演示如何创建一个XML WEB服务,其中使用GetCustomers取数据库中customers表数据,使用UpdateCustomers更新数据库中数据:

1. ‘Visual Basic

2. ``` @ WebService Language = "VB" Class = "Sample"

 1
 23\.  Imports System 
 3
 44\.  Imports System.Data 
 5
 65\.  Imports System.Data.SqlClient 
 7
 86\.  Imports System.Web.Services 
 9
107\.

<webservice(namespace:="http: ")="" microsoft.com="" webservices=""> _

8. Public Class Sample

9. Public nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")

10. <webmethod( )="" ,="" :="False" description="" enablesession=""> _

11. Public Function GetCustomers() As DataSet

12. Dim custDA As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerID, CompanyName FROM Customers", nwindConn)

13. Dim custDS As DataSet = New DataSet()

14. custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey

15. custDA.Fill(custDS, "Customers")

16. GetCustomers = custDS

17. End Function

18. <webmethod( )="" ,="" :="False" description="" enablesession=""> _

19. Public Function UpdateCustomers(custDS As DataSet) As DataSet

20. Dim custDA As SqlDataAdapter = New SqlDataAdapter()

21. custDA.InsertCommand = New SqlCommand("INSERT INTO Customers (CustomerID, CompanyName) " & _ "Values(@CustomerID, @CompanyName)", nwindConn)

22. custDA.InsertCommand.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")

23. custDA.InsertCommand.Parameters.Add("@CompanyName", SqlDbType.NChar, 15, "CompanyName")

24. custDA.UpdateCommand = New SqlCommand("UPDATE Customers Set CustomerID = @CustomerID, " & _

25. "CompanyName = @CompanyName WHERE CustomerID = @OldCustomerID", nwindConn)

26. custDA.UpdateCommand.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")

27. custDA.UpdateCommand.Parameters.Add("@CompanyName", SqlDbType.NChar, 15, "CompanyName")

28. Dim myParm As SqlParameter = custDA.UpdateCommand.Parameters.Add("@OldCustomerID", SqlDbType.NChar, 5, "CustomerID")

29. myParm.SourceVersion = DataRowVersion.Original

30. custDA.DeleteCommand = New SqlCommand("DELETE FROM Customers WHERE CustomerID = @CustomerID", nwindConn)

31. myParm = custDA.DeleteCommand.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")

32. myParm.SourceVersion = DataRowVersion.Original

33. custDA.Update(custDS, "Customers")

34. UpdateCustomers = custDS

35. End Function

36. End Class

37.

38. ‘C#

39. ``` @ WebService Language = "C#" Class = "Sample"

1
240\.  using System; 
3
441\.  using System.Data; 
5
642\.  using System.Data.SqlClient; 
7
8&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 36pt; TEXT-INDENT:</webmethod(></webmethod(></webservice(namespace:="http:>
Published At
Categories with 数据库类
Tagged with
comments powered by Disqus