ADO ------(二) 把ADO.NET封装成数据库数据成C#对象
要封装成类我们的先导入命名空间:
using System.Data;
using System.Data.SqlClient;
然后把非查询语句封装:
执行非查询语句
<param name="sql">insert,update,delete语句</param>
<returns>影响的行数</returns>
View Code
1 public int ExecuteNonQuery(string sql) 2 { 3 4 PreparedCommand(sql, null); 5 6 int i = cmd.ExecuteNonQuery(); 7 Close(); 8 return i; 9 }
执行查询语句
View Code
1 public SqlDataReader ExecuteQuery(string sql) 2 { 3 PreparedCommand(sql, null); 4 return cmd.ExecuteReader(); 5 }
那我们就试试封装吧:
View Code
1 using System.Data; 2 using System.Data.SqlClient; 3 namespace Com.HS.DBHelper 4 { 5 public class SqlHelper 6 { 7 private SqlConnection con = null; 8 private SqlCommand cmd = null; 9 private string connectionString; 10 /// <summary> 11 /// 获取连接字符串 12 /// </summary> 13 public string ConnectionString 14 { 15 get { return this.connectionString; } 16 } 17 /// <summary> 18 /// 构造函数 19 /// </summary> 20 /// <param name="strcon">连接字符串</param> 21 public SqlHelper(string strcon) 22 { 23 con = new SqlConnection(strcon); 24 cmd = new SqlCommand(); 25 cmd.Connection = con; 26 this.connectionString = strcon; 27 } 28 /// <summary> 29 /// 执行非查询语句 30 /// </summary> 31 /// <param name="sql">insert,update,delete语句</param> 32 /// <param name="param">参数</param> 33 /// <returns>影响的行数</returns> 34 public int ExecuteNonQuery(string sql, params SqlParameter[] param) 35 { 36 PreparedCommand(sql, param); 37 int i = cmd.ExecuteNonQuery(); 38 Close(); 39 return i; 40 } 41 /// <summary> 42 /// 执行非查询语句 43 /// </summary> 44 /// <param name="sql">insert,update,delete语句</param> 45 /// <returns>影响的行数</returns> 46 public int ExecuteNonQuery(string sql) 47 { 48 49 PreparedCommand(sql, null); 50 51 int i = cmd.ExecuteNonQuery(); 52 Close(); 53 return i; 54 } 55 /// <summary> 56 /// 执行查询 57 /// </summary> 58 /// <param name="sql">查询语句</param> 59 /// <param name="param">参数</param> 60 /// <returns>SqlDataReader</returns> 61 public SqlDataReader ExecuteQuery(string sql, params SqlParameter[] param) 62 { 63 PreparedCommand(sql, param); 64 return cmd.ExecuteReader(); 65 } 66 /// <summary> 67 /// 执行查询 68 /// </summary> 69 /// <param name="sql">查询语句</param> 70 /// <returns>SqlDataReader</returns> 71 public SqlDataReader ExecuteQuery(string sql) 72 { 73 PreparedCommand(sql, null); 74 return cmd.ExecuteReader(); 75 } 76 /* 77 用来初始化Command对象 78 */ 79 private void PreparedCommand(string sql, params SqlParameter[] param) 80 { 81 /* 82 SqlCommand cmd1 = null; 83 if(cmd!=null) 84 { 85 cmd1 = cmd; 86 } 87 else{ 88 cmd1 = new SqlCommand(); 89 }*/ 90 cmd.CommandText = sql; 91 //清空Parameters中的参数 92 cmd.Parameters.Clear(); 93 if (param != null) 94 { 95 foreach (SqlParameter p in param) 96 { 97 cmd.Parameters.Add(p); 98 } 99 } 100 con.Open(); 101 } 102 103 /* 104 打开数据库 105 */ 106 private void Open() 107 { 108 con.Open(); 109 } 110 /* 111 关闭数据库 112 */ 113 /// <summary> 114 /// 关闭数据库 115 116 /// </summary> 117 118 119 public void Close() 120 { 121 122 cmd.Dispose(); 123 con.Close(); 124 } 125 126 } 127 }
嗯我们还可以把存储过程,事物还有DataTable也封装到DBHelper类里,那就再试试封装?...
可能会有点遗漏希望大家多指点...