Microsoft SQL Server 2005 Developer’s Guide- P34 ppt

10 113 0
Microsoft SQL Server 2005 Developer’s Guide- P34 ppt

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

Thông tin tài liệu

Chapter 8: Developing Database Applications with ADO 309 result set. The following code illustrates how you can add rows to a Recordset object that was created using a keyset cursor: Private Sub CursorAdd(cn As ADODB.Connection) Dim rs As New ADODB.Recordset Dim i As Integer 'Pass in the SQL, Connection, Cursor type, lock type and 'source type rs.Open "Select Dep_ID, Dep_Name From Sales.SalesDepartment", _ cn, adOpenKeyset, adLockOptimistic, adCmdText 'Add 50 rows to the Sales.SalesDepartment table ' Note that the Bang ! notation is used to specify column names For i = 1 To 50 rs.AddNew rs!Dep_ID = i rs!Dep_Name = “Department " & CStr(i) rs.Update Next 'Display the new rows in a grid DisplayKeysetGrid rs, Grid, 1 rs.Close End Sub The first parameter of the Recordset object’s Open method accepts a string containing a SQL statement that defines the result set. In this case, the result set consists of the Dep_ID and Dep_Name columns from the Sales.SalesDepartment table created in the earlier dynamic SQL example. The second parameter of the Open method contains the name of an active Connection object named cn. The third parameter uses the constant adOpenKeyset to specify that the Recordset object will use a keyset cursor. The fourth parameter contains the value adLockOptimistic. These two parameters indicate this Recordset object set is updatable and will use optimistic record locking. After the result set has been opened, a For Next loop is used to add 50 rows to the Recordset object. Within the For Next loop, the AddNew method is called to create a row buffer that will contain the new row values. Unlike the earlier examples in this chapter that accessed columns by iterating through the Fields collection, this example illustrates how to access individual columns using the column name and Bang (!) notation. 310 Microsoft SQL Server 2005 Developer’s Guide The value of the Dep_ID column is set using a unique integer value obtained by using the loop counter. The Dep_Name column is set using the string formed by concatenating the literal “Department” and the string representation of the loop counter. After the row values have been set, the Update method is called to add the row to the Recordset object and the data source. Next, the DisplayKeysetGrid subroutine is called, which displays the new row values in a grid. Finally, the Close method is used to close the Recordset object. Updating Rows with the Recordset The Recordset object’s Update method can be used to update rows in an updatable ADO result set. The following code illustrates how you can update the rows in an ADO Recordset object created using a keyset cursor: Private Sub CursorUpdate(cn As ADODB.Connection) Dim rs As New ADODB.Recordset Dim i As Integer Dim sTemp As String ' Pass in SQL, Connection, cursor type, lock type and source type rs.Open "Select Dep_ID, Dep_Name From Sales.SalesDepartment", _ cn, adOpenKeyset, adLockOptimistic, adCmdText Do Until rs.EOF 'Trim off the blanks - ADO doesn’t truncate fixed char data sTemp = Trim(rs!Dep_Name) rs!Dep_Name = "Updated " & sTemp 'Update the row rs.Update rs.MoveNext Loop 'Display the updated rows in a grid DisplayKeysetGrid rs, Grid, 1 rs.Close End Sub Again, the Recordset object’s Open method is used to create a new ADO Recordset object named rs. The first parameter of the Open method accepts a string that specifies the result set. In this case, Recordset object consists of the Dep_ID and the Dep_Name columns from the Sales.SalesDepartment table. An active Connection object named cn is used in the second parameter. The adOpenKeyset and asLockOptimistic constants used in the third and fourth parameters indicate the Recordset object will use an updatable keyset cursor and optimistic record locking. Chapter 8: Developing Database Applications with ADO 311 After the Recordset object set has been created, a Do Until loop reads through all the rows in the Recordset object. The loop ends when the Recordset object’s EOF property turns true. Within the Do loop, the value of the Dep_Name column is set to a new string value that begins with the literal “Updated” concatenated with the current column value. Then the Update method is called to update the row Recordset object, and the MoveNext method positions the cursor to the next row. After all the rows in the Recordset have been updated, the DisplayKeysetGrid function displays the contents of the updated Sales.SalesDepartment table. Finally, the Close method closes the Recordset object. Deleting Rows from a Recordset Object The Recordset object’s Delete method removes rows in an updatable ADO Recordset object. The following code illustrates how you can delete rows in a forward-only type of result set: Private Sub CursorDelete(cn As ADODB.Connection) Dim rs As New ADODB.Recordset 'Pass in the SQL, Connection, cursor type, lock type and source 'type. Note that this is a forward-only cursor but it can update ' the current row. rs.Open "Select Dep_ID, Dep_name From Sales.SalesDepartment", _ cn, adOpenForwardOnly, adLockOptimistic, adCmdText 'Delete all of the rows Do Until rs.EOF rs.Delete rs.MoveNext Loop 'Display the empty Recordset in a grid DisplayForwardGrid rs, Grid rs.Close End Sub As in the previous examples, the Open method is used to create a new ADO Recordset object named rs that contains the Dep_ID and Dep_Name columns from the Sales.SalesDepartment table. The second parameter contains the name of an active Connection object named rs. The third and fourth parameters contain the constants adOpenForwardOnly and adLockOptimistic, which specify the result set will use a forward-only cursor that supports updates using optimistic record locking. 312 Microsoft SQL Server 2005 Developer’s Guide TIP Forward-only record sets are often thought of as read-only because they don’t support the same type of capabilities as keyset cursors. However, forward-only Recordset objects do support updating the current row, and in ADO, they provide much better performance than keyset or dynamic cursors. Any changes made to the data source won’t be reflected in a forward-only Recordset object until it’s refreshed. After the Recordset object has been created, a Do Until loop reads through all the rows contained in the Recordset object. The rs Recordset object’s Delete method deletes each row, and the MoveNext method positions the cursor on the next row in the result set. After all the rows have been deleted, the DisplayForwardGrid subroutine displays the (now empty) Sales.SalesDepartment table. Finally, the Close method closes the Recordset object. Updating Data with the ADO Command Object The preceding section showed how to update SQL Server databases using Recordset objects and cursors. However, while updating data using Recordset objects is easy to code, this method isn’t usually optimal in terms of performance. Using prepared SQL statements to update data usually provides better performance—especially in OLTP-type applications where the SQL statements have a high degree of reuse. Next, you see how you can use prepared SQL statements and the ADO Command object’s Execute method to insert, update, and delete data in a SQL Server table. Inserting Rows with a Command Object and Prepared SQL The SQL Insert statement adds rows to a table. The following example illustrates how to use the SQL Insert statement with an ADO Command object: Private Sub PreparedAdd(cn As ADODB.Connection) Dim cmd As New ADODB.Command Dim rs As New ADODB.Recordset Dim i As Integer 'Set up the Command object's Connection, SQL and parameter types With cmd .ActiveConnection = cn .CommandText = "Insert Into Sales.SalesDepartment Values(?,?) " .CreateParameter , adChar, adParamInput, 4 .CreateParameter , adChar, adParamInput, 25 End With Chapter 8: Developing Database Applications with ADO 313 'Execute the prepared SQL statement to add 50 rows For i = 1 To 50 cmd.Parameters(0) = CStr(i) cmd.Parameters(1) = "Department " & CStr(i) cmd.Execute , , adExecuteNoRecords Next 'Create a recordset to display the new rows rs.Open "Select * From Sales.SalesDepartment", cn, , , adCmdText DisplayForwardGrid rs, Grid rs.Close End Sub In this example, you create new ADO Command and Recordset objects. Then the ActiveConnection property of the Command object receives the name of an active Connection object named cn. Next, the CommandText property is assigned a SQL Insert statement that uses two parameter markers. The CreateParameter method is then used to specify the characteristics of each parameter. The first parameter contains a character value that is 4 bytes long, and the second parameter contains a character value that is 25 bytes long. As you would expect with an Insert statement, both parameters are input-only. TIP While this example simply refers to each parameter using its ordinal position within the Parameters collection, you can also name each parameter when it’s created. Naming the parameters lets you refer to them in almost the same way as working with the Field objects contained in a Recordset. For instance, you can create a named parameter as follows: cmd.CreateParameter "Dep_ID" , adChar, adParamInput, 4 You could then refer to the parameter as: cmd.Paramters("Dep_ID") = CStr(i). A For Next loop adds 50 rows to the table. Within the For Next loop, the values used by each parameter are assigned. The cmd.Parameter(0) object refers to the first parameter marker, while the cmd.Parameter(1) object refers to the second parameter marker. As in the earlier example that added rows using a cursor, the first parameter (the Dep_ID column) has a unique integer value based on the loop counter. The second parameter (the Dep_Name column) has a string that contains the literal “Department” in conjunction with a string representation of the loop counter. After you set the parameter values, the prepared statement executes using the Execute method. 314 Microsoft SQL Server 2005 Developer’s Guide The adExecuteNoRecords option specifies that the Execute method will not return a Recordset. The DisplayForwardGrid subroutine displays the contents of the Sales. SalesDepartment table in a grid, and then the Recordset closes. Updating Data with a Command Object and a Prepared SQL The SQL Update statement updates columns in a table. The following example illustrates using the SQL Update statement with an ADO Command object to update all the rows in the Sales. SalesDepartment table: Private Sub PreparedUpdate(cn As ADODB.Connection) Dim cmd As New ADODB.Command Dim rs As New ADODB.Recordset Dim i As Integer 'Set up the Command object’s Connection, SQL and parameter types With cmd .ActiveConnection = cn .CommandText = _ "Update Sales.SalesDepartment Set Dep_Name = ? Where Dep_ID = ?" .CreateParameter , adChar, adParamInput, 25 .CreateParameter , adChar, adParamInput, 4 End With ' Execute the prepared SQL statement to update 50 rows For i = 0 To 50 cmd.Parameters(0).Value = "Updated Department " & CStr(i) cmd.Parameters(1).Value = CStr(i) cmd.Execute , , adExecuteNoRecords Next ' Create a recordset to display the updated rows rs.Open "Select * From Sales.SalesDepartment", cn, , , adCmdText DisplayForwardGrid rs, Grid rs.Close End Sub As in the previous insert example, new ADO Command and Recordset objects are created in the beginning of the subroutine. The ActiveConnection property method of the Command object has the name of an active Connection object named cn. Here, the CommandText property has a SQL Update statement that uses two parameter markers. In this case, the first parameter refers to the Dep_Name column, Chapter 8: Developing Database Applications with ADO 315 and the second parameter refers to the Dep_ID column. Then the CreateParameter method specifies the characteristics of each parameter. A For Next loop updates each of the 50 rows in the Sales.SalesDepartment table. Within the For Next loop, the values used by each parameter are assigned and the Update statement is run using the Command object’s Execute method. After the updates are finished, a Recordset object is created and displayed in a grid using the DisplayForwardGrid subroutine. Deleting Data with a Command Object and Prepared SQL As with Insert and Update operations, ADO Command objects can be used to delete one or more rows in a remote data source. The following code listing illustrates how to delete rows from a SQL Server database using a prepared SQL Delete statement and a Command object: Private Sub PreparedDelete(cn As ADODB.Connection) Dim cmd As New ADODB.Command Dim rs As New ADODB.Recordset Dim i As Integer 'Set up the Command object's Connection and SQL command With cmd .ActiveConnection = cn .CommandText = "Delete Sales.SalesDepartment" End With 'Execute the SQL once (that's all that is needed) cmd.Execute , , adExecuteNoRecords 'Create a recordset to display the empty table rs.Open "Select * From Sales.SalesDepartment", cn, , , adCmdText DisplayForwardGrid rs, Grid rs.Close End Sub Thanks to SQL’s set-at-time functionality, this example is a bit simpler than the previous insert and update examples. SQL’s capability to manipulate multiple rows with a single statement allows one SQL Update to be used to update all 50 rows in the table. As in those examples, first new ADO Command and Recordset objects are created, and then the ActiveConnection property method of the Command object gets the name of an active Connection object. Next, a SQL statement is assigned to the Command object’s CommandText property. In this case, the SQL Delete statement doesn’t use any parameters. Because no Where clause is contained 316 Microsoft SQL Server 2005 Developer’s Guide in this statement, the Delete operation is performed on all rows in the Sales. SalesDepartment table when the Execute method is run. NOTE Use caution when you employ SQL action statements without a Where clause. This powerful technique can easily and inadvertently modify more rows than you intend. After the updates are finished, a Recordset object is created and displayed in a grid using the DisplayForwardGrid subroutine, and then the Recordset object is closed. Executing Stored Procedures with Command Objects Stored procedures provide the fastest mechanism available for accessing SQL Server data. When a stored procedure is created, a compiled data access plan is added to the SQL Server database. By using this existing data access plan, the application foregoes the need to parse any incoming SQL statements, and then creates a new data access plan. This results in faster execution of queries or other data manipulation actions. SQL Server automatically shares stored procedures among multiple users. Stored procedures can also be used to implement a more robust database security than you can achieve by setting permissions directly on target files. For example, you can restrict all direct access to SQL Server tables and only permit access to the stored procedures. When centrally controlled and administered, the stored procedures can provide complete control over SQL Server database access. Using ADO, stored procedures are called in much the same way as are prepared SQL statements. The Command object calls the stored procedure, and a question mark denotes each stored procedure’s input and output parameters. The following example is a simple stored procedure that accepts one input parameter and returns one output parameter: Create Procedure CountOrderQty ( @SalesOrderID Char(4), @OrderQty int Output ) As Select @OrderQty = Select Sum(OrderQty) From Sales.SalesOrderDetail Where SalesOrderID = @SalesOrderID GO Chapter 8: Developing Database Applications with ADO 317 The CountOrderQty stored procedure in this example accepts a character argument containing the SalesOrderID as input and returns an integer value containing the total of the OrderQty column for all the rows in the sales table that matched the supplied SalesOrderID. In this example, the SQL Select sum() function is used to sum up the values contained in the OrderQty column. NOTE The variable names used in the stored procedure don’t need to match the column names in the source table. The following code example shows how you can call the CountOrderQty stored procedure using an ADO Command object: Private Sub CallSP(cn As ADODB.Connection) Dim cmd As New ADODB.Command Dim parm0 As New ADODB.Parameter Dim parm1 As New ADODB.Parameter Dim sSQL As String On Error GoTo ErrorHandler cmd.ActiveConnection = cn cmd.CommandType = adCmdStoredProc cmd.CommandText = "CountOrderQty" parm0.Direction = adParamInput parm0.Type = adInteger parm0.Size = 4 cmd.Parameters.Append parm0 parm1.Direction = adParamOutput parm1.Type = adInteger parm1.Size = 4 cmd.Parameters.Append parm1 parm0.Value = 43675 cmd.Execute Label_Mid.Caption = " Total Qty for Sales Order 43675: " Text_Mid.Text = parm1.Value ErrorHandler: DisplayADOError cn End Sub 318 Microsoft SQL Server 2005 Developer’s Guide In the beginning of this subroutine, you can see where an ADO Command object named cmd and two ADO Parameter objects named parm0 and parm1 are created. Using Parameter objects is an alternative to using the CreateParameter method illustrated earlier in this chapter, in the section “Using Prepared SQL and the Command Object.” Both techniques can be used to specify the characteristics of a parameter marker, and either method can be used to execute prepared SQL, as well as stored procedures. Next, the ActiveConnection property of the Command object is assigned the name of an existing Connection object named cn. This associates the Command object with a target data source. Then the Command object’s CommandType property is assigned the value of adCmdStoredProc, and the CommandText property is assigned the name of the stored procedure to be executed. Because the CommandType property tells ADO this Command object is used to call a stored procedure, no need exists to set up a SQL string that contains an ODBC Call statement. The next section of code shows how Parameter objects are initialized. For each Parameter object, the Direction, Type, and Size properties are set. Then the Append method of the Parameters collection is used to add the Parameter object to the Parameters collection. NOTE You must add each Parameter object to the Parameters collection in the same order as the parameter is used by the stored procedure or prepared SQL statement. In other words, you must use the Append method for the first Parameter object, which represents the first parameter, before you execute the Append method for the second Parameter object, which represents the second parameter. After the Parameter objects have been added to the Command object’s Parameters collection, the Value property of the first parameter is assigned a string that contains a valid SalesOrderID value. This value is passed to the first parameter of the CountOrderQty stored procedure. Then the Command object’s Execute method is used to call the stored procedure. When the call to the stored procedure has completed, the value of the output parameter is available in the Value property of the second Parameter object (parm1). In the previous example, this value is assigned to a text box to be displayed. Error Handling Run-time errors that are generated using the ADO object framework are placed in the ADO Errors collection. When an ADO run-time error occurs, Visual Basic’s . how to access individual columns using the column name and Bang (!) notation. 310 Microsoft SQL Server 2005 Developer’s Guide The value of the Dep_ID column is set using a unique integer value. use a forward-only cursor that supports updates using optimistic record locking. 312 Microsoft SQL Server 2005 Developer’s Guide TIP Forward-only record sets are often thought of as read-only because. prepared SQL statements and the ADO Command object’s Execute method to insert, update, and delete data in a SQL Server table. Inserting Rows with a Command Object and Prepared SQL The SQL Insert

Ngày đăng: 05/07/2014, 05:20

Mục lục

  • Contents

  • Acknowledgments

  • Introduction

  • Chapter 1 The Development Environment

    • SQL Server Management Studio

      • The SQL Server Management Studio User Interface

      • SQL Server Management Studio User Interface Windows

      • SQL Server 2005 Administrative Tools

      • BI Development Studio

        • The Business Intelligence Development Studio User Interface

        • BI Development Studio User Interface Windows

        • Summary

        • Chapter 2 Developing with T-SQL

          • T-SQL Development Tools

            • SQL Server Management Studio

            • Visual Studio 2005

            • Creating Database Objects Using T-SQL DDL

              • Databases

              • Tables

              • Views

              • Synonyms

              • Stored Procedures

              • Functions

              • Triggers

              • Security

              • Storage for Searching

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan