VB.NET • View the GetSQLVersion CLR's Scalar Value

Listing 2. This class module contains a UDF that returns the SQL version as a scalar value in the return code, instead of as a resultset (as you did with your stored procedure in Listing 1).

Imports System
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlServer
Imports System.Data.SqlTypes

Partial Public Class UserDefinedFunctions
        <SqlFunction(DataAccess:=DataAccessKind.Read)> _
                Public Shared Function GetSqlVersion() As SqlString
                ' Get the current connection for this context
                Dim objSqlCommand As SqlCommand =  _
                        SqlContext.GetCommand()

                ' Execute our query
                objSqlCommand.CommandText = "SELECT @@version"

                ' Return the value from the function
                Return New _ 
                        String(CType(objSqlCommand.ExecuteScalar(), Char()))
        End Function
End Class