How to connect with SQL Server database using .NET Core Class Library (.NET Standard)? - visual-studio

I have just started to create an ASP.NET Core Web API Project. I am not much aware of "ASP.NET Core .NET Standard Library".
I am creating this application using Visual Studio 2017 RC and in the application, I have taken a project of type Class Library (.NET Standard) at repository layer.
Following is the screenshot for the same:
Now from repository Layer I want to connect to the database. I have created a variable
IDbConnection con;
Now I am trying to add reference of System.Data but I am unable to add any reference because when I am opening the add reference window then I am getting the following message:
No Framework assemblies were found on the machine.
How can I connect to database using .NET Core Class Library(.NET Standard)?

.NET Standard Class libraries don't work by directly referencing a DLL, because with .NET Core there is no guarantee the framework will be installed on the system and .NET Core applications can also run as self-contained applications which ship the framework libraries with the application and do not require a runtime to be installed before.
You have to use the NuGet package manager (or project.json or *.csproj in VS2017) to add dependencies. For SQLServer you need to add the System.Data.SqlClient package (link) if you want to directly communicate with the Database (i.e. w/o an ORM).

Above answer (Tseng) may have been valid in 2016 and .NET Standard 1.4, but in the meantime, Microsoft did remove some showstoppers, allowing access to SQL Server from a .NET Standard 2.1 library. Mind the old System.Data.SqlClient will not link, so you (really!) have to refer EF6 via NuGet and change some using.
Create a .NET Standard Class library and put version on 2.1
Install Entity framework (this contains the lower level libs)
In using, refer to System.Data and to Microsoft.Data
Using are:
using System.Data
using Microsoft.Data.SqlClient
Now, "legacy" classes like DataSet, SqlConnection and SqlClient will become available.
It is not completely compatible (yet) There are some things that are not available in .NET standard 2.1, such as enumerating available SQL servers on the LAN. This was done with SqlDataSourceEnumerator which is a class in System.Data I cannot locate in Microsoft.Data.
NOTE: I tested successfully with a .Net Core 3.1 console application. A Standard lib configured as above can be called from .NET Core and connect to the database without issues. You cannot use a .NET Framework caller.

Related

What Non-4.* .NET App Versions play well with Standard?

