分享
 
 
 

The System.Data Namespace

王朝asp·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

The System.Data Namespace

This book will take advantage of three namespaces that allow you to work

with data within the application in this book: System.Data,

System.Data.OleDb, and System.Data.SqlClient. (Microsoft and other vendors

will add support for additional data namespaces in the future.) The

System.Data namespace contains classes that manage data within memory.

System.Data.OleDb and System.Data.SqlClient provide classes that can

connect to data sources, read and write data, and fill System.Data objects

with data.

The SqlClient and OleDb Namespaces

Microsoft has provided two namespaces that we'll use for managing data

throughout this book. The System.Data.OleDb namespace allows you to work

with data using "unmanaged" OLE DB providers (unmanaged means that the

namespace uses code written outside of the .NET CLR). The

System.Data.SqlClient namespace allows you to work with data in Microsoft

SQL Server 7.0 or higher, using the SQL Server native API (with no OLE DB

provider required). The classes and their members are almost identical in

name and functionality. Table 4.1 provides a list of the most commonly used

classes from the System.Data.OleDb and System.Data.SqlClient namespaces. To

make things simpler, we've removed the namespace-specific prefixes from

each object. Add the appropriate prefix ("OleDb" or "Sql") to each of these

class names.

Table 4.1. ADO.NET Classes Class Description

Connection This class is responsible for making the actual connection to

the database. It has methods for starting, committing, and rolling back

transactions as well.

Command This class allows you to submit SQL statements to a data source or

to call stored procedures. It can either perform action queries (INSERT,

UPDATE, and DELETE) or execute SELECT statements to return a DataReader

object.

DataReader This class provides forward-only, read-only access to data. If

you're an ADO developer, you're familiar with this type of data access梚t's

similar to the default ADO Recordset (often called a fire hose cursor).

DataAdapter This class encapsulates a connection and one or more commands

(SELECT, UPDATE, DELETE, and INSERT). It can retrieve and update data and

uses its SelectCommand property to fill a DataSet or DataTable object with

data.

The System.Data Classes

The System.Data class provides a number of classes, but you'll only use a

few of them when developing your applications. The classes described here

all work with data in memory rather than interacting with any particular

data source. You can typically use these in-memory data stores to both view

and update data. Table 4.2 provides a list of the most commonly used

System.Data classes.

Table 4.2. System.Data Classes Class Description

DataSet This data cache is much like an in-memory database. It can contain

one or more DataTable objects. You can store the data as well as the schema

information for the tables. You may also set relationships between

DataTable objects within a DataSet object.

DataTable A DataTable provides a collection of DataRow objects. Each

DataRow object is, in turn, made up of a collection of DataColumn objects.

A DataTable is a single table or a single view of data. A DataTable can be

populated via a DataAdapter object.

DataView A DataView object is used to sort or filter the data within a

DataTable. You can also use a DataView for searching data as well.

Making database access part of the underlying .NET Framework means that any

language targeting the .NET platform will have access to the same set of

data classes. In addition, because the data-access classes are part of the

.NET Framework, if or when the framework is ported to alternate platforms

such as Linux, the same data classes should be available in the new

platforms as well.

This set of data classes in .NET is known collectively as ADO.NET. ADO.NET

is different from previous Microsoft database access technologies in

several ways. It's more highly focused on disconnected data access, which

makes it easier for Web developers to work with data. ADO.NET separates

objects that talk to data sources from objects that just hold data. The

data classes also "speak" fluent XML. This makes it easier to transmit data

from one application to another, regardless of operating system or

transport mechanism.

A Data-Handling Example

The following example reads data from the Products table in the Northwind

sample database. It uses a number of the ADO.NET classes, including

OleDbDataAdapter and DataSet.

The procedure shown in this section, XMLGet, first creates a connection

string and a SQL string. It passes these two strings to the constructor of

the OleDbDataAdapter object. The code then calls the OleDbDataAdapter

object's Fill method in order to fill the DataSet object with data.

NOTE

You don't need to type System.Data.OleDb.OleDbDataAdapter here because VB

.NET automatically includes an Imports statement that imports the

System.Data namespace.

The Fill method is responsible for creating an implicit OleDbConnection

object using the supplied connection string. Next, the Fill method creates

an OleDbCommand object and uses the SELECT statement to read the data from

the data source. The Fill method reads the data and fills one table within

the DataSet object. The code closes the collection, leaving you with just

the filled DataSet object.

The DataSet object's GetXml method returns its entire set of data as an XML

stream. Your code can handle the XML in any way it requires梩he following

code snippet places the XML into a text box on the sample page:

Private Sub XMLGet()

Dim strConn As String

Dim strSQL As String

Dim da As OleDb.OleDbDataAdapter

Dim ds As DataSet

strConn = "Provider=sqloledb;Data

Source=(local);Initial Catalog=Northwind;User ID=sa"

strSQL = "SELECT ProductID, ProductName FROM Products"

da = New OleDb.OleDbDataAdapter(strSQL, strConn)

ds = New DataSet()

da.Fill(ds)

txtXML.Text = ds.GetXml()

End Sub

If you know that you are only going to be using SQL Server, you might

choose to use the SqlClient namespace. The classes in this namespace are

specific for SQL Server and will give you a little better performance when

working with SQL Server. The following code snippet provides the same

functionality, using the SqlClient namespace classes:

Private Sub XMLGetSqlClient()

Dim strConn As String

Dim strSQL As String

Dim da As SqlClient.SqlDataAdapter

Dim ds As DataSet

strConn = "Server=(local);Database=Northwind;" _

"User ID=sa"

strSQL = "SELECT ProductID, ProductName FROM Products"

da = New SqlClient.SqlDataAdapter(strSQL, strConn)

ds = New DataSet()

da.Fill(ds)

txtXML.Text = ds.GetXml()

End Sub

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有