DataContextDapper.cs

mail@pastecode.io avatar
unknown
csharp
16 days ago
1.4 kB
1
Indexable
Never
using HelloWorld.Models;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Data.SqlClient;
using System.Data;
using Dapper;


namespace HelloWorld.Data
{

    public class DataContextDapper
    {
        //private IConfiguration _config;
        private string? _connectionString;
        public DataContextDapper(IConfiguration config){

            //_config = config;
            _connectionString = config.GetConnectionString("DefaultConnection");

        }
       

        public IEnumerable<T> LoadData<T>(string sql)
        {
            IDbConnection dbConnection = new SqlConnection(_connectionString);
             return dbConnection.Query<T>(sql);
        } 

        public T LoadDataSingle<T>(string sql)
        {
            IDbConnection dbConnection = new SqlConnection(_connectionString);
             return dbConnection.QuerySingle<T>(sql);
        } 

        public bool ExecuteSql(string sql)
        {
            IDbConnection dbConnection = new SqlConnection(_connectionString);
             return (dbConnection.Execute(sql) > 0);
        } 

         public int ExecuteSqlWithRowCount(string sql)
        {
            IDbConnection dbConnection = new SqlConnection(_connectionString);
             return dbConnection.Execute(sql);
        } 
    } 
}
Leave a Comment