I am building an MVC Web API (Service) with Views returned in specific cases. As an architectural decision, I've been directed to NOT build the service project in .NET Framework 4.*. Rather, I am to attempt .NET 5.0 first, and then Core 3.1 if 5.0 doesn't work.
This service project in my solution will depend on a few class library projects, call them DataLibrary, ComplexLibrary, and DocLibrary. DataLibrary will depend on a Nuget package of Oracle, be it ODP or Oracle Managed Data, in order to query an Oracle database via an Oracle Package on that database. DocLibrary will depend on a Nuget package of Aspose Word & Aspose PDF. ComplexLibrary will depend on Oracle AND Aspose.
Here's my dilemma:
Aspose Word's latest stable release (21.6) will report that it is compatible with 5.0 and Standard 2.0, but not .NET Core.
Oracle Managed Data reports that it is compatible with Standard 2.1 or Standard 2.0, but not 5.0 or .NET Core.
My own libraries have reported that they are not compatible with my API .csproj if...
3a. The API is 5.0 and the libraries are .NET Core or .NET Standard
3b. The API is Core 3.1 and the libraries are .NET Standard.
Since my compile script naturally requires a run of Nuget to retrieve all the necessary dependencies, I cannot get a clean compile because I seemingly have no combination of versions for my WebAPI and libraries that satisfy each others' compatibility needs. Since Standard libraries are the only common .NET version that satisfy the needs of both Aspose & Oracle, What available version for my WebAPI (i.e. I don't believe that Standard is an option for anything other than a class library) is compatible with .NET Standard libraries?
Thanks.
Please see the following article https://learn.microsoft.com/en-us/dotnet/standard/net-standard.
.NET Standard is not a framework it is kind f specification and .NET Core, .NET 5, Mono framework etc are .NET Standard implementations.
So for library projects I would select .NET Standard and for the service .NET Core or .NET 5 (which is actually the next version of .NET Core)
Well, don't I feel silly.
Turns out, the issue wasn't incompatibilities within Aspose, Office, .NET Core, 5.0, and Standard, but a failure of a prior version of NuGet to handle the different versions.
While my VS 2019 install was able to compile the whole solution effectively, my local install of NuGet was not. This was due to VS2019 likely using the most up-to-date version as of this post (5.9.#), while my locally installed version was 4.9.#. Thus, VS was able to sail through while my compile script kept failing at the NuGet stage (which I have included prior to the actual compile.) Once I ran a NuGet update, everything was good to go.
Long story short: KEEP YOUR NUGET VERSION UP-TO-DATE!!!

ASP.Net Core 2.2: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information

I recently started with ASP.Net Core 2.2. I have a situation where I need to use legacy SAP Connector library within my application. After some reading I created my application to target full .Net Framework 4.6.1. I have a test class that references the SAP Connector library.
However, when I run my application, it breaks within Startup.cs right where it calls this line:
app.UseMvc();
Checking the LoaderExceptions property, it shows the error:
Could not load file or assembly 'sapnco, Version=3.0.0.42, Culture=neutral, PublicKeyToken=50436dca5c7f7d23' or one of its dependencies. An attempt was made to load a program with an incorrect format.
My hunch is that although ASP.Net Core supports full .Net Framework, the referenced assembly has to go as far back to particular version of .Net Framework and no less. Although I can't find any concrete info on that.
Please help!
For SAP Connector, I think you need to contact SAP Forum Support to confirm if the SAP Connector .NET Core version is available or will be available.
From the perspective of .Net Core, I am afraid there are no connectors and no plans to develop such connectors.

Trying to connect to an Informix database in Visual Studio

I am trying to connect to an Informix database and need to add this reference to my project:
using IBM.Data.Informix;
What package in package manager console do I need to be able to use this library?
Thanks in advance!
To connect to an Informix database from .NET you have to options:
The Informix .NET Provider
The IBM Data Server .NET Provider
Have a look at this tech note which describes both .NET provider:
https://www.ibm.com/developerworks/data/library/techarticle/dm-1007dsnetids/
Both will work with Informix. The first one is the 'native' .NET provider included with CSDK (CSDK or ClientSDK is the product that includes all the Informix drivers (ODBC/OLEDB/.NET) etc.
At the moment the only way to get the drivers installed is with a stand alone package (Informix CSDK). There are some plans to get them in NuGet, so you would be able to get the drivers directly from the Visual Studio package manager without anything to install.
The second option, 'IBM Data Server .Net Provider' is included in the 'IBM Data Server' which is an 'common' set of drivers from IBM. It will allow you to connect to either DB2 or Informix (through a DRDA connection)
You can get the IBM Data Server Driver Package from the IBM website, or download the .NET drivers (and required libraries) directly from NuGet:
https://www.nuget.org/packages/IBM.Data.DB.Provider/
PM> Install-Package IBM.Data.DB.Provider
The .NET assembly class is called 'IBM.Data.DB2.dll' There used to be a 'replacement' dll named the same as the Informix CSDK one (IBM.Data.Informix.dll) but is now deprecated. Even with that name ;) it is fully supported against Informix databases.
There are some differences between the .NET providers (e.g. connection string) so if you are going to use the 'DB2' one, I suggest to check the documentation at:
https://www.ibm.com/support/knowledgecenter/SSEPGG_11.1.0/com.ibm.swg.im.dbclient.adonet.doc/doc/c0024472.html
There is also a new beta .NET provider for the new '.NET Core' supporting both Windows and Linux platforms. If you are going to develop for .NET Core, this is the one you want
https://www.nuget.org/packages/IBM.Data.DB2.Core/1.0.0.100
PM> Install-Package IBM.Data.DB2.Core

Why does the .NetCore Class Library reference .NetStandard?

In VS2015 > New Project > .Net Core, there is a template for "Class Library (.NET Core)"
It wasn't until I tried to reference this library in a .NET Core Web API program, that I realized that the Class Library template is referencing .NETStandard v1.6. And my .NET Core API porject won't take it as a reference. I was trying to avoid building the library as a nuget package.
Any ideas on why a Core template isn't referencing core?
Any ideas on quick workarounds?
Update:
Opened a new "Class Library (.NET Core)", before first build I changed frameworks in project.json to
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
Then I created, in a different VS instance, a new "ASP.NET Core Web Application (.NET Core)".
I tried to add a reference to the .dll file for the new class library, now in a netcoreapp1.0 folder and I still get the same error:
.NET Core Projects only support referencing .NET framework assemblies in this release.
How to fix your problem
I was trying to avoid building the library as a nuget package.
Any ideas on why a Core template isn't referencing core? Any ideas on quick workarounds?
When you try to add the .dll as a reference for the API, your error message should say somewhere that you need to create a package in order to use that class library. It is described in the documentation.
However if you do not want to bother to make a package, for example if your class library is used now only in your API project, the easiest way is to build your class library as a Project of your Solution where the API lives. To do so, copy-paste your code in the Solution folder and include it as a project.
Some explanations about the version and frameworks
The Core template (I guess you are talking about the .NET Core API) should create a project.json with
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
}
Let's see what the lines are responsible for:
netcoreapp1.0 under frameworks means your API is targeting the framework .NET Core 1.0. That version of the .NET Core framework implements the .NET Standard 1.6 (the list here) Good thing: your Class library also use this .NET Standard 1.6. Knowing the .NET Standard 1.6 that your Class library is using make you able to choose what version of .NET Core/Xamarin/.NET Framework you can use when you later want to develop a Web app/Mobile app/Windows software for example
imports lists other frameworks/version of packages that your application can use
According to this link dotnet5.6 and portable-net45+win8 are deprecated and a netstandard version should be used instead. dotnet5.6 is still mysterious for me. portable-net45+win8 is equivalent to netstandard1.0. If you let it it means that your application can use package using .NET Standard 1.0. This can be a problem because .NET Core 1.0 starts with .NET Standard 1.6 so I would actually delete those two lines.
That being said, I am more than welcome for comments!
It seems like other answers (before this) didn't get to the point.
.NET Standard is the way forward to write .NET class libraries because it follows a set of standard APIs that can run on virtually all .NET runtimes (full framework, .NET core, Mono Xamarin...)
So if you want to write a .NET core library, you're effectively writing a .NET Standard library. No need to differentiate them. The reference to .NET standard 1.6 is to let the compiler know what to compile against.
netcoreapp1.0 is basically for .NET core console apps. A netcoreapp1.0 project will compile to dll that can be executed (contains program entry like static void Main())
So it makes sense why you can't have that in a library :)

