Failed to CREATE AN ASSEMBLY in SQL - sql

I've already dived into SQL clr programming. Unfortunately my first attempt is troubled.
My C# assembly code is just so:
enter code here
public partial class FirstCLRRoutines
{
public static int GetCLRFrameworkMajorVersion()
{
return System.Environment.Version.Major;
}
}
And SQL code is:
USE master
GO
CREATE ASSEMBLY [Chapter2.FirstCLRRoutine]
FROM 'D:\projeler\SQL_CLR\SQL_CLR\bin\Debug\SQL_CLR.dll'
But I get this error message from MSSMSE:
Msg 6218, Level 16, State 3, Line 1
CREATE ASSEMBLY for assembly 'SQL_CLR' failed because assembly 'SQL_CLR' failed verification. Check if the referenced assemblies are
up-to-date and trusted (for external_access or unsafe) to execute in
the database. CLR Verifier error messages if any will follow this
message

I just encountered exactly the same problem.
This is an old page, but first, formost and finally, the DLL must be built with .NET 2.0. SqlServer 2005 is built with .net 2.0, because that is the latest available when it was written. SqlServer 2008 might also have the same issue.
The DLL and the SqlServer must both reference the same .NET framekwork version. Once I figured this out, I created a separate project under 2.0 and shazam! worked perfect first time.
To see what version of .net your sql server uses:
select * from sys.dm_clr_properties

The accepted answer, while seeming to resolve the O.P.'s issue, is only partially correct and presents an overly simplistic explanation of the underlying cause, which could lead other people with a similar problem in the wrong direction.
The problem with the accepted answer is a misunderstanding of the .NET environment, and that same misunderstanding can also be seen in the Question itself. Within .NET, the CLR and the Framework are two separate things, each with their own versions.
The CLR (Common Language Runtime) is what executes managed code. This is not updated nearly as often as the Framework. The .NET Framework is a collection of libraries that provide the basic means of interacting with a particular version of the CLR.
A single version of the CLR typically has multiple versions of the Framework that it works with. A single version of the Framework, however, only works with one specific version of the CLR. For example, CLR version 2.0 works with Framework version 2.0, 3.0, and 3.5, while CLR version 4.0 works with all of the 4.x versions of the .NET Framework (i.e. 4.0, 4.5, 4.5.1, 4.5.2, 4.6, etc). To see the chart of CLR verion to Framework version relationships, see the MSDN page for .NET Framework Versions and Dependencies.
With regards to SQLCLR code, SQL Server only works with a single version of the CLR, and the specific version depends upon the version of SQL Server. SQL Server 2005, 2008, and 2008 R2 work only with CLR version 2. Since CLR version 2 only works with .NET Framework versions 2.0, 3.0, and 3.5, this means that SQL Server 2005, 2008, and 2008 R2 only work with .NET Framework versions 2.0, 3.0, and 3.5. Of course, SQL Server 2005 only included .NET Framework version 2.0 so there are a couple of newer libraries in .NET Framework version 3.0 and 3.5 that don't work in SQL Server 2005 without manually importing them (i.e. System.Core and System.Xml.Linq). Along those same lines, SQL Server 2012, 2014, and 2016 are statically linked to CLR version 4, which works with .NET Framework versions 4.0, 4.5, 4.5.1, 4.5.2, 4.6.
With regards to the information returned from both System.Environment.Version (in the Question) and sys.dm_clr_properties.version (in the accepted answer), they are reporting the CLR version, not the Framework version. So be careful not to confuse those two things reporting 2.0 or 4.0 as meaning you can only use Framework version 2.0 or 4.0.
And fortunately, due to backwards compatibility, code compiled against the CLR 2 Framework versions (2.0, 3.0, and 3.5) will run without needing to be recompiled in SQL Server 2012 and newer, even though they are on CLR version 4.
So, you generally cannot go wrong with using a Target Framework Version of 2.0, but you most certainly can use Framework versions beyond 2.0.
For a more in-depth look at developing SQLCLR code, check out the following article (and the series in general), which I wrote:
Stairway to SQLCLR Level 5: Development (Using .NET within SQL Server)

USE master
GO
CREATE ASSEMBLY [Chapter2.FirstCLRRoutine]
FROM 'D:\projeler\SQL_CLR\SQL_CLR\bin\Debug\SQL_CLR.dll'
WITH PERMISSION_SET = UNSAFE
Try that, and let me know if that works.

The library, System.Environment, is not supported for CLR:
http://msdn.microsoft.com/en-us/library/ms403279.aspx
You can still use it, as indicated in the "Unsupported Libraries" section of the article above, but keep in mind that the code has not been tested for security and reliability. This can cause unpredictable results in a production environment, so think about the risks and carefully test before deploying.
Also, I believe it either has to have a strong name or be deployed to a database marked as "Trustworthy" before it will execute.

