简体   繁体   中英

How to get the Microsoft SQLCLR Stored Procedure Example to work

I'm new to CLR and am struggling with the Microsoft tutorial already.
This is the Tutorial: How to: Create and Run a CLR SQL Server Stored Procedure

There is an example stored procedure Insert_Currency_CS and a test.sql

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;


public partial class StoredProcedures
{
    [SqlProcedure()]
    public static void InsertCurrency_CS(
        SqlString currencyCode, SqlString name)
    {
        using (SqlConnection conn = new SqlConnection("context   connection=true"))
        {
            SqlCommand InsertCurrencyCommand = new SqlCommand();
            SqlParameter currencyCodeParam = new SqlParameter("@CurrencyCode", SqlDbType.NVarChar);
            SqlParameter nameParam = new SqlParameter("@Name", SqlDbType.NVarChar);

            currencyCodeParam.Value = currencyCode;
            nameParam.Value = name;

            InsertCurrencyCommand.Parameters.Add(currencyCodeParam);
            InsertCurrencyCommand.Parameters.Add(nameParam);

            InsertCurrencyCommand.CommandText =
                "INSERT Sales.Currency (CurrencyCode, Name, ModifiedDate)" +
                " VALUES(@CurrencyCode, @Name, GetDate())";

            InsertCurrencyCommand.Connection = conn;

            conn.Open();
            InsertCurrencyCommand.ExecuteNonQuery();
            conn.Close();
        }
    }
}

and

EXEC InsertCurrency 'AAA', 'Currency Test'
SELECT * from Sales.Currency where CurrencyCode = 'AAA'

To get this to work I have to add the AdventureWorks2014 I guess, so I just added it as new Database Project into my solution can copypasted its content (tables and 2 scripts) into my First Project Database inside of the solution explorer. But then I get Syntax Errors and not recognized option errors in my Deployment Script CLRTest.sql at for example these lines:

GO
:setvar DatabaseName "CLRTest"
:setvar DefaultFilePrefix "CLRTest"
:setvar DefaultDataPath "C:\Users\Torsten\AppData\Local\Microsoft\VisualStudio\SSDT\CLRTest"
:setvar DefaultLogPath "C:\Users\Torsten\AppData\Local\Microsoft\VisualStudio\SSDT\CLRTest"

The first :setvar line I get the syntax error and DataBaseName is not a recognized option. Similarly here

:on error exit

and here

:setvar __IsSqlCmdEnabled "True"

Now can maybe someone explain me how to make my Database project use the AdventureWorks2014 database, so that the tutorial CLR stored procedure is working when being executed by the test.sql.

I guess the problem is that I don't know how to put the AdventureWorks2014 database into my empty database project. I am trying SQLSchemaCompare now, then update Target (localdb)\\ProjectsV12.CLR Test. Here I have the problem, that if I include AW2014FullTextCatalog in the Upgrade, I receive the an script execution error, and if I exclude it, I get an error in another script CREATE FULLTEXT INDEX ON... Additionally this error:

Error 1 SQL72014: .Net SqlClient Data Provider:
Msg 9982, Level 16, State 100, Line 1 Cannot use full-text search in user instance.
C:\\Users\\Torsten\\AppData\\Local\\Temp\\CLR Test_1_Update3.publish.sql 47 1

I'm not even sure is this is the right way to put a database into an empty database, the MS tutorial wasn't even up to VS2013 but only VS2010...

If you have no knowledge of SQLCLR, and are even unfamiliar with "SQLCMD mode" in SSMS / Visual Studio (hence the syntax errors on lines starting with :setvar ), then you should not be starting with the Microsoft examples. For example, the tutorial linked in the question even states, at the top:

This documentation is archived and is not being maintained.

So, clearly the tutorial page isn't being read very carefully and any number of things could be going on here, possibly related to version differences in SQL Server, Visual Studio, etc. Or, there could be prerequisite steps for doing the Microsoft tutorials that haven't been followed.

Instead, you should review the series of articles I am writing on working with SQLCLR on SQL Server Central:

Stairway to SQLCLR

Free registration is required to view content on that site, but it is well worth it as there is an incredible amount of free information related to working with SQL Server.

The series on SQLCLR that I am writing guides you through what exactly SQLCLR is and isn't, what it can (and can't) do, what the differences are between SQL Server's CLR host environment and the Windows CLR host (ie the "regular" .NET environment) and the fact that SQLCLR operates more like a Web App than a Console App / WinForm App.

The examples in my articles are step-by-step with full explanations of what is going on. Working Assemblies are provided for the first several articles as one shouldn't be messing with creating SQLCLR objects until there is a basic understanding of the environment, given the restrictions and nuances of working within SQL Server that could be dangerous if you just start creating SQLCLR objects without having any basis for understanding what you are doing and if you should even be doing it.

After that, then (maybe) take another look at the Microsoft examples. But it could very well be that either you don't need SQLCLR (ie T-SQL is just fine), or perhaps you can't do what you want in T-SQL but doing it in SQLCLR won't work due to the environmental constraints.


With regards to the out-dated tutorial you are working on, I don't see why you are trying to include the AdventureWorks2014 in your Visual Studio / SSDT project. You should be able to ignore the existing schema / objects and just publish to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM