SQL Server™ 2005: CLR Integration Table of Contents

SQL Server™ 2005: CLR Integration Table of Contents

SQL Server™ 2005: CLR Integration Table of Contents SQL Server™ 2005: SQL CLR Integration...................................................................................................................3 Lab Setup.......................................................................................................................................................................4 Exercise 1 Working with SQL Server Projects..............................................................................................................5 Exercise 2 Leveraging the .NET Framework in Database Development ....................................................................10 Exercise 3 Understanding Managed Code Permissions...............................................................................................13 Exercise 4 Creating User-Defined Types and User-Defined Aggregates....................................................................17 Exercise 5 Comparing and Contrasting T-SQL and Managed Code ...........................................................................32 Exercise 6 Exploring New Features in ADO.NET ......................................................................................................35 SQL Server™ 2005: CLR Integration SQL Server™ 2005: SQL CLR Integration After completing this lab, you will be able to work with SQL Objectives Server Projects, work with the in-process managed provider, NOTE: This lab focuses on the leverage the .NET Framework in database development, concepts in this module and as a understand managed code permissions, create triggers using result may not comply with managed code, create user-defined types and user-defined Microsoft® security aggregates, compare and Contrast T-SQL to managed code, recommendations. and explore new features in ADO.NET. NOTE: The SQL Server 2005 labs are based on beta builds of the product. The intent of these labs is to provide you with a general feel for some of the planned features in the next release of SQL Server. As with all software development projects, the final version may differ from beta builds in both features and user interface. For the latest details on SQL Server 2005, please visit http://www.microsoft.com/sql/2005/. Scenario Prerequisites 90 Minutes Estimated Time to Complete This Lab Page 3 of 38 SQL Server™ 2005: CLR Integration Lab Setup Scenario Tasks Detailed Steps Task 1: Log In Log in using the Administrator user account. The password is Pass@word1. Page 4 of 38 SQL Server™ 2005: CLR Integration Exercise 1 Working with SQL Server Projects In this exercise, you will learn how to work with Microsoft Visual Studio® 2005 and Microsoft SQL Server 2005 in tandem by creating a SQL Server project and a basic user-defined function whose implementation is in managed code. Then you will build a Microsoft Windows® Form application that displays the result returned from the user-defined function. The goal of this exercise is to lay a foundation for subsequent exercises by introducing how Visual Studio 2005 and Microsoft SQL Server 2005 work together. NOTE: Long code snippets are available in the file C:\SQL Labs\Lab Projects\SQL CLR\SQL CLR Lab Code.txt for your convenience. Tasks Detailed Steps Task 1: Creating a 1. Start Visual Studio 2005. SQL Server 2. Select the File | New | Project menu command. Project Visual Studio displays the New Project dialog. 1. In the Project Types pane, indicate that you want to create a Visual Basic project. Expand the node for Visual Basic and select the Database node. 2. In the Templates pane, select SQL Server Project. 3. In the Name field, enter SqlServerProject. 4. In the Location field, enter C:\SQL Labs\User Projects\. 5. Uncheck the Create directory for solution option. 6. Click OK. Visual Studio displays the New Database Reference dialog box (as shown in Figure 1). Page 5 of 38 SQL Server™ 2005: CLR Integration Tasks Detailed Steps Figure 1: The New Database Reference dialog box A SQL Server project is associated with a specific database. This dialog box lets you select the database to which you will be deploying managed code. Database References are shown in the Solution Explorer and become part of the Visual Studio 2005 environment. As you open and close different projects, Database References are saved so they can be reused. Task 2: Creating a 1. In the server name field, enter “localhost”. Database 2. Select Use Windows NT Integrated security. Reference 3. In the database field, enter “SQLCLRIntegration”. 4. Click the Test Connection button. 5. Click OK to dismiss the Add Database Reference dialog box. Upon clicking OK, a dialog box like Figure 2 will be displayed: Figure 2: Debugging Warning 6. Click Yes so that debugging will be enabled. Task 3: Using a User- 1. In the Solution Explorer, right-click SqlServerProject and select Add | New Item from the context menu. Defined Function Template 2. In the Add New Item dialog box, click User-Defined Function. 3. Click Add to accept the default file name. In SQL Server, a user-defined function is similar to a function in other programming languages. Unlike a stored procedure, which can return only an integer or result set, a user-defined function can return a variety of data types. You also can treat a user-defined function similarly to a table or view, issuing SELECT statements directly against the function. In prior versions of SQL Server, you could only create a user-defined function using T-SQL. With Microsoft SQL Server 2005, you can now create user-defined functions using managed code. Visual Studio includes a pre-defined template to use in your SQL Server Projects to make the process easier. 4. In the Solution Explorer, open the TestScripts folder, right-click test.sql and select Open. Notice that when you used the User-Defined Function template, Visual Studio automatically added an additional file to your project named test.sql. You can use the script in test.sql for debugging your code. You modify this script to call your code so that it can be debugged. Page 6 of 38 SQL Server™ 2005: CLR Integration Tasks Detailed Steps 5. In the Solution Explorer, right-click Function1.vb and select View Code. You’ll notice that an example function has already been defined. You can remove it or leave it. 6. In the Code Editor, before the End Class statement, type the following code: <Microsoft.SqlServer.Server.SqlFunction()> _ Public Shared Function GetTodaysDate As Date Return Date.Today End Function 7. In the Solution Explorer, right-click test.sql and select Open. 8. Replace the line select 'put your test script here' (located at the bottom of the file) with the following text: select dbo.GetTodaysDate() Task 4: Using a User- 1. In the Solution Explorer, right-click SqlServerProject and select Deploy. Defined Function Before an assembly can be used by a database, it must first be deployed Template to the database server. Visual Studio 2005 makes deploying an assembly to Microsoft SQL Server 2005 easy to do. Just by right-clicking the project name in the Solution Explorer and selecting the Deploy menu item, Visual Studio 2005 automatically builds and deploys your assembly to SQL Server 2005. The assembly is also deployed and debugged simply by pressing F5. 2. Once the deployment process has completed, press F5. However, there are two problems. The first is that the code just appears to execute and return. In order to debug, place a breakpoint in the Function1.vb file where your function is defined. 3. Next, in order to get managed code to run, you need to enable the CLR for the database. From the Windows task bar, select Start | All Programs | Microsoft SQL Server 2005 CTP | SQL Server Management Studio. 4. When the Connect to Server dialog box opens, verify that SQL Server is selected as the Server type, verify that Server name is same name as the host machine or set it to localhost, and verify that Windows Authentication is selected as the authentication method. 5. Click Connect. 6. Open a new query window by via File | New | New Database Engine Query. When prompted, click Connect in the Connect to Database Engine dialog box. 7. In the editor, enter the following T-SQL: sp_configure 'clr enabled', 1 8. Press F5 to execute the query. Then enter the following: reconfigure; Page 7 of 38 SQL Server™ 2005: CLR Integration Tasks Detailed Steps 9. Highlight the new line and press F5. 10. Close SQL Server Management Studio (there’s no need to save anything if prompted). 11. Now, back in Visual Studio, Press F5. 12. Once you hit your breakpoint, press F5 to continue. 13. In the Output window, in the Show output from drop-down list, select Debug. You can see that the output from the user-defined function is displayed in the Output window, allowing you to fully test your managed routines without leaving Visual Studio 2005. Note that you can make it easier to see your output by right-clicking in the Output window and de-selecting all of the output options except for Program Output (see Figure 3). If you re-run your project, you see much less information displayed. Figure 3: Controlling output window information Task 5: Accessing a 1. Select the File | Add | New Project menu command. User-Defined 2. If prompted to save the existing project, select the C:\SQL Labs\User Projects Function from a folder. Windows 3. In the Project Types pane, select Visual Basic. Application 4. In the Templates list, select Windows Application. 5. In the Name field, enter: SqlServerProjectWinApp. In the Location field, enter NOTE: If the Toolbox C:\SQL Labs\User Projects\. window isn’t visible, 6. Click OK to dismiss the dialog box. select View | Toolbox to display it. 7. Right-click on SqlServerProjectWinApp in the Solution Explorer and select Project | Add Reference. Scroll the list of components until you find NOTE: You might need System.Data. Select it and then click OK to accept the new reference. to expand the Common 8. From the Toolbox, drag a Button control to the design surface of Form1. Controls group to see 9. Double-click Button1. the Button control on the Toolbox. 10. Scroll to the top of the code file, and enter the following Imports statement: Imports System.Data.SqlClient 11.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    38 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us