标题:English articles----ADO.NET technology
只看楼主
iloveenglish
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-5-7
 问题点数:0 回复次数:2 
English articles----ADO.NET technology
Microsoft. NET include a new data access technology called Using this new technology to various reasons, including: the disconnection of the internal data support, as well as comprehensive support for the XML technology with the whole. NET framework for seamless integration.

This paper focused on what features of the technology include, and how to make full use of this technology to use the database. We first introduced connected to the basic elements of the database, and then discuss how to read and write database. The contents of extensive operation against each provided a lot of ways. In this paper, we will use the DataSet object to read and write data, and try to use DataReader and the Command object manually read and write data. Dynamic SQL at the same time will also explore the use and storage process. Read this article, you will be able to use to create, read, update and delete data.

Most Visual Basic? . NET project types are automatically invoked , including Windows? Applications, Web applications and class library project. In addition, these types of projects to a particular level, will automatically into System.Data namespace. You can project Property (attributes) that the overall situation in the dialog boxes List Import statement.

System.Data namespace for application to provide basic data to support focused mainly on the use of data rather than the data access itself. This means that, in the default situation, we can visit DataSet object and its related sub-object, you can also access definition of how to proceed with the basic data access interface.

However, to interact with the database before, we need to identify the data used to provide procedures. Microsoft. NET includes two off-the-shelf data provider. SQL Server provides procedures for Microsoft SQL Server? 7.0 or higher version of the optimization of the visit. OleDb provide procedures to provide a procedure to provide all the OleDb database visit.

Provide procedures in SQL Server System.Data.SqlClient namespace. Provide procedures for the visit of SQL Server were optimized, and the use of ADO through OLE DB compared, it can provide better performance. In addition, the program also has provided an intelligent link buffer pool mechanism, and therefore its predecessor OLE DB or ODBC compared to the buffer can provide faster connections to the database.

OleDb procedures provided in the System.Data.OleDb namespace. Provide procedures for the use of this, we can OLE DB database and interactive. Contain any provision of the existing OLE DB database procedures can be adopted. NET in the OleDb provide procedures for a visit.

There is also a Odbc provider, you can download the program from MSDN. It is ODBC packaging program that lets you with any ODBC driver with an interactive database.

Connected to the database

Database connectivity by providing procedures for database processing. All offers procedures will provide a connection object. SQL procedures include providing SqlConnection object, and provide procedures include OleDbConnection OleDb objects. Linking these two objects are connected using string and other information (such as security evidence) to define the location of the database.

OleDbConnection use ordinary OLE DB connection string, it is not surprising that, because it is only passed to the foundation string OLE DB Provider. Below are the Access database for the OleDbConnection string Example:

Provider = Microsoft.Jet.OLEDB.4.0; Password = ""; User ID = Admin; Data

Source = grocertogo.mdb

SqlConnection use similar connection string, but it does not need database provider, but only on how to find needed information and database security credentials. Below are the Pubs database SQL Server examples:

Data source = localhost; Initial catalog = pubs; User id = sa; Password =

In many cases, even when the object manually create links, we also have to worry about how to create your own connection string. Visual Studio? . NET IDE contains the functions that we do not need to type any code can be easily added and configured connections.

In the toolbox, you will find Data (data) tab, including common data objects, such as DataSet, OleDbConnection and SqlConnection object. You can drag and drop these objects to the Windows Forms, Web Form or components of the design.

These non-object graphics entity, it will not directly displayed on the window, but on display in the design of icons at the bottom of the column to facilitate user access. These entities are in many ways similar to the controls, so when these entities click, you can use an ordinary Properties (attributes) window to change their attributes and settings.

For example, we create a name ADO Windows applications, and drag and drop一个SqlConnection object in the window. The object icon will appear in control column. Then, you can use Properties (attributes) window will be its name changed to a more descriptive name, such as cnPubs.

Wizard is a more useful tool, it can help us connect string structure. In Properties (attributes) window click ConnectionString field, and then click the field revealed that the next arrow. Controls which will set out the definition of all connections and a <New Connection...> (<link ...>) new entry.

If you click <New Connection...> (<link ...>) new entries, Visual Studio. NET will display a dialog to define data sources.

Click OleDbConnection object will show standard OLE DB connection dialog box, you can choose from OLE DB data provider, and the designation of the procedures necessary to provide full information. Click SqlConnection object will show a dialog box, you can choose from SQL Server, security settings and choose a definition of the server database.

Here, we set up a local computer at the Pubs database connectivity, in the back of examples will use the link. CnPubs settings stored in the Windows Forms Form Design in the generated code for the region, by default, these settings are hidden. This and other Forms of control is no different.

You can also choose the manual configuration connecting objects. To this end, need to use code similar to the following:

Dim cnPubs As New SqlConnection ()

CnPubs.ConnectionString = _

"Data source = localhost; Initial catalog = pubs; Integrated security = SSPI "

With these basic code for the design of the basic code we create the same.

Open and close the connection

Please note that regardless of the design or use manual code, only definition of the connection, and did not open the link. We open and close the connection when it has complete control over the application procedure can be carried out in the most effective operation.

Remember, SqlClient will buffer database connectivity, therefore, as a general principle, it is better to open the links for system design, the use of connections, and immediately after the completion of operations and close the connection. When using OleDb client, a database for your specific OLE DB Provider, you need to follow certain best practices and experiences.

