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

Microsoft SQL Server 2005 Developer’s Guide- P11 ppsx

10 235 0

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

THÔNG TIN TÀI LIỆU

Cấu trú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

    • Querying and Updating with T-SQL DML

      • Select and Joins

      • Modifying Data

      • Error Handling

    • Summary

  • Chapter 3 Developing CLR Database Objects

    • Understanding CLR and SQL Server 2005 Database Engine

      • CLR Architecture

      • Enabling CLR Support

      • CLR Database Object Components

    • Creating CLR Database Objects

      • CLR Stored Procedures

      • User-Defined Functions

      • Triggers

      • User-Defined Types

      • Aggregates

    • Debugging CLR Database Objects

      • .NET Database Object Security

      • Managing CLR Database Objects

    • Summary

  • Chapter 4 SQL Server Service Broker

    • SQL Server Service Broker Architecture

      • Messages

      • Queues

      • Contracts

      • Services

      • Dialogs

    • Developing SQL Service Broker Applications

      • SQL Server Service Broker DDL and DML

      • T-SQL DDL

      • T-SQL DML

      • Enabling SQL Server Broker

      • Using Queues

      • Sample SQL Server Service Broker Application

    • SQL Server Service Broker Activation

    • Dialog Security

    • System Views

    • Summary

  • Chapter 5 Developing with Notification Services

    • Notification Services Overview

      • Events

      • Subscriptions

      • Notifications

    • Developing Notification Services Applications

      • Defining the Application

      • Compiling the Application

      • Building the Notification Subscription Management Application

      • Adding Custom Components

    • Notification Services Application Sample

      • Creating the ICF File

      • Defining the ADF File

      • Building the Notification Services Application

    • Updating Notification Services Applications

    • Building a .NET Subscription/Event Application

      • Listing Subscriptions

      • Adding Subscriptions

      • Deleting Subscriptions

      • Firing the Data Event Using .NET

      • Firing the Data Event Using T-SQL

    • Summary

  • Chapter 6 Developing Database Applications with ADO.NET

    • The ADO.NET Architecture

    • ADO.NET Namespaces

    • .NET Data Providers

      • Namespaces for the .NET Data Providers

      • Core Classes for the .NET Data Providers

    • Core Classes in the ADO.NET System.Data Namespace

      • DataSet

      • DataTable

      • DataColumn

      • DataRow

      • DataView

      • DataViewManager

      • DataRelation

      • Constraint

      • ForeignKeyConstraint

      • UniqueConstraint

      • DataException

    • Using the .NET Framework Data Provider for SQL Server

      • Adding the System.Data.SqlClient Namespace

    • Using the SqlConnection Object

      • The .NET Framework Data Provider for SQL Server Connection String Keywords

      • Opening a Trusted Connection

      • Using Connection Pooling

    • Using the SqlCommand Object

      • Executing Dynamic SQL Statements

      • Executing Parameterized SQL Statements

      • Executing Stored Procedures with Return Values

      • Executing Transactions

    • Using the SqlDependency Object

    • Using the SqlDataReader Object

      • Retrieving a Fast Forward–Only Result Set

      • Reading Schema-Only Information

      • Asynchronous Support

      • Multiple Active Result Sets (MARS)

      • Retrieving BLOB Data

    • Using the SqlDataAdapter Object

      • Populating the DataSet

      • Using the CommandBuilder Class

    • Summary

  • Chapter 7 Developing with XML

    • The XML Data Type

      • Data Validation Using an XSD Schema

    • XQuery Support

      • Querying Element Data

    • XML Data Type Methods

      • Exist(XQuery)

      • Modify(XML DML)

      • Query(XQuery)

      • Value(XQuery, [node ref])

    • XML Indexes

      • Primary XML Indexes

      • Secondary XML Indexes

    • Using the For XML Clause

      • For XML Raw

      • For XML Auto

      • For XML Explicit

      • Type Mode

      • FOR XML Path

      • Nested FOR XML Queries

      • Inline XSD Schema Generation

    • OPENXML

    • XML Bulk Load

    • Native HTTP SOAP Access

      • Creating SOAP Endpoints

      • Using SOAP Endpoints

    • Summary

  • Chapter 8 Developing Database Applications with ADO

    • An Overview of OLE DB

    • OLE DB Architecture Overview

    • ADO (ActiveX Data Objects)

    • OLE DB and ADO Files

    • ADO Architecture

      • An Overview of Using ADO

    • Adding the ADO Reference to Visual Basic

    • Using ADO Objects with Visual Basic

      • Connecting to SQL Server

      • Retrieving Data with the ADO Recordset

      • Executing Dynamic SQL with the ADO Connection Object

      • Modifying Data with ADO

      • Executing Stored Procedures with Command Objects

      • Error Handling

    • Advanced Database Functions Using ADO

      • Batch Updates

      • Using Transactions

    • Summary

  • Chapter 9 Reporting Services

    • Reporting Services Architecture

      • Reporting Services Components

      • Installing Reporting Services

    • Report Server

      • Report Server Processors

      • Report Server Extensions

    • Report Manager

    • Reporting Services Configuration and Management Tools

      • Reporting Services Configuration Tool

      • Report Server Command-Prompt Utilities

    • Report Authoring Tools

      • Report Designer

      • Report Model Designer

      • Report Builder

    • Programmability

      • Using URL Access in a Window Form

      • Integrating Reporting Services Using SOAP

      • Extensions

      • RDL

    • Accessing Reports

      • Using URL Access

      • URL Access Through a Form POST Method

    • Report Authoring

      • Development Stages

      • Creating a Reporting Services Report

      • Deploying a Reporting Services Report

      • Running a Reporting Services Report

    • Summary

  • Chapter 10 SQL Server Integration Services

    • An Overview of SQL Server Integration Services

      • Data Transformation Pipeline (DTP)

      • Data Transformation Runtime (DTR)

    • Creating Packages

      • Using the SSIS Import and Export Wizard

      • Using the SSIS Designer

      • Using Breakpoints

      • Using Checkpoints

      • Using Transactions

      • Package Security

    • Deploying Packages

      • Creating Configurations

      • Using the Package Deployment Utility

    • Programming with the SQL Server Integration Services APIs

    • Summary

  • Chapter 11 Developing BI Applications with ADOMD.NET

    • Analysis Services Overview

      • XML for Analysis

      • Analysis Management Objects (AMO) Overview

      • ADOMD.NET Overview

      • AMO Hierarchy

      • ADOMD.NET Object Model

    • Building a BI Application with ADOMD.NET

      • Adding a Reference for ADOMD.NET

      • Using the AdomdConnection Object

      • Using the AdomdCommand Object

      • Using the AdomdDataAdapter Object

      • Using the CubeDef Object

    • Summary

  • Chapter 12 Developing with SMO

    • Using SMO

      • Adding SMO Objects to Visual Studio

      • Creating the Server Object

      • Using SMO Properties

      • SMO Property Collections

    • SMO Hierarchy

    • Building the SMO Sample Application

      • Creating the Server Object

      • Listing the Registered SQL Systems

      • Connecting to the Selected SQL Server System

      • Listing Databases

      • Listing Tables

      • Listing Columns

      • Retrieving Column Attributes

      • Creating Databases

      • Transferring Tables

      • Showing T-SQL Script for Tables

      • SMO Error Handling

    • Summary

  • Chapter 13 Using sqlcmd

    • sqlcmd Components

      • Command Shell

      • Command-Line Parameters

      • sqlcmd Extended Commands

      • sqlcmd Variables

    • Developing sqlcmd Scripts

      • Developing sqlcmd Scripts with Query Editor

      • Using sqlcmd Variables

      • Using sqlcmd Script Nesting

      • Using sqlcmd Variables and T-SQL Statements

    • Summary

  • Appendix: SQL Profiler

    • Starting SQL Profiler

    • Starting, Pausing, and Stopping a Trace

    • Replaying a Trace

    • Showplan Events

  • Index

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    • H

    • I

    • J

    • L

    • M

    • N

    • O

    • P

    • Q

    • R

    • S

    • T

    • U

    • V

    • W

    • X

Nội dung

Chapter 3: Developing CLR Database Objects 79 CLR Architecture The .NET Framework CLR is very tightly integrated with the SQL Server 2005 database engine. In fact, the SQL Server database engine hosts the CLR. This tight level of integration gives SQL Server 2005 several distinct advantages over the .NET integration that’s provided by DB2 and Oracle. You can see an overview of the SQL Server 2005 database engine and CLR integration in Figure 3-1. As you can see in Figure 3-1, the CLR is hosted within the SQL Server database engine. A SQL Server database uses a special API or hosting layer to communicate with the CLR and interface the CLR with the Windows operating system. Hosting the CLR within the SQL Server database gives the SQL Server database engine the ability to control several important aspects of the CLR, including ᭤ Memory management ᭤ Threading ᭤ Garbage collection The DB2 and Oracle implementation both use the CLR as an external process, which means that the CLR and the database engine both compete for system resources. SQL Server 2005’s in-process hosting of the CLR provides several important advantages over the external implementation used by Oracle or DB2. First, in-process hosting enables SQL Server to control the execution of the CLR, putting SQL Server Engine SQL Server OS Windows CLR Hosting Layer Figure 3-1 The SQL Server CLR database architecture 80 Microsoft SQL Server 2005 Developer’s Guide essential functions such as memory management, garbage collection, and threading under the control of the SQL Server database engine. In an external implementation the CLR will manage these things independently. The database engine has a better view of the system requirements as a whole and can manage memory and threads better than the CLR can do on its own. In the end, hosting the CLR in-process will provide better performance and scalability. Enabling CLR Support By default, the CLR support in the SQL Server database engine is turned off. This ensures that update installations of SQL Server do not unintentionally introduce new functionality without the explicit involvement of the administrator. To enable SQL Server’s CLR support, you need to use the advanced options of SQL Server’s sp_configure system stored procedure, as shown in the following listing: sp_configure 'show advanced options', 1 GO RECONFIGURE GO sp_configure 'clr enabled', 1 GO RECONFIGURE GO CLR Database Object Components To create .NET database objects, you start by writing managed code in any one of the .NET languages, such as VB, C#, or Managed C++, and compile it into a .NET DLL (dynamic link library). The most common way to do this would be to use Visual Studio 2005 to create a new SQL Server project and then build that project, which creates the DLL. Alternatively, you create the .NET code using your editor of choice and then compiling the code into a .NET DLL using the .NET Framework SDK. ADO.NET is the middleware that connects the CLR DLL to the SQL Server database. Once the .NET DLL has been created, you need to register that DLL with SQL Server, creating a new SQL Server database object called an assembly. The assembly essentially encapsulates the .NET DLL. You then create a new database object such as a stored procedure or a trigger that points to the SQL Server assembly. You can see an overview of the process to create a CLR database object in Figure 3-2. Chapter 3: Developing CLR Database Objects 81 SQL Server .NET Data Provider If you’re familiar with ADO.NET, you may wonder exactly how CLR database objects connect to the database. After all, ADO.NET makes its database connection using client-based .NET data providers such as the .NET Framework Data Provider for SQL Server, which connects using networked libraries. While that’s great for a client application, going through the system’s networking support for a database call isn’t the most efficient mode for code that’s running directly on the server. To address this issue, Microsoft created the new SQL Server .NET Data Provider. The SQL Server .NET Data Provider establishes an in-memory connection to the SQL Server database. Assemblies After the coding for the CLR object has been completed, you can use that code to create a SQL Server assembly. If you’re using Visual Studio 2005, then you can simply select the Deploy option, which will take care of both creating the SQL Server assembly as well as creating the target database object. If you’re not using Visual Studio 2005 or you want to perform the deployment process manually, then you need to copy the .NET DLL to a common storage location of your choice. Then, using SQL Server Management Studio, you can execute a T-SQL CREATE ASSEMBLY statement that references the location of the .NET DLL, as you can see in the following listing: CREATE ASSEMBLY MyCLRDLL FROM '\\SERVERNAME\CodeLibrary\MyCLRDLL.dll' Code database object using managed code and complie to DLL Register DLL with SOL Server using T-SQL create assembly Create database object using T-SQL Create Figure 3-2 Creating CLR database objects 82 Microsoft SQL Server 2005 Developer’s Guide The CREATE ASSEMBLY command takes a parameter that contains the path to the DLL that will be loaded into SQL Server. This can be a local path, but more often it will be a path to a networked file share. When the CREATE ASSEMBLY is executed, the DLL is copied into the master database. If an assembly is updated or becomes deprecated, then you can remove the assembly using the DROP ASSEMBLY command as follows: DROP ASSEMBLY MyCLRDLL Because assemblies are stored in the database, when the source code for that assembly is modified and the assembly is recompiled, the assembly must first be dropped from the database using the DROP ASSEMBLY command and then reloaded using the CREATE ASSEMBLY command before the updates will be reflected in the SQL Server database objects. You can use the sys.assemblies view to view the assemblies that have been added to SQL Server 2005 as shown here: SELECT * FROM sys.assemblies Since assemblies are created using external files, you may also want to view the files that were used to create those assemblies. You can do that using the sys. assembly_files view as shown here: SELECT * FROM sys.assembly_files Creating CLR Database Objects After the SQL Server assembly is created, you can then use SQL Server Management Studio to execute a T-SQL CREATE PROCEDURE, CREATE TRIGGER, CREATE FUNCTION, CREATE TYPE, or CREATE AGGREGATE statement that uses the EXTERNAL NAME clause to point to the assembly that you created earlier. When the assembly is created, the DLL is copied into the target SQL Server database and the assembly is registered. The following code illustrates creating the MyCLRProc stored procedure that uses the MyCLRDLL assembly: CREATE PROCEDURE MyCLRProc AS EXTERNAL NAME MyCLRDLL.StoredProcedures.MyCLRProc The EXTERNAL NAME clause is new to SQL Server 2005. Here the EXTERNAL NAME clause specifies that the stored procedure MyCLRProc will Chapter 3: Developing CLR Database Objects 83 be created using a .SQL Server assembly. The DLL that is encapsulated in the SQL Server assembly can contain multiple classes and methods; the EXTERNAL NAME statement uses the following syntax to identify the correct class and method to use from the assembly: Assembly Name.ClassName.MethodName In the case of the preceding example, the registered assembly is named MyCLRDLL. The class within the assembly is StoredProcedures, and the method within that class that will be executed is MyCLRProc. Specific examples showing how you actually go about creating a new managed code project with Visual Studio 2005 are presented in the next section. Creating CLR Database Objects The preceding section presented an overview of the process along with some example manual CLR database object creation steps to help you better understand the creation and deployment process for CLR database objects. However, while it’s possible to create CLR database objects manually, that’s definitely not the most productive method. The Visual Studio 2005 Professional, Enterprise, and Team System Editions all have tools that help create CLR database objects as well as deploy and debug them. In the next part of this chapter you’ll see how to create each of the new CLR database objects using Visual Studio 2005. NOTE The creation of SQL Server projects is supported in Visual Studio 2005 Professional Edition and higher. It is not present in Visual Studio Standard Edition or the earlier releases of Visual Studio. CLR Stored Procedures Stored procedures are one of the most common database objects that you’ll want to create using one of the managed .NET languages. One of the best uses for CLR stored procedures is to replace existing extended stored procedures. T-SQL is only able to access database resources. In order to access external system resources, Microsoft has provided support in SQL Server for a feature known as extended stored procedures. Extended stored procedures are unmanaged DLLs that run in the SQL Server process space and can basically do anything a standard executable program can do, including 84 Microsoft SQL Server 2005 Developer’s Guide accessing system resources that are external to the database, such as reading and writing to the file system, reading and writing to the Registry, and accessing the network. However, because extended stored procedures run in the same process space as the SQL Server database engine, bugs, memory violations, and memory leaks in the extended stored procedure could potentially affect the SQL Server database engine. CLR stored procedures solve this problem because they are implemented as managed code and run within the confines of the CLR. Another good candidate for CLR stored procedures is to replace existing T-SQL stored procedures that contain complex logic and embody business rules that are difficult to express in T-SQL. CLR stored procedures can take advantage of the built-in functionality provided by the classes in the .NET Framework, making it relatively easy to add functionality such as complex mathematical expressions or data encryption. Plus, since CLR stored procedure are compiled rather than interpreted like T-SQL, they can provide a significant performance advantage for code that’s executed multiple times. However, CLR stored procedures are not intended to be used as a replacement for T-SQL stored procedures. T-SQL stored procedures are still best for data-centric procedures. To create a CLR stored procedure in Visual Studio 2005, first select the New | Project option and then select the SQL Server Project template as is shown in Figure 3-3. Give your project a name and click OK to create the project. In this example you can see that I’ve used the name usp_ImportFile for my stored procedure. This stored Figure 3-3 Creating a new SQL Server stored procedure project Chapter 3: Developing CLR Database Objects 85 procedure shows how you can replace an extended stored procedure with a CLR stored procedure. In this case the CLR stored procedure will read the contents of a file and store it in a SQL Server column. After naming the project, click OK. Before Visual Studio generates the project code, it displays the New Database Reference dialog that you can see in Figure 3-4. Visual Studio 2005 uses the New Database Reference dialog to create a connection to your SQL Server 2005 system. That connection will be used to both debug and deploy the finished project. Drop down the Server Name box and select the name of the SQL Server that you want to use with this project. Then select the type of Figure 3-4 The New Database Reference dialog 86 Microsoft SQL Server 2005 Developer’s Guide authentication that you want to use and the database where the CLR stored procedure will be deployed. In Figure 3-4 you can see that I’ve selected the SQL Server system named SQL2005. The project will connect using Windows authentication, and the stored procedure will be deployed to the AdventureWorks database. You can verify the connection properties by clicking the Test Connection button. Once the connection properties are set up the way you want, click OK. All of the required references will automatically be added to your SQL Server project, and Visual Studio 2005 will generate a SQL Server starter project. NOTE While Visual Studio 2005 lets you group multiple stored procedures, triggers, and other CLR database objects in a single DLL, it’s really better to create each CLR database object as a separate DLL. This gives you more granular control in managing and later updating the individual database objects. Next, to create the CLR stored procedure, you can select the Project | Add Stored Procedure option to display the Visual Studio installed templates dialog that’s shown in Figure 3-5. Figure 3-5 Adding a CLR stored procedure Chapter 3: Developing CLR Database Objects 87 From the Add New Item dialog, select the Stored Procedure option from the list of templates displayed in the Templates list and then provide the name of the stored procedure in the Name field that you can see at the bottom of the screen. Here you can see that the stored procedure will be created using the source file usp_ImportFile.vb. Visual Studio 2005 will add a new class to your project for the stored procedure. The generated class file is named after your stored procedure name and will include all of the required import directives as well as the starter code for the stored procedure. You can see the SQL Server CLR stored procedure template in Figure 3-6. By default the SQL Server .NET Data Provider is added as a reference, along with an include statement for its System.Data.SqlServer namespace. Plus, you can see the System.Data reference, which provides support for ADO.NET and its data-oriented objects such as the DataSet and the System.Data.SqlTypes namespace that provides support for the SQL Server data types. Figure 3-6 The CLR stored procedure template 88 Microsoft SQL Server 2005 Developer’s Guide It’s up to you to fill in the rest of the code that makes the stored procedure work. The following example illustrates the source code required to create a simple CLR stored procedure that imports the contents of a file into a varchar or text column: Imports System Imports System.Data Imports System.Data.Sql Imports System.Data.SqlTypes Imports Microsoft.SqlServer.Server Imports System.IO Partial Public Class StoredProcedures <Microsoft.SqlServer.Server.SqlProcedure()> _ Public Shared Sub usp_ImportFile _ (ByVal sInputFile As String, ByRef sColumn As String) Dim sContents As String Try Dim stmReader As New StreamReader(sInputFile) sContents = stmReader.ReadToEnd() stmReader.Close() sColumn = sContents Catch ex As Exception Dim sp As SqlPipe = SqlContext.Pipe() sp.Send(ex.Message) End Try End Sub End Class The first important point to note in this code is the directive that imports the Microsoft.SqlServer.Server namespace. This enables the usp_ImportFile project to use the SQL Server .NET Data Provider without always needing to reference the fully qualified name. The second thing to notice is the <Microsoft.SqlServer.Server. SqlProcedure()> attribute that precedes the method name; it tells the compiler this method will be exposed as a SQL Server stored procedure. Next, you can see that the default class name for this stored procedure is set to StoredProcedures. This class contains a shared method named usp_ImportFile that accepts two parameters: a string that specifies the name of the file that will be imported and a second input parameter that specifies the name of a column that will contain the contents of the file. For C#, the method must be defined as static. For VB.NET code, the method would need to be defined as Shared. Inside the usp_ImportFile method, a new string object named sContents is declared that will contain the contents of the file. Next, a Try-Catch loop is used to . execution of the CLR, putting SQL Server Engine SQL Server OS Windows CLR Hosting Layer Figure 3-1 The SQL Server CLR database architecture 80 Microsoft SQL Server 2005 Developer’s Guide essential. System.Data Imports System.Data .Sql Imports System.Data.SqlTypes Imports Microsoft. SqlServer .Server Imports System.IO Partial Public Class StoredProcedures < ;Microsoft. SqlServer .Server. SqlProcedure()>. directly on the server. To address this issue, Microsoft created the new SQL Server .NET Data Provider. The SQL Server .NET Data Provider establishes an in-memory connection to the SQL Server database. Assemblies After

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

TỪ KHÓA LIÊN QUAN