8

I'm trying to implement a Common Language Runtime (CLR) assembly to use with SQL Server. For creating dll I'm using Visual Studio 2019.

I have the following code from an example tutorial:

using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;


namespace Split
{
    public class Class1
    {
        [SqlFunction(
            DataAccess = DataAccessKind.None,
            FillRowMethodName = "MyFillRowMethod"
            , IsDeterministic = true)
        ]

        public static IEnumerable Split(string stringToSplit, string delimiters)
        {

            string[] elements = stringToSplit.Split(delimiters.ToCharArray());
            return elements;
        }

        public static void MyFillRowMethod(Object theItem, out SqlChars results)
        {
            results = new SqlChars(theItem.ToString());
        }
    }
}

But I encounter this error:

Error CS0234 The type or namespace name 'SqlServer' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

Can anybody show me the way of fixing my problem?

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
Bohdan
  • 158
  • 2
  • 10

1 Answers1

8

As the error message hinted at:

You're probably missing the reference to System.Data.SqlClient, which contains the SqlFunctionAttribute in the Microsoft.SqlServer.Server namespace.

  1. Right click on your project and click on Manage NuGet Packages...

    NuGet

  2. Search for System.Data.SqlClient

    enter image description here

  3. Install

    enter image description here

That should resolve your issue.

div
  • 905
  • 1
  • 7
  • 20
  • Why would someone have to get a NuGet package for something that is part of the .NET Framework itself. From Microsoft: *"CLR integration functionality is exposed in an assembly called System.Data.dll, which is part of the .NET Framework. This assembly can be found in the Global Assembly Cache (GAC) as well as in the .NET Framework directory. A reference to this assembly is typically added automatically by both command line tools and Microsoft Visual Studio, so there's no need to add it manually."* – Ian Boyd May 24 '23 at 15:01
  • Because I assumed that the author wasn't talking about the .NET framework version but instead about .NET Core, where the SqlClient has been moved out into a separate package. – div May 30 '23 at 13:16