Use correct error handling to ensure that the database connectivity always been closed (even when there is a mistake), this point is very important. The use of database connections, recommend the following structure:

Try

CnPubs.Open ()

Try

'Data visit here

Finally

CnPubs.Close ()

End Try

Catch

'Wrong here handle data access

End Try

The outer Try .. Catch block allows you to deal with any possible data access errors, including not break connection, as well as in the read or update data at any mistakes that may occur.

Try .. Finally the inner block can ensure that, even in the read or update data has indeed wrong circumstances, the connections are still closed. Finally replacement structure will not be wrong, all wrong Catch all the outer structure will be caught. However, whether or not an error occurs, will run Finally block to ensure correct and close the connection.

In fact, in many cases you do not need this structure, because we are about to use the other objects are automatically open or close the connection. Generally speaking, you prepare for the substantial additional data access code, is likely to be targeted or through other methods provide a more simple solution.

The use of data

We already know how the definition of open and closed Database Connection, Next, we will read and write through the use of data to the database.

Interactive database is ultimately passed Command object to complete, although these objects more likely to be hidden in other abstract objects behind. Command object contains information on how to read or update data in the database directives. These directives may be dynamic SQL statements, also may be called for information storage process.

Read data usually have two methods. One is the DataSet object, which we can use to provide a, abstract and disconnected copy of the data. DataSet filled by the DataAdapter object, DataAdapter object with basic Command and Connection objects interactive.

DataAdapter using a DataReader objects called from the database access to the actual data. Then, these data will be used to fill DataSet object.

The second way is to read data directly use DataReader, thereby bypassing the entire DataSet and DataAdapter technology. This approach in the creation of Web pages or XML Web Services, is particularly useful, because we usually only extract data from the database, then simply copy the pages of its XML output or outcome. Data will be copied to the DataSet, and then copy the page or its XML results need to spend extra time.

Although this manual methods are sometimes quite fast, but compared to the use of DataAdapter and DataSet, which we need to do more.

DataSet and DataAdapter object also provided the mechanism for updating data. DataAdapter not only includes the Command object retrieval of data, but also contains insert, update and delete data Command object. You simply have to change the DataSet object transmitted to the DataAdapter, the latter will automatically call the Command object corresponding change in the update to the database.

By default, this update process with the use of optimized processing, that is, before the update will examine basic data. If the user changes the data, it is not applied update, and will notify the code in the conflict. You can disable this behavior, so as to avoid blindly into the database will be updated, regardless of the changes made by other users.

In addition, you can also choose through direct call Command Objects (which includes instructions for updating the database) manually update the database. In such circumstances, we need to address any issue with.

Use DataSet read and update data

DataSet is stored in memory for you to use the disconnection copy of the data. Data provided from the proceedings filled DataSet, we need to use the DataAdapter object, the object will use the Command object Generation DataReader, then data can be read from the DataReader will DataSet.

The creation and configuration DataSet many ways, the simplest way is to use Visual Studio. NET. You can also manually configuration DataSet, but like dealing with the same object connect you with the code prepared by the Visual Studio. NET code automatically generated the same effect.

Interestingly, the adoption of Visual Studio. NET DataSet is actually the creation of such a process, namely the creation of DataAdapter object, then let DataAdapter object automatic generation DataSet.

From the toolbox will SqlDataAdapter OleDb tab drag and drop objects in the window. This will activate the guide.

The first panel shows, and then guide the next panel will be asked to use the connection object. We have only one connection, it is easy to choose. Please note that you can also connect from the creation of a panel, then use the DataAdapter started the whole process, so that we can through this wizard guides you through the whole process.

  

A panel depends on the choice of options. If you choose to use SQL statements or create new storage process, you can use input SELECT statements or inquiries generator to build a storage process. Executive insert, update and delete operations will be based on three other orders automatically constructed SELECT statements.

If you choose the existing storage process, a panel will allow you to choose four storage process orders with four associated. In this example, we use contains the SQL SELECT statements, as follows:

SELECT

Au_id,

Au_lname,

Au_fname

FROM

Authors

The last panel guide only confirm our designated operation. Click Finish (completed), the code for the DataAdapter configuration will be saved to Form or other hidden code designed for the region.

In Properties (attributes) window, will change the name of DataAdapter object to daAuthors, because it is configured for use together with the authors table.

You can right-click the column controls icon DataAdapter, and then choose Configure Data Adapter (configuration data adapter) option, which of these options to change.

You can also right-click control and select Generate Dataset (generating data sets), the representative of the DataAdapter automatically create data DataSet object. This configuration DataSet object is the most simple method, we will use this method.

This will display a dialog box, from which you can be with existing DataSet DataAdapter linked to, or create a new DataSet. In this example, we will create a new DataSet called dsPubs.

Click OK (determine), can create DataSet. A new document called dsPubs1.xsd will be added to the project, a new name dsPubs1 Controls Controls icon will be added to the column.

Xsd XML document is a framework document for the definition of control maintained by the data structure. The framework is based DataAdapter object of SELECT statements or stored procedures automatically generated.

Click dsPubs1 control, and then Properties (attributes) window will change its name to dsPubs. The control is our DataSet, it is now ready to programming or data through the use of bundled data in the DataAdapter.

Choose DataGrid, and then use Properties (attributes) of its DataSource attribute window set to dsPubs.Authors. This will mesh bundled with DataSet, and will immediately display grid are correct name. The project has added xsd framework document, and therefore all the data are available.

