Implement IDbConnection in .Net Core
Asked Answered
R

2

15

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.

Redneck answered 30/7, 2017 at 15:34 Comment(0)
H
19

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;    // As of 2019 using Microsoft.Data.SqlClient;
...

using (IDbConnection dbConnection = new SqlConnection(connectionString))
{
    dbConnection.Open();
    // dbConnection.Execute(query, data);
}
Hadfield answered 30/7, 2017 at 16:38 Comment(5)
Ahh, what a wild ride.Redneck
As of 2019, you should now use Microsoft.Data.SqlClient (available as a nuget package).Unborn
Microsoft.Data.SqlClient does not define IDbConnection.Sociability
It's still as System.Data.SqlClient for me using new .Net Core 3.1 project.Marginate
Maybe you don't have problem anymore but for other who found here. You should abandon IDbConnection and start to use DbConnection which is found from System.Data.Common namespace it is Abstract class which can be used quite similar as that interface.Fib
S
0
//create class for DBconnection
public class DBConnectionHelper
{
    private readonly string _connectionString;

    public DBConnectionHelper(string connectionString)
    {
        _connectionString = connectionString;
    }

    public IDbConnection ConnectToDatabase()
    {
        SqlConnection conn = new SqlConnection(_connectionString);
        try
        {
           // conn.AccessToken = new Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProvider().GetAccessTokenAsync("https://database.windows.net/").Result;
            conn.Open();
        }
        catch (Exception)
        {
        }
        finally
        {
        }
        return conn;
    }
}

In startup class:

services.AddTransient(db => new DBConnectionHelper(AppSettings.ApplicationDBConnString).ConnectToDatabase());

And in apsetting.json:

"ConnectionStrings": {
 "ApplicationDBConnectionString": "server=....;Database=....;user id=....;Password=...;",
Signesignet answered 18/3 at 5:21 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Authoritative

© 2022 - 2024 — McMap. All rights reserved.