Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement IDbConnection in .Net Core

I have a .Net core web application and .net core library. When using Dapper, I need to pass in a concrete class that inherits from IDbConnection. The problem is that in .Net Core System.Data is not a viable reference, it looks like they are doing away with it.

Is there a .Net replacement or a nuget package I can use some where. I need to connect to Microsoft Sql Server 2016

Edit

I attempted to make a .Net Standard Library to see if I could use System.Data that way. And I also could not add System.Data to the new project.

Edit 2

I found a nuget package System.Data.Common. This seemed to work it had the interface IDbConnection however I can't see a way to connect to SQL. I would have to make my own implementation of IDbConnection. So I am at a loss again.

like image 382
christopher clark Avatar asked Jul 30 '17 15:07

christopher clark


People also ask

What is IDbConnection in C#?

The IDbConnection interface enables an inheriting class to implement a Connection class, which represents a unique session with a data source (for example, a network connection to a server). For more information about Connection classes, see Connecting to a Data Source.

Can I use Dapper in .NET core?

Using Dapper Queries in ASP.NET Core Web API Then inside the using statement, we use our DapperContext object to create the SQLConnection object (or to be more precise an IDbConnection object) by calling the CreateConnection method.

What is the difference between IDbConnection and SqlConnection?

Answers. SqlConnection is used to connect to Sql Server databases only. IDbConnection is a generic interface.


Video Answer


1 Answers

Add System.Data.SqlClient nuget package as dependency. It has

//
// Summary:
//     Represents an open connection to a SQL Server database. This class cannot be
//     inherited.
public sealed class SqlConnection : DbConnection

so you will be able to do

using System.Data.SqlClient;
...

using (IDbConnection dbConnection = new SqlConnection(connectionString))
{
    dbConnection.Open();
    // dbConnection.Execute(query, data);
}
like image 116
Set Avatar answered Sep 18 '22 15:09

Set