Walkthrough: Simple deployment of SQLCLR Visual Basic Stored Procedure
The Yukon release of SQL Server allows functions, procedures and triggers to be written in any of the .Net languages. User code can access data from the local or other SQL servers using the Sql Programming Model.
SQL Server Yukon includes a managed provider defined in System.Data.SqlServer that runs in the process space of the server and that users writing code in any of the .Net languages can use to access data.
This document describes a simple example of a Visual Basic SQL Project which contains a simple stored procedure using the data access managed provider that runs inside the server.
In order to complete this walkthrough, you will need:
The walkthrough is split into a number of pieces:
Creating the Project and Function
The first step is to create a VB SQL Server or Visual C# Project
To create the project and function
Visual Studio will create a new project and display a dialog to choose the SQL Server to which the Assembly will be deployed.
Note: If there hasn’t been a connection previously established to a SQL Server 2005 server, Visual Studio will first prompt the user w/ the Add Connection dialog.
In this dialog, the user will choose an existing connection to a SQL Server 2005 sever or create a new connection by selecting the Add Reference button.
This completes the creation of the SQL Server project
Add a Stored Procedure to the project
Now that the project has been created, a stored procedure can be added to the project.
The newly created code file will contain the following Visual Basic code.
Imports System
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlServer
Imports System.Data.SqlTypes
Partial Public Class StoredProcedures
<SqlProcedure()> _
Public Shared Sub StoredProcedure1 ()
' Add your code here
End Sub
End Class
The previous code snippet is the basic template from which a stored procdured can be created. Now, change the code in the stored procedure to the following:
Dim cmd As SqlCommand
cmd = SqlContext.GetConnection.CreateCommand()
cmd.CommandText = "UPDATE Person.Contact SET" & _
"MiddleName = 'Jack' WHERE ContactID = 1"
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
This example performs a simple update to the Contacts table in the AdventureWorks database. This is accomplished by creating a command object from the SqlContext.Connection object. In effect, what is happening is that the procedure is running in the “context” of the connection that it is being called from. The user is not required, nor allowed, to create a completely new connection to perform this operation.
Deploy the Stored Procedure to the SQL Server 2005 database
So far, the project with a connection to the SQL Server 2005 database is created, and the stored procedure is created. The project is now ready to be deployed to the server.
The Build Output pain should contain information similar to the following indicating a successful deployment
------ Build started: Project: SqlServerProject1, Configuration: Debug Any CPU ------
------ Deploy started: Project: SqlServerProject1, Configuration: Debug Any CPU ------
Deploying file: SqlServerProject1.dll, Path: D:\school\SqlServerProject1\SqlServerProject1\obj\Debug\SqlServerProject1.dll ...
Deploying file: SqlServerProject1.pdb, Path: D:\school\SqlServerProject1\SqlServerProject1\obj\Debug\SqlServerProject1.pdb ...
Deploying file: StoredProcedure1.vb, Path: D:\school\SqlServerProject1\SqlServerProject1\StoredProcedure1.vb ...
Deploying file: My Project\MyResources.resx, Path: D:\school\SqlServerProject1\SqlServerProject1\My Project\MyResources.resx ...
Deploying file: SqlServerProject1.vbproj, Path: D:\school\SqlServerProject1\SqlServerProject1\SqlServerProject1.vbproj ...
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Deploy: 1 succeeded, 0 failed, 0 skipped ==========
For another method of verification, the user may query the system tables to check for a successful deployment:
SELECT * FROM sys.assemblies
SELECT * FROM sys.assemby_modules
Execute the deployed Stored Procedure
Now that the procedure is deployed to the server, users can execute the procedure from Server Explorer to verify it is performing as desired.
Note: The Stored Procedures node may need to be refreshed in order to display what was just deployed.
Note: Ensure Database output is selected in the Output pane to view the results of the execution of the deployed function. There is a bug currently whereby the number of rows affected is not returned correctly. Verification of the operation can be accomplished by re-querying the database.
Summary
This walkthrough has illustrated the basic steps involved in creating, deploying, and executing a Visual Basic, Visual C# SQL Server Project. The user could make a number of enhancements to this project.