真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

在asp.netcore中使用Dapper進行數(shù)據(jù)庫管理

準備工作

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序制作、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了橋東免費建站歡迎大家使用!

一個接口一個類(數(shù)據(jù)庫管理類)

    public interface DBInterface
    {
        IDbConnection GetConnection();
        IEnumerable Query(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null);
        int Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);

    }
    public class DB:DBInterface
    {
        IDbConnection _dbConnection;
        public DB(IDbConnection dbConnection, string connectionString)
        {
            _dbConnection = dbConnection;
            _dbConnection.ConnectionString = connectionString;
        }
        /// 
        /// 連接對象
        /// 
        /// 
        public IDbConnection GetConnection()
        {
            return _dbConnection;
        }
        /// 
        /// 查詢方法
        /// 
        /// 映射實體類
        /// sql語句
        /// 參數(shù)對象
        /// 事務(wù)
        /// 是否緩存結(jié)果
        /// command超時時間(秒)
        /// command類型
        /// 
        public IEnumerable Query(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
        {
            return _dbConnection.Query(sql, param, transaction, buffered, commandTimeout, commandType);
        }
        /// 
        /// 執(zhí)行方法
        /// 
        /// 映射實體類
        /// 參數(shù)對象
        /// 事務(wù)
        /// command超時時間(秒)
        /// command類型
        /// 
        public int Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return _dbConnection.Execute(sql, param, transaction, commandTimeout, commandType);
        }

    }

數(shù)據(jù)庫管理接口和類都已經(jīng)準備好,接下來就是注入進去才能進行使用

    //sqlite連接字符串
    var connectionString = string.Format("Data Source={0}/db.sqlite", System.IO.Directory.GetCurrentDirectory());
    //將數(shù)據(jù)庫連接字符串注入進去
    services.AddSingleton(connectionString);
    //sqlieconnection注放
    services.AddScoped();
    //注放數(shù)據(jù)庫
    services.AddScoped();

使用

用戶實體類

    /// 
    /// 用戶實體類
    /// 
    public class User
    {
        /// 
        /// ID
        /// 
        public int ID
        { get; set; }
        /// 
        /// 用戶名
        /// 
        public string UserName
        { get; set; }

        /// 
        /// 密碼
        /// 
        public string Password
        { get; set; }

        public Int64 Phone
        { get; set; }
        /// 
        /// 用戶名稱
        /// 
        public string Name
        { get; set; }
        /// 
        /// 角色ID
        /// 
        public int RoleID
        { get; set; }
        /// 
        /// 部門編號
        /// 
        public int DepartmentID
        { get; set; }
    }

用戶管理接口

    public interface IUserRepository
    {
        /// 
        /// 登錄
        /// 
        /// 用戶名
        /// 密碼
        /// 
        UserRole Login(string userName, string password);
    }

用戶管理接口實現(xiàn)

 public class UserRepository : IUserRepository
 {
         /// 
        /// 數(shù)據(jù)庫對象
        /// 
        DBInterface _db;

        public UserRepository(DBInterface db)
        {
            _db= db;

        }
        /// 
        /// 登錄
        /// 
        /// 用戶名
        /// 密碼
        /// 
        public UserRole Login(string userName, string password)
        {
            string sql = "select " +
                "users.*,roles.rolename " +
                "from users join roles on users.roleid=roles.id " +
                "where username=@username and password=@password";
            var userRole = _db.Query(sql, new { username = userName, password = password }).SingleOrDefault();
            if (userRole == null)
            {
                throw new Exception("用戶名或密碼錯誤!");
            }
            else
            {
                return userRole;
            }

        }
 }

控制器中使用

        /// 
        /// 登錄接口
        /// 
        /// 
        /// 
        /// 
        /// 
        //[HttpPost]
        public IActionResult Login(string userName, string password)
        {
            try
            {
                var userRole = _userRepository.Login(userName, password);
                var BuildToken = Token.BuildToken(
                    new Dictionary {
                     { "userid", userRole.ID },
                     { "username", userRole.Name },
                     { "phone", userRole.Phone }
                 });
                HttpContext.Session.SetString("token", BuildToken);
                return backFun.success(Msg: "登錄成功", Data: new { token = BuildToken,userId= userRole.ID,userInfo= userRole });
            }
            catch (Exception ex)
            {
                return backFun.error(Msg: ex.Message);
            }

        }

    寫到這里基本上跟都已經(jīng)搞定,雖然有些不完善,但是asp.net cor和dapper的接口使用流程與方法做了簡單的使用和介紹,不完善的地方需要留給您來完善,不完整的地方需要你自己去完整他,不動腦怎么成長,不動手怎么去驗證劉成剛是否正確。


網(wǎng)站名稱:在asp.netcore中使用Dapper進行數(shù)據(jù)庫管理
當(dāng)前鏈接:http://weahome.cn/article/jehihe.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部