question

JuliaZhuang-2149 avatar image
0 Votes"
JuliaZhuang-2149 asked cooldadtx answered

SqlProcedure does not exist in the namespace Microsoft.SqlServer.Server

I am trying to create a CLR Stored Procedures assembly in visual studio following this tutorial: https://docs.microsoft.com/en-us/aspnet/web-forms/overview/data-access/advanced-data-access-scenarios/creating-stored-procedures-and-user-defined-functions-with-managed-code-cs
However, I have the error message saying SqlProcedure does not exist in the namespace Microsoft.SqlServer.Server.
I already have Microsoft.SqlServer.Server and System.Data.SqlClient imported using Nuget as shown in the picture, Can anyone help me with this error? Thanks!
216618-screen-shot-2022-06-30-at-103914-am.png


vs-setupdotnet-package-managementdotnet-sqlclient
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered

The SqlProcedureAttribute is defined in System.Data which should already be referenced by your code. It isn't defined in SMO and that assembly isn't needed unless you're trying to work with SMO for other reasons.

using Microsoft.SqlServer.Server;

[SqlProcedure(Name = "MySproc")]
public static void MySproc ()
{
}


Based upon your solution explorer screenshot it looks like you might be trying to create a .NET 5/6 project for your code. Note that you won't be able to do that for SQL CLR. SQL CLR relies on the version of .NET that ships with SQL which is currently .NET Framework. Therefore you need to ensure that your code is compiling with a target framework of .NET 4.5 to 4.8, depending upon what version is actually installed on your SQL machine with 4.7.2 being the most common. Otherwise the code won't run.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.