这一篇,我们编写SQLHelper这个类的目的是为了封装对数据库的操作。
1. 封装方法的原则
把不变的代码写入方法中,把变化的部分通过参数传递
不变的代码:连接数据、执行数据库操作的方法等
变化的部分:SQL语句,进行参数化查询的时候需要传递的参数
2. 实现SQLHelper类
1 //不声明为publc的目的:这个类只在程序集当中使用,不必对外。 2 //尽可能地对外少暴露publc类 3 class SQLHelper 4 { 5 //连接数据库的字符串 6 private static string strConn = ConfigurationManager.ConnectionStrings["dbconStr"].ConnectionString; 7 8 /// <summary> 9 /// 执行非查询sql语句,如insert、delete、update 10 /// </summary> 11 /// <param name="sqlCmd">要执行的sql语句</param> 12 /// <param name="parameters">sql语句中的参数</param> 13 /// <returns>执行语句后,受到影响的行数</returns> 14 public static int ExecuteNoQuery(string sqlCmd, params SqlParameter[] parameters) 15 { 16 using (SqlConnection conn = new SqlConnection(strConn)) 17 { 18 conn.Open(); 19 using (SqlCommand cmd = conn.CreateCommand()) 20 { 21 cmd.CommandText = sqlCmd; 22 cmd.Parameters.AddRange(parameters); 23 return cmd.ExecuteNonQuery(); 24 } 25 } 26 } 27 28 /// <summary> 29 /// 一般用于返回一个数据的查询数据,如查询一个学生的数学成绩 30 /// </summary> 31 /// <param name="sqlCmd">要执行的sql语句</param> 32 /// <param name="parameters">sql语句中的参数</param> 33 /// <returns>查询结果,object类型</returns> 34 public static object ExecuteScalar(string sqlCmd, params SqlParameter[] parameters) 35 { 36 using (SqlConnection conn = new SqlConnection(strConn)) 37 { 38 conn.Open(); 39 using (SqlCommand cmd = conn.CreateCommand()) 40 { 41 cmd.CommandText = sqlCmd; 42 cmd.Parameters.AddRange(parameters); 43 return cmd.ExecuteScalar(); 44 } 45 } 46 } 47 48 /// <summary> 49 /// 将查询结果以DataSet的形式返回 50 /// </summary> 51 /// <param name="sqlCmd">要执行的sql语句</param> 52 /// <param name="parameters">sql语句中的参数</param> 53 /// <returns>数据集合,DataSet类型</returns> 54 public static DataSet ExecuteDataSet(string sqlCmd, params SqlParameter[] parameters) 55 { 56 using (SqlConnection conn = new SqlConnection(strConn)) 57 { 58 conn.Open(); 59 using (SqlCommand cmd = conn.CreateCommand()) 60 { 61 cmd.CommandText = sqlCmd; 62 cmd.Parameters.AddRange(parameters); 63 SqlDataAdapter adpter = new SqlDataAdapter(cmd); 64 DataSet dataset = new DataSet(); 65 adpter.Fill(dataset); 66 return dataset; 67 } 68 } 69 } 70 71 /// <summary> 72 /// 执行查询结果数据量较大的查询语句,如查询1000个学生的信息 73 /// </summary> 74 /// <param name="sqlCmd">要执行的sql语句</param> 75 /// <param name="parameters">sql语句中的参数</param> 76 /// <returns>一个Staff类型的List</returns> 77 public static List<Staff> ExecuteReader(string sqlCmd, params SqlParameter[] parameters) 78 { 79 using (SqlConnection conn = new SqlConnection(strConn)) 80 { 81 conn.Open(); 82 using (SqlCommand cmd = conn.CreateCommand()) 83 { 84 cmd.CommandText = sqlCmd; 85 cmd.Parameters.AddRange(parameters); 86 87 SqlDataReader reader = cmd.ExecuteReader(); 88 List<Staff> lStaff = new List<Staff>(); 89 while (reader.Read()) 90 { 91 lStaff.Add(new Staff() 92 { 93 Name = (string)reader["Name"], 94 Age = (int)reader["Age"], 95 Sex = (bool)reader["Sex"], 96 Height = (decimal)reader["Height"], 97 Salary = (decimal)reader["Salary"], 98 //数据库中有些数据可以为空,如果使用强制转换的话,会出现错误 99 //应该使用as来转换 100 Department = reader["Department"] as string 101 }); 102 } 103 return lStaff; 104 } 105 } 106 } 107 }
3. 调用SQLHelper类
string sqlCmd = "insert into t_staff(Name,Age,Sex,Height,Salary) values('郑冰',27,1,1.64,3000)"; SQLHelper.ExecuteNoQuery(sqlCmd);
string sqlCmd = "select Salary from t_staff where id > 2"; DataSet dataset = SQLHelper.ExecuteDataSet(sqlCmd); DataTable table = dataset.Tables[0]; foreach(DataRow row in table.Rows) { MessageBox.Show(row[0].ToString() + " == " + row["Salary"].ToString()); }
string sqlCmd = "select Salary from t_staff where Name = @value"; object salary = SQLHelper.ExecuteScalar(sqlCmd, new SqlParameter("@value", "马金")); MessageBox.Show(salary.ToString());
string sqlCmd = "select * from t_staff"; List<Staff> lStaff = SQLHelper.ExecuteReader(sqlCmd);