Once your code is ready to test, you need to double-check the spelling of the function name to make sure it matches the code specification. Since the Visual Studio compilers (perhaps foolishly) assume you know what you're doing, it won't help you spell the entry point of the class you're creating. To build your application, choose "Build" from the project right-click menu or "Build Solution" from the Build menu. This instructs Visual Studio to compile the runtime DLL for the executable. If this succeeds, you're almost ready to deploy. Preparing to Deploy a CLR Executable FunctionThe deployment phase installs the CLR executables into the targeted SQL Server database you specified earlier. To view or re-address this database, right-click the Project and choose "Properties". This opens the familiar tabbed dialog (as shown in Figure 13.10) that is the GUI interface for the assemblyinfo(.vb/.cs) configuration file. Click the "Database" tab to view the current Connection String. You can change this manually or click on Browse to re-open the Data Connection chooser dialog you saw earlier in Figure 13.5. Figure 13.10. Addressing the target database.Tip Sure, it would be useful to visit the SQL Server database to see if the new CLR executable might collide with an existing executable. Exploring Executables with the Server ExplorerThe Server Explorer can be used to view and manage CLR executables, as shown in Figure 13.11. Note that the Server Explorer lists the deployed (and visible) executables along with the Assemblies used to create these executables. Yes, this means the source code used to build your function, stored procedure, aggregate, or User-Defined type is stored in the targeted database. Figure 13.11. Using the Server Explorer to view (and manage) SQL Server CLR Executables.
On closer inspection, you can see that the Server Explorer categorizes the executables by intermixing T-SQL functions, stored procedures, and types with CLR executables. This can be a bit confusing, especially since all of the function types are rolled together. Note that while the function parameters are shown, you need to drill into the property page to view the data type. Tip Missing from this tree view is any reference to ordinary SQL Server User-Defined types. Exploring Objects with SQL Server Management StudioIn contrast, the SQL Server Management Studio Object Explorer breaks out functions into categories based on the type of data returned, as shown in Figure 13.12. In this case, functions are broken down by "Table-valued" functions (which return a Table-type variable); "Scalar-valued" functions, which return a single value; "Aggregate" functions, which return an aggregated value; and "System" functions, which are implemented by SQL Server. This approach makes SQL Server Management Studio a more appealing choice for developers who need to manage large numbers of executables. Figure 13.12. Using the SQL Server Management Studio Object Explorer to view deployed executables.
Note that the SQL Server Management Studio Object Explorer also exposes the executable parameters along with the parameter datatype and default. This explorer also lists the CLR "User-Defined Types" (not to be confused with ordinary SQL Server "User-Defined Data Types"). Managing Executable DependenciesRemember, I'm exploring existing CLR executables to see if my new CLR function will collide with an existing function (possibly created by someone else on the team). So what if the CLR executable you're about to deploy already exists? Well, in this case, you have some issues to deal with:
Deploying a CLR Executable FunctionIf you think it's okay to deploy (your new function won't break existing functionality), you can proceed to deployment. Note that the default Solution Configuration automatically builds and deploys the CLR executable being developed when you click on the green debug arrow or press F5[6]. During the (build) deploy cycle, the Visual Studio Output (build) window displays messages returned from the compiler as Visual Studio builds and deploys your CLR executable. Note that several files in the assembly are posted to the server, where they're added to the Assemblies in the target database:
It's always a good practice to check the Output Build window to see if the build and deploy phases completed before continuing. Yes, your application should deploy without issue unless there are frozen dependencies that block completion of the process. Of course, this step requires access to the designated SQL Server and sufficient rights to create the executables. Don't take this for granted. Most DBAs don't grant these rights to just anyone.
As you move back and forth from Visual Studio to SQL Server, you'll want to right-click and choose "Refresh" to ensure that just-added objects are visible in the Object and Server Explorers. Profiling the Deployment ProcessA tremendous amount of work goes on behind the scenes when you deploy your CLR executable. It's well beyond your "need to know," as far as I'm concerned, but if you're interested or have some issue that can't seem to be resolved by letting Visual Studio deploy your CLR executable, it might be helpful to understand what's going on behind the curtains when you click "Deploy" (as witnessed by the SQL Profiler):
Yes, you can use the Profiler to watch these operations flash by as you deploy your CLR executable, and you can learn enough about the process to replicate it using your own T-SQL code, if necessary. Some authors would have you think that using T-SQL is the only way to deploy CLR Assemblies, but they're not aware of what Visual Studio can door they don't care. The following steps walk you through the process of creating deploy scripts (leveraging an existing CLR Assembly) to deal with complex deployment issuesor to simply document what you've done. That's something that makes DBAs happy, and we all know how important that is.
No, as I said, in most cases, you don't need to go through all of this when deploying your CLR executable. |