The remaining work just add some code, the use of DataAdapter to load the DataSet. In Refresh data (data set) buttons behind adding this code:

Private Sub btnRefresh_Click (ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnRefresh.Click

DaAuthors.Fill (DsPubs.authors)

End Sub

Please note that here we do not have open or close the connection. Because we DataAdapter can be done automatically for this operation. It can open connectivity, data acquisition and close connections, and no need for us to prepare any additional code.

Similarly, we can Save Changes (preservation changes) button inserted following code to update the data:

Private Sub btnSave_Click (ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnSave.Click

DaAuthors.Update (DsPubs.authors)

End Sub

As DataAdapter contain enforceable insert, update and delete operations Command object, it only called Automatic Update method will be able to complete all the tasks. DataSet itself will be tracking data for each trip was changed, inserted or deleted, this technology will be changed to update the data in the database.

As grid bundled with DataSet, when we run the application and click Refresh (refresh) button, the data will show, and can edit the data. Just click Save Changes (preservation changes) button, the changes will be saved to the database.

This shows that the DataAdapter DataSet object and the power, but also shows that we right. NET data binding in the control.

Furthermore, we should also be aware that the use of the DataSet here is very simple. In fact, DataSet can simultaneously support multiple forms, and can know the relationship between these tables, which help to strengthen relations between the rules and navigation data. DataSet is completely disconnected connected, that is, through the network can be transmitted to the remote client, and remote client can be returned to the server (which can be used to change the DataAdapter updates to the database).

Manually read and update data

Although DataSet and DataAdapter very simple and very useful, but they are not the best choice at all times. Sometimes, we need to quickly read data, and does not require storage of data in memory copy. Other times, we have to update the data in the database may not come from the DataSet, or to update the data in the database, will be deemed unsuitable for use DataSet data on memory.

Read data, we could only find data and send it to other locations, such as Web pages, XML files or text files. Under these circumstances, the direct use DataReader object will be more effective.

To explain its methods of work, we replicate data to text file. Add in another Forms button, and was named btnCopyToFile. Then add the following code:
Private Sub btnCopyToFile_Click (ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCopyToFile.Click
Dim cn As New SqlConnection ()
Dim cm As New SqlCommand ()
Cn.ConnectionString = _
"Data source = localhost; Initial catalog = pubs; Integrated security = SSPI "
Try
Cn.Open ()
Try
Cm.Connection = cn
= "SELECT au_id, au_fname, au_lname FROM authors"
'Data processing here
Finally
Cn.Close ()
End Try
Catch ex As Exception
MsgBox (ex.ToString)
End Try
End Sub
This is what we discussed in front of the code structure, it would open the Connection object and the closure of the operation included in the nested Try block to ensure that no matter what mistakes can close the connection. Please note that our allocation of a Command object to the use of SQL statements from the authors table in the choice of data. Here, we use Command object directly, rather than be embedded in the DataAdapter.
搜索更多相关主题的帖子: technology NET articles framework English 
2008-05-07 19:09
DL_ASD
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-5-7
得分:0 
好深奥啊,技术.没中文吗?
2008-05-07 19:20
A13433758072
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:广东潮州
等 级:小飞侠
威 望:1
帖 子:1182
专家分:2784
注 册:2010-7-22
得分:0 
为什么不发中文啊!!!!





技术----英文文章
Microsoft.微软。 NET include a new data access technology called NET中包括一个新的数据访问技术,称为。 Using this new technology to various reasons, including: the disconnection of the internal data support, as well as comprehensive support for the XML technology with the whole.使用这种新技术的各种原因,包括:内部数据支持断线,以及与整个XML技术的全面支持。 NET framework for seamless integration. NET框架的无缝集成。

This paper focused on what features of the technology include, and how to make full use of this technology to use the database.哪些特性的技术包括,如何集中本文利用这一技术充分利用使用该数据库。 We first introduced connected to the basic elements of the database, and then discuss how to read and write database.我们首先介绍了连接到数据库的基本元素,然后讨论如何读取和写入数据库。 The contents of extensive operation against each provided a lot of ways.对每个的粗放经营的内容提供了很多办法。 In this paper, we will use the DataSet object to read and write data, and try to use DataReader and the Command object manually read and write data.在本论文中,我们将使用DataSet对象读取和写入数据,并尝试使用DataReader和Command对象手动读取和写入数据。 Dynamic SQL at the same time will also explore the use and storage process.动态的SQL,同时也将探讨使用和存储过程。 Read this article, you will be able to use to create, read, update and delete data.阅读这篇文章,你将可以使用来创建,读取,更新和删除数据。

Most Visual Basic?大多数Visual Basic? . 。 NET project types are automatically invoked , including Windows? NET项目类型被自动调用中,包括Windows? Applications, Web applications and class library project.应用程序, Web应用程序和类库项目。 In addition, these types of projects to a particular level, will automatically into System.Data namespace.此外,这些类型的项目到一个特定水平,将自动进入System.Data命名空间。 You can project Property (attributes) that the overall situation in the dialog boxes List Import statement.你可以项目属性(属性)对话框中的列表导入声明的大局。

System.Data namespace for application to provide basic data to support focused mainly on the use of data rather than the data access itself. System.Data的应用提供基础数据空间,支持重点主要放在对数据的使用,而不是数据访问本身。 This means that, in the default situation, we can visit DataSet object and its related sub-object, you can also access definition of how to proceed with the basic data access interface.这意味着,在默认情况下,我们可以参观的DataSet对象及其相关的子对象,也可以访问定义如何进行基本的数据访问接口。

However, to interact with the database before, we need to identify the data used to provide procedures.然而,与数据库进行交互之前,我们需要确定用于提供程序的数据。 Microsoft.微软。 NET includes two off-the-shelf data provider. NET包含两个现成的现成数据提供程序。 SQL Server provides procedures for Microsoft SQL Server? SQL Server提供的Microsoft SQL Server的程序? 7.0 or higher version of the optimization of the visit. 7.0或更高版本的访问的优化。 OleDb provide procedures to provide a procedure to provide all the OleDb database visit.的OLEDB提供程序提供一个过程来提供所有的OLEDB数据库的访问。

Provide procedures in SQL Server System.Data.SqlClient namespace.在SQL Server提供System.Data.SqlClient命名空间的程序。 Provide procedures for the visit of SQL Server were optimized, and the use of ADO through OLE DB compared, it can provide better performance.为SQL Server的访问程序进行了优化,并通过OLE DB的ADO使用相比,它可以提供更好的性能。 In addition, the program also has provided an intelligent link buffer pool mechanism, and therefore its predecessor OLE DB or ODBC compared to the buffer can provide faster connections to the database.此外,该软件还提供了一个智能连接缓冲池的机制,因此,其前身OLE DB或ODBC相比,缓冲区可提供更快速的连接到数据库。

OleDb procedures provided in the System.Data.OleDb namespace.的OLEDB提供程序在System.Data.OleDb命名空间。 Provide procedures for the use of this, we can OLE DB database and interactive.为这个使用过程中,我们可以OLE DB数据库和互动。 Contain any provision of the existing OLE DB database procedures can be adopted.包含任何现有的OLE DB数据库程序的规定可以被采纳。 NET in the OleDb provide procedures for a visit. NET中的OLEDB提供程序进行访问。

There is also a Odbc provider, you can download the program from MSDN.还有一个ODBC提供程序,您可以从MSDN下载程序。 It is ODBC packaging program that lets you with any ODBC driver with an interactive database.这是ODBC包装方案,让任何一个互动的数据库的ODBC驱动程序。

Connected to the database连接到数据库

Database connectivity by providing procedures for database processing.通过提供数据库连接数据库处理程序。 All offers procedures will provide a connection object.所有提供程序将提供一个连接对象。 SQL procedures include providing SqlConnection object, and provide procedures include OleDbConnection OleDb objects. SQL过程包括提供SqlConnection对象,并提供程序包括OleDbConnection的OLEDB访问对象。 Linking these two objects are connected using string and other information (such as security evidence) to define the location of the database.连接这两个对象是连接使用字符串和其他信息(如安全证据)来定义数据库的位置。

OleDbConnection use ordinary OLE DB connection string, it is not surprising that, because it is only passed to the foundation string OLE DB Provider. OleDbConnection的使用普通的OLE DB连接字符串,这是不足为奇的,因为它是唯一的字符串传递给OLE DB提供基础。 Below are the Access database for the OleDbConnection string Example:下面是为OleDbConnection字符串示例Access数据库:

Provider = Microsoft.Jet.OLEDB.4.0; Password = ""; User ID = Admin; Data供应商= Microsoft.Jet.OLEDB.4.0;密码=“”;用户ID =管理员,数据

Source = grocertogo.mdb来源= grocertogo.mdb

SqlConnection use similar connection string, but it does not need database provider, but only on how to find needed information and database security credentials.使用类似的SqlConnection连接字符串,但它并不需要数据库提供者,但仅限于如何找到所需的信息和数据库的安全认证。 Below are the Pubs database SQL Server examples:以下是在Pubs数据库的SQL Server实例:

Data source = localhost; Initial catalog = pubs; User id = sa; Password =资料来源=本地主机;初始目录=酒吧,用户的ID =山,密码=

In many cases, even when the object manually create links, we also have to worry about how to create your own connection string.在许多情况下,即使对象手动创建链接,我们也不必担心如何创建自己的连接字符串。 Visual Studio?视觉工作室? . 。 NET IDE contains the functions that we do not need to type any code can be easily added and configured connections. NET IDE中包含的功能,我们并不需要输入任何代码可以很容易地添加和配置的连接。

In the toolbox, you will find Data (data) tab, including common data objects, such as DataSet, OleDbConnection and SqlConnection object.在工具箱中,你会发现数据(数据)选项卡,包括常用的数据对象,如DataSet中,OleDbConnection和SqlConnection对象。 You can drag and drop these objects to the Windows Forms, Web Form or components of the design.您可以拖放这些对象到Windows窗体,Web窗体或组件的设计。

These non-object graphics entity, it will not directly displayed on the window, but on display in the design of icons at the bottom of the column to facilitate user access.这些非图形对象的实体,它不会直接显示在窗口,而是显示在该列底部的图标设计,以方便用户访问。 These entities are in many ways similar to the controls, so when these entities click, you can use an ordinary Properties (attributes) window to change their attributes and settings.这些实体在许多方面类似的控制,因此,当这些实体的点击,你可以使用普通的属性(属性)窗口来改变它们的属性和设置。

For example, we create a name ADO Windows applications, and drag and drop一个SqlConnection object in the window.例如,我们创建了一个名称ADO的Windows应用程序,和拖放窗口中的一个SqlConnection对象。 The object icon will appear in control column.对象图标将出现在控制列。 Then, you can use Properties (attributes) window will be its name changed to a more descriptive name, such as cnPubs.然后,您可以使用属性(属性)窗口将其名称更改为更具描述性的名称,如cnPubs。

Wizard is a more useful tool, it can help us connect string structure.向导是一个比较有用的工具,它可以帮助我们连接字符串结构。 In Properties (attributes) window click ConnectionString field, and then click the field revealed that the next arrow.在属性(属性)窗口中单击ConnectionString的字段,然后单击域透露,下一个箭头。 Controls which will set out the definition of all connections and a <New Connection...> (<link ...>) new entry.控制这将列出所有连接的定义和<新Connection...>(<link ...>)的新条目。

If you click <New Connection...> (<link ...>) new entries, Visual Studio.如果您单击<新建Connection...>(<link ...>)新条目时,Visual Studio。 NET will display a dialog to define data sources. NET将显示一个对话框来定义数据源。

Click OleDbConnection object will show standard OLE DB connection dialog box, you can choose from OLE DB data provider, and the designation of the procedures necessary to provide full information.点击OleDbConnection对象将显示标准的OLE DB连接对话框中,您可以选择从OLE DB数据提供者,和必要程序,以提供充分的信息名称。 Click SqlConnection object will show a dialog box, you can choose from SQL Server, security settings and choose a definition of the server database.点击SqlConnection对象将显示一个对话框,你可以选择从SQL服务器,安全设置,选择一个服务器的数据库定义。

Here, we set up a local computer at the Pubs database connectivity, in the back of examples will use the link.在这里,我们设在Pubs数据库连接本地计算机上,在后面的例子将使用链接。 CnPubs settings stored in the Windows Forms Form Design in the generated code for the region, by default, these settings are hidden.在Windows Form窗体设计存储在该地区生成的代码默认情况下,CnPubs设置,这些设置是隐藏的。 This and other Forms of control is no different.这和其他形式的控制是没有什么不同。

You can also choose the manual configuration connecting objects.您也可以选择手动配置连接对象。 To this end, need to use code similar to the following:为此,需要使用类似的代码如下:

Dim cnPubs As New SqlConnection ()由于新的SqlConnection(点心cnPubs)

CnPubs.ConnectionString = _ CnPubs.ConnectionString = _

"Data source = localhost; Initial catalog = pubs; Integrated security = SSPI " “数据源=本地主机;初始目录=酒吧,综合安全= SSPI的”

With these basic code for the design of the basic code we create the same.有了这些基本的代码,我们创建相同的设计的基本代码。

Open and close the connection打开和关闭连接

Please note that regardless of the design or use manual code, only definition of the connection, and did not open the link.请注意,无论在设计或使用手册的代码,唯一的连接定义,并没有打开链接。 We open and close the connection when it has complete control over the application procedure can be carried out in the most effective operation.我们打开和关闭连接时,它已经超过可以进行最有效的操作了应用程序的完全控制。

Remember, SqlClient will buffer database connectivity, therefore, as a general principle, it is better to open the links for system design, the use of connections, and immediately after the completion of operations and close the connection.请记住,SqlClient的将数据库连接缓冲,因此,作为一般原则,最好是打开系统的设计链接,连接的使用,并立即行动完成后,关闭连接。 When using OleDb client, a database for your specific OLE DB Provider, you need to follow certain best practices and experiences.当使用OLEDB客户端,为您的特定的OLE DB提供程序数据库,则需要遵循一定的最佳做法和经验。

Use correct error handling to ensure that the database connectivity always been closed (even when there is a mistake), this point is very important.使用正确的错误处理,以确保数据库连接总是被关闭(即使是一个错误),这一点是非常重要的。 The use of database connections, recommend the following structure:使用的数据库连接,推荐下面的结构:

Try尝试

CnPubs.Open () CnPubs.Open()

Try尝试

'Data visit here '数据点击这里查看

Finally最后

CnPubs.Close () CnPubs.Close()

End Try最终的结局

Catch捕捉

'Wrong here handle data access '错误在这里处理数据访问

End Try最终的结局

The outer Try ..外层的尝试.. Catch block allows you to deal with any possible data access errors, including not break connection, as well as in the read or update data at any mistakes that may occur. Catch块可以应付任何可能的数据访问错误,包括未破的连接,以及在读或更新任何可能出现的错误数据。

Try ..试试.. Finally the inner block can ensure that, even in the read or update data has indeed wrong circumstances, the connections are still closed.最后,内部块可以保证,即使在读取或更新确实错了的情况下,仍然关闭数据连接。 Finally replacement structure will not be wrong, all wrong Catch all the outer structure will be caught.最后更换结构将不会有错,都错了捕捉所有的外部结构将被捕获。 However, whether or not an error occurs, will run Finally block to ensure correct and close the connection.但是,无论是否发生错误,将运行finally块来确保正确和关闭连接。

In fact, in many cases you do not need this structure, because we are about to use the other objects are automatically open or close the connection.事实上,在许多情况下,你不需要这个结构,因为我们将要使用其它对象是自动开启或关闭连接。 Generally speaking, you prepare for the substantial additional data access code, is likely to be targeted or through other methods provide a more simple solution.一般来说,你提供了大量额外的数据访问代码编写,是可能成为袭击目标或通过其他方法提供了更简单的解决方案。

The use of data使用数据

We already know how the definition of open and closed Database Connection, Next, we will read and write through the use of data to the database.我们已经知道如何打开和关闭数据库连接,下一个定义,我们将通过读写数据的使用到数据库中。

Interactive database is ultimately passed Command object to complete, although these objects more likely to be hidden in other abstract objects behind.交互式数据库,最终通过Command对象来完成,但这些对象更可能是在其他抽象的物体隐藏在后面。 Command object contains information on how to read or update data in the database directives. Command对象包含有关如何读取或更新数据库中的数据信息的指令。 These directives may be dynamic SQL statements, also may be called for information storage process.这些指令可能是动态的SQL语句,也可能是对信息存储过程调用。

Read data usually have two methods.读取数据通常有两种方法。 One is the DataSet object, which we can use to provide a, abstract and disconnected copy of the data.一个是DataSet对象,我们可以用它来提供的,抽象的和断开的数据副本。 DataSet filled by the DataAdapter object, DataAdapter object with basic Command and Connection objects interactive.数据集由DataAdapter对象填充,具有基本的命令和Connection对象互动DataAdapter对象。

DataAdapter using a DataReader objects called from the database access to the actual data. DataAdapter的使用DataReader从数据库中获取的实际数据称为对象。 Then, these data will be used to fill DataSet object.然后,这些数据将被用于填充DataSet对象。

The second way is to read data directly use DataReader, thereby bypassing the entire DataSet and DataAdapter technology.第二种方法是直接使用DataReader读取数据,从而绕过整个DataSet和DataAdapter技术。 This approach in the creation of Web pages or XML Web Services, is particularly useful, because we usually only extract data from the database, then simply copy the pages of its XML output or outcome.这在Web网页或XML Web服务的创建方式,是特别有用,因为我们通常只从数据库中提取数据,然后简单地复制它的XML输出或结果的网页。 Data will be copied to the DataSet, and then copy the page or its XML results need to spend extra time.数据将被复制到DataSet,然后将复制的网页或XML结果需要花费额外的时间。

Although this manual methods are sometimes quite fast, but compared to the use of DataAdapter and DataSet, which we need to do more.虽然本手册方法有时相当快,但相比,DataAdapter和DataSet的,这就需要我们做更多的使用。

DataSet and DataAdapter object also provided the mechanism for updating data. DataSet和DataAdapter对象还提供了更新数据的机制。 DataAdapter not only includes the Command object retrieval of data, but also contains insert, update and delete data Command object. DataAdapter的不仅包括数据命令对象检索外,还含有插入,更新和删除数据命令对象。 You simply have to change the DataSet object transmitted to the DataAdapter, the latter will automatically call the Command object corresponding change in the update to the database.您只需更改DataSet对象传递给DataAdapter的,后者会自动调用命令对象与数据库的更新相应的变化。

By default, this update process with the use of optimized processing, that is, before the update will examine basic data.默认情况下,这与优化处理,也就是前使用此更新,更新过程将研究的基础资料。 If the user changes the data, it is not applied update, and will notify the code in the conflict.如果用户更改数据,它不是应用的更新,并通知在冲突代码。 You can disable this behavior, so as to avoid blindly into the database will be updated, regardless of the changes made by other users.您可以禁用此行为,以避免盲目到数据库将被更新,而不管其他用户所做的更改。

In addition, you can also choose through direct call Command Objects (which includes instructions for updating the database) manually update the database.此外,您也可以选择通过直接调用命令对象(包括用于更新数据库的说明)手动更新数据库。 In such circumstances, we need to address any issue with.在这种情况下,我们需要解决的任何问题。

Use DataSet read and update data使用DataSet读取和更新数据

DataSet is stored in memory for you to use the disconnection copy of the data. DataSet是存储在内存中供您使用数据的断开的副本。 Data provided from the proceedings filled DataSet, we need to use the DataAdapter object, the object will use the Command object Generation DataReader, then data can be read from the DataReader will DataSet.数据填充DataSet的诉讼规定,我们需要使用DataAdapter对象,该对象将使用Command对象的生成DataReader的,那么数据可以从DataReader中读取的数据集。

The creation and configuration DataSet many ways, the simplest way is to use Visual Studio.数据集的创建和配置方法很多,最简单的方法是使用Visual Studio。 NET. NET的。 You can also manually configuration DataSet, but like dealing with the same object connect you with the code prepared by the Visual Studio.您也可以手动配置的DataSet,连接,但与同一个对象一样处理由Visual Studio中编写的代码。 NET code automatically generated the same effect. NET代码自动生成同样的效果。

Interestingly, the adoption of Visual Studio.有趣的是,Visual Studio的通过。 NET DataSet is actually the creation of such a process, namely the creation of DataAdapter object, then let DataAdapter object automatic generation DataSet. NET的数据集实际上是这样一个进程的创建,即创建DataAdapter对象,然后让DataAdapter对象自动生成的DataSet。

From the toolbox will SqlDataAdapter OleDb tab drag and drop objects in the window.从工具箱将在窗口的OLEDB SqlDataAdapter的标签拖放对象。 This will activate the guide.这将激活指南。

The first panel shows, and then guide the next panel will be asked to use the connection object.第一个小组表演,然后引导下一个小组将被要求使用的连接对象。 We have only one connection, it is easy to choose.我们只有一个连接,很容易选择。 Please note that you can also connect from the creation of a panel, then use the DataAdapter started the whole process, so that we can through this wizard guides you through the whole process.请注意,您也可以从一个创作小组,然后使用DataAdapter开始的全过程,通过这个向导将引导您,使我们能够通过整个过程。



A panel depends on the choice of options.一个小组取决于选择范围。 If you choose to use SQL statements or create new storage process, you can use input SELECT statements or inquiries generator to build a storage process.如果您选择使用SQL语句或创建新的存储过程中,您可以使用输入或查询的SELECT语句生成器来建立一个存储过程。 Executive insert, update and delete operations will be based on three other orders automatically constructed SELECT statements.执行插入,更新和删除操作将放在三个其他命令SELECT语句的自动构造。

If you choose the existing storage process, a panel will allow you to choose four storage process orders with four associated.如果您选择现有存储过程中,一个小组将允许您选择四个存储过程相关的四个订单。 In this example, we use contains the SQL SELECT statements, as follows:在这个例子中,我们使用包含SQL SELECT语句,如下:

SELECT选择

Au_id, Au_id,

Au_lname,的au_lname,

Au_fname Au_fname

FROM从

Authors作者

The last panel guide only confirm our designated operation.最后只证实了我们的指导小组指定的操作。 Click Finish (completed), the code for the DataAdapter configuration will be saved to Form or other hidden code designed for the region.单击Finish(完成),为DataAdapter配置代码将被保存到表格或其他隐藏代码区域设计的。

In Properties (attributes) window, will change the name of DataAdapter object to daAuthors, because it is configured for use together with the authors table.在属性(属性)窗口中,将改变反对daAuthors DataAdapter的名字,因为它被配置为使用authors表在一起。

You can right-click the column controls icon DataAdapter, and then choose Configure Data Adapter (configuration data adapter) option, which of these options to change.您可以右键单击列DataAdapter的控制图标,然后选择配置数据适配器(配置数据适配器)选项,这些选项的改变。

You can also right-click control and select Generate Dataset (generating data sets), the representative of the DataAdapter automatically create data DataSet object.您也可以右键单击控件,然后选择生成数据集(生成数据集),DataAdapter的代表自动创建数据的DataSet对象。 This configuration DataSet object is the most simple method, we will use this method.此配置DataSet对象是最简单的方法,我们将使用此方法。

This will display a dialog box, from which you can be with existing DataSet DataAdapter linked to, or create a new DataSet.这将显示一个对话框,从中你可以用现有的DataSet DataAdapter的联系,或者创建一个新的DataSet。 In this example, we will create a new DataSet called dsPubs.在这个例子中,我们将创建一个新的DataSet称为dsPubs。

Click OK (determine), can create DataSet.点击OK(确定),可以创建数据集。 A new document called dsPubs1.xsd will be added to the project, a new name dsPubs1 Controls Controls icon will be added to the column.一个新的文件名为dsPubs1.xsd将被添加到项目中,一个新的名称dsPubs1控件图标将被添加到该列。

Xsd XML document is a framework document for the definition of control maintained by the data structure. XSD的XML文件是一个用于控制的数据结构保持定义框架文件。 The framework is based DataAdapter object of SELECT statements or stored procedures automatically generated. DataAdapter的框架是基于对象的SELECT语句或存储自动生成程序。

Click dsPubs1 control, and then Properties (attributes) window will change its name to dsPubs.点击dsPubs1控制,然后属性(属性)窗口将其名称更改为dsPubs。 The control is our DataSet, it is now ready to programming or data through the use of bundled data in the DataAdapter.控制是我们的DataSet时,它正准备进行编程或通过在捆绑数据使用DataAdapter的数据。

Choose DataGrid, and then use Properties (attributes) of its DataSource attribute window set to dsPubs.Authors.选择DataGrid,然后利用它的DataSource属性设置为dsPubs.Authors窗口属性(属性)。 This will mesh bundled with DataSet, and will immediately display grid are correct name.这将与DataSet网捆绑,并立即显示格是正确的名称。 The project has added xsd framework document, and therefore all the data are available.该项目已增加的XSD架构文件,因此所有数据都可用。

The remaining work just add some code, the use of DataAdapter to load the DataSet.剩下的工作只需添加一些代码,DataAdapter的使用加载数据集。 In Refresh data (data set) buttons behind adding this code:在(数据集)后面加入此代码按钮刷新的数据:

Private Sub btnRefresh_Click (ByVal sender As System.Object, _私人小组btnRefresh_Click(为System.Object,_ ByVal发件人

ByVal e As System.EventArgs) Handles btnRefresh.Click ByVal e视System.EventArgs)处理btnRefresh.Click

DaAuthors.Fill (DsPubs.authors) DaAuthors.Fill(DsPubs.authors)

End Sub结束小组

Please note that here we do not have open or close the connection.请注意,这里我们没有打开或关闭连接。 Because we DataAdapter can be done automatically for this operation.因为我们DataAdapter都可自动完成此操作。 It can open connectivity, data acquisition and close connections, and no need for us to prepare any additional code.它可以打开连接,数据采集和密切的联系,和我们没有必要准备任何额外的代码。

Similarly, we can Save Changes (preservation changes) button inserted following code to update the data:同样,我们可以保存更改(保存更改)按钮,插入下面的代码来更新数据:

Private Sub btnSave_Click (ByVal sender As System.Object, _私人小组btnSave_Click(为System.Object,_ ByVal发件人

ByVal e As System.EventArgs) Handles btnSave.Click ByVal e视System.EventArgs)处理btnSave.Click

DaAuthors.Update (DsPubs.authors) DaAuthors.Update(DsPubs.authors)

End Sub结束小组

As DataAdapter contain enforceable insert, update and delete operations Command object, it only called Automatic Update method will be able to complete all the tasks.由于DataAdapter的包含强制执行的插入,更新和删除操作命令对象,它只是称为自动更新的方法将能够完成所有任务。 DataSet itself will be tracking data for each trip was changed, inserted or deleted, this technology will be changed to update the data in the database. DataSet本身将每次跟踪数据的更改,插入或删除,这种技术将改变更新数据库中的数据。

As grid bundled with DataSet, when we run the application and click Refresh (refresh) button, the data will show, and can edit the data.至于与DataSet,当我们运行应用程序并单击Refresh(刷新)按钮绑定网格,数据将显示出来,可以编辑数据。 Just click Save Changes (preservation changes) button, the changes will be saved to the database.只要按一下储存变更(保存更改)按钮,更改将被保存到数据库中。

This shows that the DataAdapter DataSet object and the power, but also shows that we right.这表明,DataAdapter将DataSet对象和动力,同时也表明我们的权利。 NET data binding in the control. NET数据在控制约束力。

Furthermore, we should also be aware that the use of the DataSet here is very simple.此外,我们还应该知道,这里使用的数据集是非常简单。 In fact, DataSet can simultaneously support multiple forms, and can know the relationship between these tables, which help to strengthen relations between the rules and navigation data.事实上,数据集可以同时支持多个表格,并能知道这些表之间,这有助于加强两国关系的规则和导航数据的关系。 DataSet is completely disconnected connected, that is, through the network can be transmitted to the remote client, and remote client can be returned to the server (which can be used to change the DataAdapter updates to the database). DataSet是完全断开连接,即可以通过网络传输到远程客户端,远程客户可以返回到服务器(可以用来改变的DataAdapter更新数据库)。

Manually read and update data手动读取和更新数据

Although DataSet and DataAdapter very simple and very useful, but they are not the best choice at all times. DataSet和DataAdapter的虽然非常简单,非常有用,但它们并不是在任何时候的最佳选择。 Sometimes, we need to quickly read data, and does not require storage of data in memory copy.有时候,我们需要快速读取数据,并且不需要在内存中复制数据存储。 Other times, we have to update the data in the database may not come from the DataSet, or to update the data in the database, will be deemed unsuitable for use DataSet data on memory.其他时候,我们必须更新数据库中的数据可能并非来自DataSet或更新数据库中的数据,将被视为对使用DataSet在内存中的数据不适合的。

Read data, we could only find data and send it to other locations, such as Web pages, XML files or text files.读取数据,我们只能找到数据并将其发送到其他地点,如Web页面,XML文件或文本文件。 Under these circumstances, the direct use DataReader object will be more effective.在这种情况下,直接使用DataReader对象将更为有效。

To explain its methods of work, we replicate data to text file.为了解释其工作方法,我们将数据复制到文本文件。 Add in another Forms button, and was named btnCopyToFile.倒入另一窗体按钮,并命名为btnCopyToFile。 Then add the following code:然后添加以下代码:
Private Sub btnCopyToFile_Click (ByVal sender As System.Object, _私人小组btnCopyToFile_Click(为System.Object,_ ByVal发件人
ByVal e As System.EventArgs) Handles btnCopyToFile.Click ByVal e视System.EventArgs)处理btnCopyToFile.Click
Dim cn As New SqlConnection () Cn中作为新的SqlConnection暗淡()
Dim cm As New SqlCommand ()昏暗的CM为新SqlCommand()
Cn.ConnectionString = _ Cn.ConnectionString = _
"Data source = localhost; Initial catalog = pubs; Integrated security = SSPI " “数据源=本地主机;初始目录=酒吧,综合安全= SSPI的”
Try尝试
Cn.Open () Cn.Open()
Try尝试
Cm.Connection = cn Cm.Connection = Cn中
= "SELECT au_id, au_fname, au_lname FROM authors" =“选择au_id,au_fname,从作者的au_lname”
'Data processing here '数据处理这里
Finally最后
Cn.Close () Cn.Close()
End Try最终的结局
Catch ex As Exception作为异常捕捉前
MsgBox (ex.ToString) MsgBox(ex.ToString)
End Try最终的结局
End Sub结束小组
This is what we discussed in front of the code structure, it would open the Connection object and the closure of the operation included in the nested Try block to ensure that no matter what mistakes can close the connection.这就是我们在前面讨论的代码结构,它会打开连接对象和关闭的操作中包含嵌套的try块,以确保无论什么错误可以关闭连接。 Please note that our allocation of a Command object to the use of SQL statements from the authors table in the choice of data.请注意,我们的Command对象的SQL语句从数据选择authors表的使用分配。 Here, we use Command object directly, rather than be embedded in the DataAdapter.在这里,我们使用Command对象直接,而不是在DataAdapter的嵌入。

一步一个脚印...............................默默地前进.....
诚邀乐于解答c菜鸟问题,的热心网友加入,  QQ群38490319
2010-09-26 21:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-213031-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.071257 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved