1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Executing Batch Updates with ADO and SQL Server doc

3 347 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Executing Batch Updates with ADO and SQL Server
Định dạng
Số trang 3
Dung lượng 10,43 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Executing Batch Updates with ADO and SQL Server The biggest difference between this section and the previous one is the fact that in the last section, a recordset being populated using a

Trang 1

Executing Batch Updates with ADO and SQL Server

The biggest difference between this section and the previous one is the fact that in the last section, a recordset being populated using a Command object With the Command object,

no data was affected That is one type of use for a Command object What if you want to perform bulk tasks against a recordset? This is the major use for using the Command object because you don't necessarily need it for populating a recordset The Parameter object is also not used It was not necessary for the example, although you would want to use it if criteria were being implemented

To learn how to perform a bulk operation using ADO in Visual Basic NET, see this simple Update SQL statement, shown here:

Update Orders Set ShippedDate = DeliveryDate+1

This statement adds a day to the date in the ShippedDate column for all the records in the Orders table

This statement is being assigned to the CommandText property of the Command object instead of to the name of a stored procedure Another important task is setting the

CommandType property to be ADODB.CommandTypeEnum.adCmdText This tells ADO that you are performing a bulk operation Last, the Execute method is called from the Command object This routine, called ExecuteABatchCommand, can be seen in Listing A.9

Listing A.9 basCommandExamples.vb: Creating and Executing a Bulk Query

Sub ExecuteABatchCommand(ByVal txtResults As TextBox)

Dim cnn As New ADODB.Connection()

Dim cmd As New ADODB.Command()

Dim prm As ADODB.Parameter

Dim rstOld As New ADODB.Recordset()

Dim rstNew As New ADODB.Recordset()

' In NET, we can assign values as we declare variables

Dim strSQL As String = "Update Orders Set ShippedDate = ShippedDate+1"

Dim strDispSQL As String = _

"Select OrderID, ShippedDate From Orders Where OrderID < 10251"

' Open the connection

OpenNorthwindADOConnection(cnn)

Trang 2

' Open the Orders table and display the ShippedDate as they were

rstOld.Open(strDispSQL, cnn)

txtResults.Text = "Old Values: " & vbCrLf & vbCrLf & rstOld.GetString

' Set up the Command object to use the SQL string

cmd.ActiveConnection = cnn

cmd.CommandText = strSQL

cmd.CommandType = ADODB.CommandTypeEnum.adCmdText

' Execute the command

cmd.Execute()

' Reopen the Orders table and redisplay the ShippedDate Field

rstNew.Open(strDispSQL, cnn)

txtResults.Text = txtResults.Text & vbCrLf & vbCrLf & "New Values: " &

vbCrLf & vbCrLf & rstNew.GetString

End Sub

For this example recordset, objects were used merely to display the before and after data,

as seen in Figure A.6

Figure A.6 Although they're not pretty, you can see the values of the OrderID and

DeliveryDate before and after the routine has been executed

Trang 3

You can also use the Insert and Delete statements to perform other bulk operations with the Command object One of the last tasks that is useful to perform using ADO with SQL Server is to create objects such as tables on-the-fly

Ngày đăng: 21/01/2014, 12:20

TỪ KHÓA LIÊN QUAN