2

SQLCLR Visual Studio 2015

I'm new to writing CLR code.

I'm getting the following error when compiling a SQL CLR Function

CompilerError

I'm using the .Net Coordinates library.

The code in question is

public Datum.Datum Datum { get; }

The same library when compiled in a C# console application (not CLR) builds and executes successfully using Visual Studio 2015.

My understanding is that by using Visual Studio I'm using C# v6.

Could the .sqlproj be forcing the use of a earlier version of C#?

Mazhar
  • 3,797
  • 1
  • 12
  • 29
  • If you click project from the main menu is there an "enable c#6" option? – Alex K. Sep 27 '17 at 12:08
  • 2
    The property is auto property and it has has only get. If it doesn't have set or any logic of returning something how do you expect it to return anything. That's why you are seeing this error. If you are indented to set the value of this property from inside the class then you can declare private set in the property. Or have some logic in the get to return some value. – Chetan Sep 27 '17 at 12:08
  • @ChetanRanpariya C# 6 allows auto-properties to be declared without a setter, they can be assigned in the ctor for example. – Alex K. Sep 27 '17 at 12:14
  • No, there isn't Alex K. – Mazhar Sep 27 '17 at 12:49
  • Can you show the complete compiler error message with error code? – Paulo Morgado Sep 27 '17 at 12:58
  • I've updated the Question with the full error @PauloMorgado – Mazhar Sep 27 '17 at 13:03
  • 3
    What about '{ get; private set;}'? – Kevin Candlert Sep 27 '17 at 13:33
  • 2
    If you open the project properties, select the "SQLCLR Build" tab and open "Advanced..." you can choose the language version from a drop down.If you change from "default" to "C# 6.0" and recompile, you'll get an error "Invalid option '6' for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default". This shows that the build is using an older `csc.exe` compiler rather than the Visual Studio C# compiler. I suspect you'd need to edit some `.targets` files related to SQL to get it to use a better version of `csc.exe`. – Damien_The_Unbeliever Sep 27 '17 at 13:42
  • [similar question](https://stackoverflow.com/questions/33011682/usage-of-wrong-compiler-during-sql-server-database-project-building) – Damien_The_Unbeliever Sep 27 '17 at 13:48
  • @Damien_The_Unbeliever I made that change and I get the "invalid option ..." error. I will check out the similar question. – Mazhar Sep 27 '17 at 14:00
  • @KevinCandlert That worked for me after I restarted VS, Thanks – Mazhar Sep 28 '17 at 09:24
  • @Damien_The_Unbeliever It seems SSDT for VS2015 doesn;t support c#v6. Only SSDT on VS2017 does. – Mazhar Sep 28 '17 at 09:25

2 Answers2

1

The main issue seems to be a disconnect between Visual Studio and the .NET Framework / CLR. Visual Studio, it seems, can be updated to use newer versions of C#, independently of any new version(s) actually existing on your machine.

You shouldn't need to specify using C# 6.0 in the advanced SQLCLR build properties, but doing so gives you an error message that is insightful:

Invalid option '6' for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default

Looking at the output messages (assuming a high-enough level) it should show that you are using C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe, so it doesn't appear that the .sqlproj file or SSDT is forcing anything here. But, if you go to a command prompt, go to the folder containing Csc.exe and run it directly (i.e. just Csc at the prompt), you should see the follow note:

Microsoft (R) Visual C# Compiler version 4.6.1590.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

That link takes you to the GitHub repository for Roslyn. To make things easier, here is the link directly to that page, starting at the section for downloading the compiler without also downloading Visual Studio (since you already have that):

https://github.com/dotnet/roslyn#download-c-and-visual-basic

Solomon Rutzky
  • 46,688
  • 9
  • 128
  • 171
0

If you don't want to use C# 6, implement the getter with a private backing field:

public Datum.Datum Datum { get {return _datum; } }
private Datum.Datum _datum;

Set the value of _datum somewhere inside the class (ctor, init method, ...).

Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
  • That's the thing I want to use C#6.0 but I suspect the settings somewhere, possibly the sqlprof file, might be forcing the use of an earlier version, or perhaps the SQL Server Data Tools 2015 doesn't support C#6.0, I don't know. – Mazhar Sep 27 '17 at 13:39