I have been working with earlier versions of Visual Studio (VS2008) in conjunction with SQL Server 2008.
Recently my organization moved to VS2013 and SQL Server 2012. I am finding the database interfaces in VS2013 quite a bit different than what was present in VS2008.
My question is basically if anyone knows of an article or referenced that details how to do what is described here:
http://yassershaikh.com/how-to-create-a-clr-stored-procedure-using-c-and-visual-studio/
(Specifically the deployment of the DLL to a SQL Server 2012 database).
But using VS2013 and SQL Server 2012.
Stored procedures are routines that cannot be used in scalar expressions. They can return tabular results and messages to the client, invoke data definition language (DDL) and data manipulation language (DML) statements, and return output parameters.
Right-click on a line of code in the text editor on the function or routine that you want to debug. Select Breakpoint, Insert Breakpoint. The breakpoint is added, highlighting the line of code in red. In the Debug menu, select Start Debugging to compile, deploy, and test the project.
Overview. Setting CLR Assembly Permission Sets to SAFE_ACCESS will prevent assemblies from accessing external system resources such as files, the network, environment variables, or the registry.
(In case of minor differences between the info here and what you are seeing, I am running Visual Studio 2012 Update 4 with SSDT 11.1.31203.1 on Windows 8)
In VS 2008 / VS 2010 you could "Deploy" a database project (i.e. .csproj
/ .dbproj
file, respectively) to get the assembly and T-SQL wrapper objects into SQL Server. The newer versions of Visual Studio still have "Deploy" options in the "Build" menu (at least VS 2012 has them in for both the solution and the project), but they do not do anything. You now have a .sqlproj
file and the way to get the solution/project into the database is to use the "Publish {SolutionName}..." option in the "Build" menu.
More on publishing in a moment. First, here is some info that might help:
.sqlproj
) are explicitly handled via SQL Server Data Tools (SSDT)C:\Users\{User Name}\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\{Instance Name}
(localdb)\{Instance Name}
C:\Users\{User Name}\AppData\Local\Microsoft\VisualStudio\SSDT\{Solution Name}
...\SSDT\{Solution Name}
folder will be at least two files per project:
{Project Name}.mdf
{Project Name}.ldf
...\SSDT\{Solution Name}
folder{Build Output Path}\{Build Output File Name}_Create.sql
CREATE {Object Type} {Object Name} ... AS EXTERNAL NAME {Assembly Name}.{Class Name}.{Method Name OR "name" property, if specified, in the [SqlProcedure] / [SqlFunction] / [SqlUserDefinedAggregate] / [SqlUserDefinedType] / [SqlTrigger] attribute}
{Assembly Name}.pdb
file into SQL Server (in sys.assembly_files
); the .pdb
file will be created in the {Build Output Path}
even if this option is unchecked..pdb
file and the source code (.cs
and/or .vb.
) files as ALTER ASSEMBLY {Assembly Name} ADD FILE FROM 0x... AS {File Name}
entries. This option seems to have been replaced by the "Include Symbols" check-box on the SQL CLR tab which only includes the .pdb
file and not the source code files. [I see this as an improvement as I never liked the idea of deploying the source code since some people (maybe many?) promoted all of that stuff into Production :-(.]Selecting the "Publish {Solution Name}..." option in the "Build" menu will bring up a "Publish Database" dialog asking for database details. For "Target database connection:", click the "Edit..." button to the right. A dialog for "Connection Properties" will open. There will be a drop-down for "Server name:" that you can either pull down to discover instances or just type in the name. If you have a default instance running locally, you should be able to just type in "(local)" [but without the double-quotes]. The default authentication in the next section, "Log on to the server", is "Use Windows Authentication". If you are using a SQL Server login then you can select the other radio button and enter in the "User name:" and "Password:" details. The next section is "Connect to a database" and the top radio-button for "Select or enter a database name:" is already selected. There is a drop-down that, if pulled down, will populate with a list of databases on the server that was selected (or entered) in the "Server name:" drop-down. Select, or enter in, the desired database and click the "Test Connection" button on the bottom left of the dialog. You should get a pop-up stating "Test connection succeeded" so click the OK button in that pop-up and then the "OK" button in the "Connection Properties" dialog.
Now you can:
{Build Output Path}\{Build Output File Name}.publish.sql
Just FYI: the base T-SQL pieces to create the objects can be found in the following location, but you should be careful not to run the SQL script as is since the items are not in a runnable order:{ProjectDir}\obj\{Configuration Name}\{Assembly Name}.generated.sql
For several of the Visual Studio-specific references above, those are the names of the fields that you see in the UI. Their equivalents within the .sqlproj
file are:
<Name>
<AssemblyName>
<OutputPath>
<SqlTargetName>
<GenerateCreateScript>
<GenerateSqlClrDdl>
<GenerateSqlClrSymbols>
For more information on SQLCLR in general, I am writing a series on SQL Server Central (free registration required) called "Stairway to SQLCLR". (Currently there are only 4 articles published out of what will eventually be about 12. The next one, coincidentally, is on Development & Visual Studio :-) )
EDIT:
One interesting, and mostly good, new feature is the verification of dependencies to give compile-time errors of T-SQL objects, just like the .Net languages have. The downside is that there doesn't seem to be a way to disable it. This doesn't usually affect SQL CLR projects, except in the case of CLR-based triggers. The problem here is that if you have the "Generate DDL" option enabled, in order to do the "Build" you need to have the Target
and Event
properties of the SqlTrigger
attribute filled out. And, the model (i.e. database model) verification step will check to make that the table named in the Target
property actually exists. If not, you will get an unresolved reference to object
error. The only way to get passed this, it seems, is to a) have that object in your project, or b) have that object in a project that is referenced as a "Database Reference":
CREATE TABLE
statements in SSMS, and then right-click on the project in Solution Explorer (or go to the "Project" menu) and go to "Import -> Script (*.sql)..."..dacpac
file:
.dacpac
fileKeep in mind:
CREATE TABLE
DDL will be generated in the _Create
script each time, but the "Publish" script will only contain DDL for changes, and so if there are no changes in the definition of the table, then the table DDL will not appear in the "Publish" .sql
script. Changes are determined by comparing the table definition in your project or .dacpac
file with the destination database defined in the Connection Settings of the "Publish" dialog / wizard.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With