1 . 用戶表t_user的字段如下表:
字段名稱 | 類型 | 描述 |
user_id | int | 用戶唯一編號 |
nickname | varchar(10) | 用戶昵稱 |
varchar(30) | 登錄郵箱 | |
password | varchar(20) | 登陸密碼 |
role_code | varchar(10) | 角色代碼(1:普通用戶,2:銀卡會員,3:金卡會員,4:鑽石會員) |
state_code | char(1) | 狀態代碼(1:正常,0:鎖定,默認是1) |
mobile | varchar(15) | 手機號 |
age | smallint | 用戶年齡 |
register_time | datetime | 注冊日期 |
2 . 為了聚焦在.net core上,所有數據庫操作都用偽代碼實現,即只做接口,不具體編碼實現。
3 . 在models文件夾中新建 user.cs,用於數據的強類型操作,代碼如下:
1 public class User 2 { 3 public int UserId{ get; set; } 4 public string Nickname { get; set; } 5 public string Email { get; set; } 6 public string Password { get; set; } 7 public int RoleCode { get; set; } 8 public int StateCode { get; set; } 9 public string Mobile { get; set; } 10 public int Age { get; set; } 11 public DateTime RegisterTime { get; set; } 12 13 }
4 . 在應用的根目錄上新建DataAccess的文件夾,並新增兩個文件,IUserDao.cs和MySqlUserDao.cs,
其中IUserDao.cs是接口,定義對表的操作接口,MySqlUserDao.cs是類文件,繼承自IUserDao.cs,負責針對MySQL數據庫的操作。
IUserDao.cs代碼如下:
1 public interface IUserDao 2 { 3 public List<User> GetUserList(); 4 5 public void CreateUser(User user); 6 7 public void UpdateUserRole(int userId, int roleCode); 8 9 public void UpdateUserState(int userId, int stateCode); 10 11 public User GetUser(int userId); 12 13 public void UpdateUser(User user); 14 15 public void UpdatePassword(int userId, string oldPassword, string newPassword); 16 }
針對MySQL數據庫,我們使用MySQL官方提供的開發工具包來做數據庫的操作,在使用前,
需要先用"管理NeGet程序包(N)..."的功能引入MySql.Data開發工具包,如下:
輸入mysql后搜索即可看到此開發工具包,直接安裝就可以了,如下:
在MySqlUserDao.cs中用using MySql.Data.MySqlClient添加引用,然后就可以使用Connection/Command/Reader等對象了,偽代碼如下:
1 using System; 2 using System.Collections.Generic; 3 using MySql.Data.MySqlClient; 4 5 namespace WebApiDemo 6 { 7 public class MySqlUserDao : IUserDao 8 { 9 private MySqlConnection _conn; 10 11 public MySqlUserDao() 12 { 13 _conn = new MySqlConnection(); 14 } 15 16 public void CreateUser(User user) 17 { 18 try 19 { 20 //do database operation... 21 return; 22 } 23 catch { throw; } 24 finally { _conn.Close(); } 25 } 26 27 public User GetUser(int userId) 28 { 29 try 30 { 31 User user = new User() 32 { 33 UserId = 1, 34 Nickname = "張三", 35 Email = "zhangsan@suho.com", 36 Password = "56ftytysvr", 37 RoleCode = 1, 38 StateCode = 1, 39 Mobile = "", 40 Age = 28, 41 RegisterTime = DateTime.Now 42 }; 43 return user; 44 } 45 catch { throw; } 46 finally { _conn.Close(); } 47 } 48 49 public List<User> GetUserList() 50 { 51 try 52 { 53 User user1 = new User() 54 { 55 UserId = 1, 56 Nickname = "張三", 57 Email = "zhangsan@suho.com", 58 Password = "56ftytysvr", 59 RoleCode = 1, 60 StateCode = 1, 61 Mobile = "13856785678", 62 Age = 28, 63 RegisterTime = DateTime.Now 64 }; 65 User user2 = new User() 66 { 67 UserId = 2, 68 Nickname = "李四", 69 Email = "lisi@yahoo.com", 70 Password = "hjgh786dgsw", 71 RoleCode = 2, 72 StateCode = 1, 73 Mobile = "13866667777", 74 Age = 31, 75 RegisterTime = DateTime.Now 76 }; 77 78 List<User> list = new List<User>(); 79 list.Add(user1); 80 list.Add(user2); 81 return list; 82 } 83 catch { throw; } 84 finally { _conn.Close(); } 85 } 86 87 public void UpdatePassword(int userId, string oldPassword, string newPassword) 88 { 89 try 90 { 91 //do database operation... 92 return; 93 } 94 catch { throw; } 95 finally { _conn.Close(); } 96 } 97 98 public void UpdateUser(User user) 99 { 100 try 101 { 102 //do database operation... 103 return; 104 } 105 catch { throw; } 106 finally { _conn.Close(); } 107 } 108 109 public void UpdateUserRole(int userId, int roleCode) 110 { 111 try 112 { 113 //do database operation... 114 return; 115 } 116 catch { throw; } 117 finally { _conn.Close(); } 118 } 119 120 public void UpdateUserState(int userId, int stateCode) 121 { 122 try 123 { 124 //do database operation... 125 return; 126 } 127 catch { throw; } 128 finally { _conn.Close(); } 129 } 130 } 131 }
至此,數據庫的操作就准備好了,在具體的項目中,只需要在try{}語句中添加相關的數據庫操作代碼就可以了,
當然,如果使用的是SQL Server或Oracle等數據庫,需要添加相應的開發工具包。
注:在同一個項目中添加的所有類或者接口都盡可能放在同樣的命名空間下,這樣編碼會比較方便。