Short answer :
set Sql Server version and .Net Framework version on project property.
First of all you have to check set your project property. in project property set the version of sql server that you want to create CLR for it. then choose .Net Framework version . for example if you want to create CLR for SQL Server 2008 you have to set .Net Framework to 3.5 and for 2005 choose .Net 2.0.
i hope this solution help you.

Related

Azure Function Targeting .NET Standard Showing Warning

Just installed Visual Studio 15.5 Preview so that I can create an Azure Function targeting .NET Core. Without making any changes, I'm seeing a warning -- see below -- that reads:
Package Microsoft.AspNet.WebApi.Client 5.2.2 was restored using .NET
Framework version 4.6.1 instead of the project target framework .NET
Standard version 2.0. This package may not be fully compatible with
your project.
Any idea how to fix this or do I ignore this warning? As I said, this is a brand new Azure Function project I created with no changes at all.
Here's what the warning looks like:
You can pretty much ignore it, in this case.
Microsoft.AspNet.WebApi.Client targets "Portable Class Library (.NETFramework 4.5, Windows 0.0, WindowsPhone 8.0, WindowsPhone 8.1, WindowsPhoneApp 8.1)" (or net45+win8+win81 as it's the target framework moniker called), which means it's fully compatible with .NET Core and .NET Standard (>= 1.2).
The warning comes, because it do not target the netstandard1.x or netstandard2.x moniker specifically. It just tell you "this might not be compatible on .NET Core/.NET Standard".
Yes, you did nothing wrong: this is to be expected for now. Functions v2 are in beta now, so you'd have to live with this warning for a while. It should give you no functional issues.

Oracle.ManagedDataAccess for .Net 3.5

The question is:
Is there a library with Oracle.ManagedDataAccess for .NET 3.5?
Currently application uses.NET 4.0 but I must crate opportunity to use application on .NET 3.5 with independent Oracle library.
The fully managed driver, which uses the Oracle.ManagedDataAccess namespace, is only available for .NET 4.0, as can be read in the Oracle documentation.
If you need to support .NET 3.5 you have to either use the unmanaged driver (Oracle.DataAccess namespace) or a third party driver like dotConnect for Oracle.

TryParse is not a member of System.Enum error in VB.NET

I checked out http://msdn.microsoft.com/en-us/library/dd783499.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1. It should exist.
EDIT:
I tried it on vs2010 (different project, though), and it exists.
Enum.TryParse was added with .Net Framework 4.0.
You can easily tell this by looking at the Other Versions drop down menu in the documentation or the version Information:
.NET Framework
Supported in: 4.5, 4
.NET Framework Client Profile
Supported in: 4
Portable Class Library
Supported in: Portable Class Library
.NET for Windows Store apps
Supported in: Windows 8
So probably you're using an older .Net Framework version.
Do you have System Import?
Imports System
If you are in Visual Studio 2010, make sure you are targeting .NET Framework 4.0

Firebird ADO.NET Data Provider Version 3.0.2 does not support .Net Framework 4?

I downloaded Firebird ADO.NET Data Provider 3.0.2 from the Firdbird website.
http://www.firebirdsql.org/en/net-provider/
I start a new visual studio project, specifying .Net Framework 4 (not client profile)
At the top of one of my source files I put
using FirebirdSql.Data.FirebirdClient
Then I try to build the project, and it gives the following error -
The type or namespace name 'FirebirdSql' could not be found (are you missing a using directive or an assembly reference?)
However if I download version 2.6 of the Firebird ADO.NET Data Provider, it works just fine.
Am I understanding this correctly, version 3.0.2 does not support .NET Framework 4.0?
Unfortunately Windows Xp doesn't support .NET 4.5 so I hope that's not the case.
You have to download the "NET40" version and reference this.

CLI and .NET versioning

Microsoft .NET CLR is an implementation of CLI standard. However, the last publication of CLI standard was made in 2006, but the latest release of CLR was produced in 2010. I understand that work is now in progress on latest version of the CLI specification.
Is it correct to understand that current public CLI specification describes previous version of CLR (2.0) and current version of CLR (4.0) does not yet have corresponding CLI specification.
Does latest version of Mono implement the version of CLI specification, which is not yet published but is complete enough to be able to build CLR-complatible runtime?
The current 5th edition (not to be confused with the version of the .NET Framework) of the CLI is specification is not yet final. You can download a working draft here:
ECMA C# and Common Language Infrastructure Standards
As the specification is not yet final it is not really correct to say that any product supports it. However, the final version will probably very similar to what is supported by Microsoft in the version 4.0 CLR.