Simple web layer-3 architecture system [first version], web layer-3 first version
SQLhelper helper class writing:
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 using System. data; 8 using System. data. sqlClient; 9 using System. configuration; 10 11 namespace DAL12 {13 public class SQLHelper14 {15 SqlCommand cmd = null; 16 17 public string strcon () 18 {19 string strcon = ConfigurationManager. connectionStrings ["strcon"]. connectionStrin G; 20 21 return strcon; 22} 23 24 public SqlConnection getcon () 25 {26 SqlConnection con = new SqlConnection (strcon (); 27 28 if (con. state = ConnectionState. closed) 29 {30 con. open (); 31} 32 33 return con; 34} 35 36 // <summary> 37 // execute the SQL statement 38 /// </summary> 39 // <param name = "SQL"> The SQL statement to be executed </param> 40 // <returns> returns the number of rows affected by the execution of the SQL statement </returns> 41 public int ExecuteNonQuery (string SQL) 42 {43 int res; 44 45 try46 {47 cmd = new SqlCommand (SQL, getcon (); 48 49 res = cmd. executeNonQuery (); 50} 51 catch (Exception ex) 52 {53 throw ex; 54} 55 finally56 {57 if (getcon (). state = ConnectionState. open) 58 {59 getcon (). close (); 60} 61} 62 63 return res; 64} 65 66 // <summary> 67 // execute the passed SQL query statement 68 /// </summary> 69 /// <param name = "SQL"> query SQL statement to be executed </param> 70 // <returns> return the dataset of the query SQL statement </returns> 71 public DataTable E XecuteQuery (string SQL) 72 {73 DataTable dt = new DataTable (); 74 75 SqlConnection con = new SqlConnection (strcon (); 76 77 // create a SqlCommand object cmd, connect the database to point to SQL statements. 78 cmd = new SqlCommand (SQL, getcon (); 79 80 // The database connected to by executing cmd. After using, close the sdr directly after execution. No need to write sdr. closed.81 using (SqlDataReader sdr = cmd. ExecuteReader () 82 {83 dt. Load (sdr); // Load is suitable for SqlDataReader. For SqlDataAdapter, the Fill method is used. 84} 85 86 getcon (). Close (); 87 88 return dt; 89} 90} 91}
Write the personDAO employee operation class:
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 using System. data; 8 using System. data. sqlClient; 9 10 namespace DAL 11 {12 public class personDAO 13 {14 SQLHelper sq = null; 15 16 public personDAO () 17 {18 sq = new SQLHelper (); 19} 20 21 // <summary> 22 // Add employee information 23 // </summary> 24 // <param name = "name"> Employee name </param> 25 // <param name = "sex"> the gender of the employee to be added </param> 26 /// <param name = "salary"> Yes the added employee's salary </param> 27 // <returns> returns the true value: if it is true, it is successfully added. If it is false, adding failed </returns> 28 public bool insert (string name, string sex, string salary) 29 {30 bool flag = false; 31 32 string SQL = "insert into person ([name], sex, salary) values ('" + name + "', '" + sex + "', '"+ salary +"') "; 33 34 if (sq. executeNonQuery (SQL)> 0) 35 {36 fl Ag = true; 37} 38 39 return flag; 40} 41 42 // <summary> 43 // Delete employee information 44 // </summary> 45 // <param name = "id"> Delete employee </param> 46 // <returns> returns the true value: if the deletion is successful, if the deletion is false, the deletion fails. </returns> 47 public bool delete (string id) 48 {49 bool flag = false; 50 51 string SQL = "delete from person where id = '" + id + "'"; 52 53 if (sq. executeNonQuery (SQL)> 0) 54 {55 flag = true; 56} 57 58 return flag; 59} 6 0 61 // <summary> 62 // change employee information 63 // </summary> 64 // <param name = "id"> employee id to be changed </param> 65 // <param name = "name"> name of the employee to be changed </param> 66 // <param name = "sex"> employee gender </param> 67 // <param name = "salary"> the employee's salary to be changed </param> 68 // <returns> returns the true value: if the change is true, the change is successful. If the change is false, the change fails. </returns> 69 public bool update (string id, string name, string sex, string salary) 70 {71 bool flag = false; 72 73 string SQL = "updat E person set [name] = '"+ name +"', sex = '"+ sex + "', salary = '"+ salary +" 'where id =' "+ id +" '"; 74 75 if (sq. executeNonQuery (SQL)> 0) 76 {77 flag = true; 78} 79 80 return flag; 81} 82 83 // <summary> 84 // judge whether the employee name is repeated 85 // </summary> 86 // <param name = "name"> Yes the name of the employee to be judged </param> 87 // <returns> returns the true value: if it is true, it indicates duplicate. If it is false, add </returns> 88 public bool repeat (string name) 89 {90 bool flag = false; 91 92 string SQL = "select * from person where [name] = '" + name + "'"; 93 94 # create a new virtual table if region is written in this way, if you use the following method directly, you do not need to establish the row number judgment. 95 // DataTable dt = sq. executeQuery (SQL); 96 97 // if (dt. rows. count> 0) // dt. rows. the Count method checks whether the returned virtual table has data. If yes, the number of rows is not zero. If not, the number of rows is zero. 98 // {99 // flag = true; 100 //} 101 # endregion102 103 if (sq. executeQuery (SQL ). rows. count> 0) // dt. rows. the Count method checks whether the returned virtual table has data. If yes, the number of rows is not zero. If not, the number of rows is zero. 104 {105 flag = true; 106} 107 108 return flag; 109} 110} 111}