How to run odp.net application in both .net 3.5 and .net 4

I am working on an internal tool for our product. Our product uses oracle database and have evolved over time from .net framework 2.0 to 4.5 and Oracle 10 to 12.2.
The aim of the tool is to write a single application which works across different versions of the product.
I have solved the problem of multiple .net framework versions by using the following entries in app.config
<supportedRuntime version="v2.0.50727"/>
<supportedRuntime version="v4.0"/>
As the oracle managed .net driver is supported for framework >=4.0, I can not use this as I have to support .net framework 3.5 also.
As I have to use unmanaged odp.net driver, I was thinking about the following scenario
My tool would use the lowest version of oracle.dataaccess.dll and target .net 3.5.
Following #1 above makes me refer to 2.xx.... version of the oracle.dataaccess.dll.
When I run this application on a machine with only .net framework 4 installed, what would be the behavior? Would it load 4.xx... version of oracle.dataaccess dll when running under the context of .net framework 4?
The best solution for this would have been availability of oracle managed driver for .net 3.5 version but I found that it is not available.
Please provide your valuable inputs.
Satish
UPDATE :
I have written a sample application targeting .net framework 3.5. In this sample app, I will build a connection string and just open a connection and close it.
This application runs successfully when there are no <supportedRuntime> tags in the app.config.
When we add any <supportedRuntime> tags in the app.config, I am getting a type initializer exception for oracle related types. I have tried this with the supported run time tags
<supportedRuntime version="v2.0.50727"/>
<supportedRuntime version="v4.0"/>
individually and both combined. But I am still getting the issue. Can anyone suggest how to resolve this issue?
ODP.NET unmanaged driver exist in following versions:
1.x (.NET Framework 1.0.3705/1.1.4322), available up to Oracle Client 11.1.
2.0 (.NET Framework 2.0.50727), introduced with Oracle Client 10.2
4.0 (.NET Framework 4.0.30319), introduced with Oracle Client 11.2
If your compile target is .NET version 3 or 3.5 then the application will try to load ODP.NET version 2.0 (and will raise an exception if it is not found on the machine). Actually I am not sure if it would also accept ODP.NET version 4.0.
If your compile target is .NET version 4 or higher then the application will try to load ODP.NET version 4.0 (and will raise an exception if it is not found on the machine).
You can do several solutions:
Provide a copy of Oracle.DataAccess.dll which matches your version and put it in your application directory.
Use late binding, i.e. instead of
var con = new Oracle.DataAccess.Client.OracleConnection();
use
var DLL = Assembly.Load(String.Format("Oracle.DataAccess, Version={0}.{1}.*.*, Culture=neutral, PublicKeyToken=89b483f429c47342", frameworkVersion, oracleVersion));
var type = DLL.GetType("Oracle.DataAccess.Client.OracleConnection", true, false);
dynamic con = Activator.CreateInstance(type)
However, this syntax is only available from .NET Framework version 4.0 on, I do not know how to write this in version 3.0/3.5.
Note, use con.GetType().Assembly.FullName and con.GetType().Assembly.Location in order to see which DLL was really loaded.

Resources