第一、DAL、BLL、Model必须为类库,UI可以为控制台、winform、asp.net等;
第二、SqlHelper放在DAL中,但ConnectionString配置在UI的config中,这是因为ConfigurationManager是读取UI项目(入口启动项目)的config;
第三、Executescalar(),ExecuteReader()不仅可以用来执行select查询,还可以用来执行insert等操作;
第四、ADO.Net中想给SqlParameter的参数赋值null的话,必须传递Dbnull.Value;
第五、在c#代码中写sql语句bool类型数据必须写1或0;
案例:
1 using SanCeng.Model; 2 using System; 3 using System.Collections.Generic; 4 using System.Data; 5 using System.Data.SqlClient; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 10 namespace SanCeng.DAL 11 { 12 public class UserDAL 13 { 14 /// <summary> 15 /// 新增 16 /// </summary> 17 /// <param name="userName"></param> 18 /// <param name="password"></param> 19 /// <param name="phoneNum"></param> 20 /// <param name="age"></param> 21 /// <returns></returns> 22 public long Add(string userName, string password, string phoneNum, int? age) 23 { 24 object obj = SqlHelper.ExecuteScalar("insert into T_Users(UserName,Password,PhoneNum,Age,IsDeleted) output inserted.id values(@UserName,@Password,@PhoneNum,@Age,0)", new SqlParameter("@UserName", userName), new SqlParameter("@Password", password), new SqlParameter("@PhoneNum", phoneNum),new SqlParameter("@Age",age==null?(object)DBNull.Value:age)); 25 return Convert.ToInt64(obj); 26 } 27 28 /// <summary> 29 /// 软删除id为主键的数据 30 /// </summary> 31 /// <param name="id"></param> 32 public void Delete(long id) 33 { 34 SqlHelper.ExecuteNonQuery("Update T_Users set IsDeleted=1 where [email protected]", new SqlParameter("@Id", id)); 35 } 36 private User ToUser(DataRow row) 37 { 38 User user = new User(); 39 user.Id = (long)row["Id"]; 40 user.Age = row.IsNull("Age") ? null : (int?)row["Age"]; 41 user.Password = (string)row["Password"]; 42 user.IsDeleted = (bool)row["IsDeleted"]; 43 user.phoneNum = (string)row["UserName"]; 44 user.UserName = (string)row["UserName"]; 45 return user; 46 } 47 48 public void Update(User user) 49 { 50 SqlHelper.ExecuteNonQuery("Update T_Users set [email protected],[email protected],[email protected],[email protected] where [email protected]", new SqlParameter("@UserName", user.UserName), new SqlParameter("@Password", user.Password), new SqlParameter("@PhoneNum", user.phoneNum), new SqlParameter("@Age", user.Age == null ? (object)DBNull.Value : user.Age), new SqlParameter("@Id", user.Id)); 51 } 52 public User GetById(long id) 53 { 54 DataTable table = SqlHelper.ExecuteQuery("select * from T_Users where [email protected]", new SqlParameter("@Id", id)); 55 if(table.Rows.Count<=0) 56 { 57 return null; 58 } 59 else if(table.Rows.Count>1) 60 { 61 throw new ApplicationException("找到多条id="+id+"的数据"); 62 } 63 else 64 { 65 DataRow row = table.Rows[0]; 66 return ToUser(row); 67 } 68 } 69 /// <summary> 70 /// 返回所有未软删除的数据 71 /// </summary> 72 /// <returns></returns> 73 public IEnumerable<User> GetAll() 74 { 75 DataTable dt = SqlHelper.ExecuteQuery("select * fom T_User where IsDeleted = 0"); 76 List<User> list = new List<User>(); 77 foreach(DataRow row in dt.Rows) 78 { 79 list.Add(ToUser(row)); 80 } 81 return list; 82 } 83 } 84 85 86 87 }
1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.Data; 5 using System.Data.SqlClient; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 10 namespace SanCeng.DAL 11 { 12 class SqlHelper 13 { 14 private static string connStr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; 15 public static void ExecuteNonQuery(string sql, params SqlParameter[] parameters) 16 { 17 using (SqlConnection conn = new SqlConnection(connStr)) 18 using (SqlCommand cmd = conn.CreateCommand()) 19 { 20 conn.Open(); 21 cmd.CommandText = sql; 22 cmd.Parameters.AddRange(parameters); 23 cmd.ExecuteNonQuery(); 24 } 25 } 26 public static object ExecuteScalar(string sql, params SqlParameter[] parameters) 27 { 28 using (SqlConnection conn = new SqlConnection(connStr)) 29 using (SqlCommand cmd = conn.CreateCommand()) 30 { 31 conn.Open(); 32 cmd.CommandText = sql; 33 cmd.Parameters.AddRange(parameters); 34 return cmd.ExecuteScalar(); 35 } 36 } 37 public static DataTable ExecuteQuery(string sql, params SqlParameter[] parameters) 38 { 39 DataTable table = new DataTable(); 40 using (SqlConnection conn = new SqlConnection(connStr)) 41 using (SqlCommand cmd = conn.CreateCommand()) 42 { 43 conn.Open(); 44 cmd.CommandText = sql; 45 cmd.Parameters.AddRange(parameters); 46 using (SqlDataReader reader = cmd.ExecuteReader()) 47 { 48 table.Load(reader); 49 return table; 50 } 51 } 52 } 53 } 54 }
1 using SanCeng.DAL; 2 using SanCeng.Model; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace SanCeng.BLL 10 { 11 public class UserBLL 12 { 13 private UserDAL uDAL = new UserDAL(); 14 public long Add(string userName,string password,string phoneNum,int? age) 15 { 16 return uDAL.Add(userName, password, phoneNum, age); 17 } 18 public void Delete(long id) 19 { 20 uDAL.Delete(id); 21 } 22 public void Update(User user) 23 { 24 uDAL.Update(user); 25 } 26 public User GetById(long id) 27 { 28 return uDAL.GetById(id); 29 } 30 public IEnumerable<User> GetAll() 31 { 32 return uDAL.GetAll(); 33 } 34 } 35 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SanCeng.Model 8 { 9 public class User 10 { 11 public long Id { get; set; } 12 public int? Age { get; set; } 13 public string phoneNum { get; set; } 14 public string Password { get; set; } 15 public bool IsDeleted { get; set; } 16 public string UserName { get; set; } 17 } 18 }
1 using SanCeng.BLL; 2 using System; 3 using System.Collections.Generic; 4 using System.ComponentModel; 5 using System.Data; 6 using System.Drawing; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 12 namespace SanCeng.WinForm 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 21 private void button1_Click(object sender, EventArgs e) 22 { 23 UserBLL bll = new UserBLL(); 24 25 bll.Delete(1); 26 } 27 } 28 }