在asp.netcore中使用Dapper进行数据库管理
                                            准备工作

网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、小程序制作、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了桥东免费建站欢迎大家使用!
一个接口一个类(数据库管理类)
    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;
        }
        /// 
        /// 连接对象
        ///  
        /// 
        /// 查询方法
        ///  
        /// 映射实体类 
        /// sql语句
        /// 参数对象
        /// 事务
        /// 是否缓存结果
        /// command超时时间(秒)
        /// command类型
        ///  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);
        }
        /// 
        /// 执行方法
        ///  
        /// 映射实体类
        /// 参数对象
        /// 事务
        /// command超时时间(秒)
        /// command类型
        ///    数据库管理接口和类都已经准备好,接下来就是注入进去才能进行使用
    //sqlite连接字符串
    var connectionString = string.Format("Data Source={0}/db.sqlite", System.IO.Directory.GetCurrentDirectory());
    //将数据库连接字符串注入进去
    services.AddSingleton(connectionString);
    //sqlieconnection注放
    services.AddScoped();
    //注放数据库
    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
    {
        /// 
        /// 登录
        ///  
        /// 用户名
        /// 密码
        /// 用户管理接口实现
 public class UserRepository : IUserRepository
 {
         /// 
        /// 数据库对象
        ///  
        DBInterface _db;
        public UserRepository(DBInterface db)
        {
            _db= db;
        }
        /// 
        /// 登录
        ///  
        /// 用户名
        /// 密码
        /// (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); } } 
写到这里基本上跟都已经搞定,虽然有些不完善,但是asp.net cor和dapper的接口使用流程与方法做了简单的使用和介绍,不完善的地方需要留给您来完善,不完整的地方需要你自己去完整他,不动脑怎么成长,不动手怎么去验证刘成刚是否正确。
标题名称:在asp.netcore中使用Dapper进行数据库管理
文章链接:http://www.scyingshan.cn/article/jehihe.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 