Search code examples
c#sql-serversqlclr

Microsoft.SqlServer.Server namespace


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?


Solution

  • 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.