标签:
SqlHelper是一个基于.NET Framework的数据库操作组件。组件中包含数据库操作方法。SqlHelper用于简化你重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等。SqlHelper 封装过后通常是只需要给方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便。
sqlhelper是C#中abstract抽象类的典型应用
<connectionStrings> <!--SqlServerHelper连接字符串设定--> <add connectionString="server=.;uid=sa;pwd=123456;database=yourdatabase" name="SqlServerHelper"/> <!--Oracle连接字符串设定--> <add connectionString="Data Source=orcl;User Id=system;Password=***;Integrated Security=no" name="OracleHelper"/> <!--MySql连接字符串设定--> <add connectionString="server=localhost;uid=root;pwd=123456;database=mysqldatabase" name="MySqlHelper"/> </connectionStrings> <!--access连接字符串设定--> <add connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\mdbFilePath\db.mdb;Jet OLEDB:Database Password=123" name="MySqlHelper"/> </connectionStrings>
<appSettings> <add key="" value=""> </appSettings>
//查询语句执行: DataTable dt=SqlServerHelper.ReadTable("select * from table1"); //插入语句执行: SqlServerHelper.ExecuteNonQuery("insert into [students] values(@student_name,@class“), SqlServerHelper.CreateInputParameter("@student_name", SqlDbType.NVarChar, 100, txt_student_name_sqlserver.Text), SqlServerHelper.CreateInputParameter("@class", SqlDbType.NVarChar, 100, txt_class_sqlserver.Text) );
1 using (SqlServerHelper helper = new SqlServerHelper()) 2 { 3 helper.Command.CommandText = "delete from [Students] where stid=@stid"; 4 helper.AddParameter("@stid", SqlDbType. Int, student_id); 5 helper.Open(); 6 helper.ExecuteNoneQuery(); 7 helper.Command.Parameters.Clear(); 8 helper.Command.CommandText = "select * from [Students]";return helper.ReadTable(); 9 }
1 using (SqlServerHelper helper = new SqlServerHelper()) 2 { 3 helper.Command.CommandText = "delete from [Students] where stid=@stid"; 4 helper.AddParameter("@stid", SqlDbType. Int, student_id); 5 helper.Open(); 6 DbTransaction tran = helper.Connection.BeginTransaction(); 7 helper.Command.Transaction=tran; 8 try 9 { 10 helper.ExecuteNoneQuery(); 11 tran.Rollback(); 12 } 13 catch { tran.Rollback(); throw; } 14 }
1 (中文注释) 2 /// <summary> 3 /// 自定义访问通用类 4 /// </summary> 5 public class SqlDbHelper 6 { 7 string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString; 8 /// <summary> 9 ///构造函数 10 /// </summary> 11 public SqlDbHelper() 12 { 13 } 14 /// <summary> 15 /// ExecuteNonQuery操作,对数据库进行 增、删、改 操作((1) 16 /// </summary> 17 /// <param name="sql">要执行的SQL语句 </param> 18 /// <returns> </returns> 19 public int ExecuteNonQuery(string sql) 20 { 21 return ExecuteNonQuery(sql, CommandType.Text, null); 22 } 23 /// <summary> 24 /// ExecuteNonQuery操作,对数据库进行 增、删、改 操作(2) 25 /// </summary> 26 /// <param name="commandType">要执行的查询类型(存储过程、SQL文本) </param> 27 /// <returns> </returns> 28 public int ExecuteNonQuery(string sql, CommandType commandType) 29 { 30 return ExecuteNonQuery(sql, commandType, null); 31 } 32 /// <summary> 33 /// ExecuteNonQuery操作,对数据库进行 增、删、改 操作(3) 34 /// </summary> 35 /// <param name="parameters">参数数组 </param> 36 /// <returns> </returns> 37 public int ExecuteNonQuery(string sql, CommandType commandType,params SqlParameter[] parameters) 38 { 39 int count = 0; 40 { 41 using (SqlCommand command = new SqlCommand(sql, connection)) 42 { 43 command.CommandType = commandType; 44 if (parameters != null) 45 { 46 foreach (SqlParameter parameter in parameters) 47 { 48 command.Parameters.Add(parameter); 49 } 50 } 51 connection.Open(); 52 count = command.ExecuteNonQuery(); 53 } 54 } 55 return count; 56 } 57 /// <summary> 58 /// SqlDataAdapter的Fill方法查询,并返回一个DataSet类型结果(1) 59 /// </summary> 60 /// <returns> </returns> 61 { 62 return ExecuteDataSet(sql, CommandType.Text, null); 63 } 64 /// <summary> 65 /// SqlDataAdapter的Fill 方法 执行一 查询,并返回一个DataSet类型结果(2) 66 /// </summary> 67 /// <returns> </returns> 68 { 69 return ExecuteDataSet(sql, commandType, null); 70 } 71 /// <summary> 72 /// SqlDataAdapter的Fill方法执行查询,并返回一个DataSet类型结果(3) 73 /// </summary> 74 /// <returns> </returns> 75 public DataSet ExecuteDataSet(string sql, CommandType commandType, SqlParameter[] parameters) 76 { 77 DataSet ds = new DataSet(); 78 { 79 using (SqlCommand command = new SqlCommand(sql, connection)) 80 { 81 command.CommandType = commandType; 82 if (parameters != null) 83 { 84 foreach (SqlParameter parameter in parameters) 85 { 86 command.Parameters.Add(parameter); 87 } 88 } 89 SqlDataAdapter adapter = new SqlDataAdapter(command); 90 adapter.Fill(ds); 91 } 92 } 93 return ds; 94 } 95 /// <summary> 96 /// SqlDataAdapter的Fill方法执行个查询,并返回一个DataTable类型结果(1) 97 /// </summary> 98 /// <returns> </returns> 99 public DataTable ExecuteDataTable(string sql) 100 { 101 return ExecuteDataTable(sql, CommandType.Text, null); 102 } 103 /// <summary> 104 /// SqlDataAdapter的Fil执行一个查询,并返回一个DataTable类型结果(2) 105 /// </summary> 106 /// <returns> </returns> 107 public DataTable ExecuteDataTable (string sql, CommandType commandType) 108 { 109 return ExecuteDataTable(sql, commandType, null); 110 } 111 /// <summary> 112 /// SqlDataAdapter的Fill方法,执行一个查询,并返回一个DataTable类型结果(3) 113 /// </summary> 114 /// <returns> </returns> 115 public DataTable ExecuteDataTable(string sql, CommandType commandType, SqlParameter[] parameters) 116 { 117 DataTable data = new DataTable(); 118 using (SqlConnection connection = new SqlConnection(connectionString)) 119 { 120 using (SqlCommand command = new SqlCommand(sql, connection)) 121 { 122 command.CommandType = commandType; 123 if (parameters != null) 124 { 125 foreach (SqlParameter parameter in parameters) 126 { 127 command.Parameters.Add(parameter); 128 } 129 } 130 adapter.Fill(data); 131 } 132 } 133 return data; 134 } 135 /// <summary> 136 /// ExecuteReader执行一查询,返回一SqlDataReader对象实例(1) 137 /// </summary> 138 /// <returns> </returns> 139 public SqlDataReaderExecuteReader (string sql) 140 { 141 return ExecuteReader(sql, CommandType.Text, null); 142 } 143 /// <summary> 144 /// ExecuteReader执行一查询,返回一SqlDataReader对象实例(2) 145 /// </summary> 146 /// <returns> </returns> 147 publicSqlDataReader ExecuteReader(string sql, CommandType commandType) 148 { 149 return ExecuteReader(sql, commandType, null); 150 } 151 /// <summary> 152 /// ExecuteReader执行一查询,返回一SqlDataReader对象实例(3) 153 /// </summary> 154 /// <returns> </returns> 155 publicSqlDataReader ExecuteReader( string sql, CommandType commandType, SqlParameter[] parameters) 156 { 157 SqlConnection connection = new SqlConnection(connectionString); 158 SqlCommand command = new SqlCommand(sql, connection); 159 command.CommandType = commandType; 160 if (parameters != null) 161 { 162 foreach (SqlParameter parameter in parameters) 163 { 164 command.Parameters.Add(parameter); 165 } 166 } 167 connection.Open(); 168 return command.ExecuteReader(CommandBehavior.CloseConnection); 169 } 170 /// <summary> 171 /// ExecuteScalar执行一查询,返回查询结果的第一行第一列(1) 172 /// </summary> 173 /// <returns> </returns> 174 public Object ExecuteScalar(string sql) 175 { 176 return ExecuteScalar(sql, CommandType.Text, null); 177 } 178 /// <summary> 179 /// ExecuteScalar执行一查询,返回查询结果的第一行第一列(2) 180 /// </summary> 181 /// <returns> </returns> 182 public Object ExecuteScalar( string sql, CommandType commandType) 183 { 184 return ExecuteScalar(sql, commandType, null); 185 } 186 /// <summary> 187 /// ExecuteScalar执行一查询,返回查询结果的第一行第一列(3) 188 /// </summary> 189 /// <returns> </returns> 190 { 191 object result = null; 192 { 193 using (SqlCommand command = new SqlCommand(sql, connection)) 194 { 195 command.CommandType = commandType; 196 if (parameters != null) 197 { 198 foreach (SqlParameter parameter in parameters) 199 { 200 command.Parameters.Add(parameter); 201 } 202 } 203 connection.Open(); 204 result = command.ExecuteScalar(); 205 } 206 } 207 return result; 208 } 209 /// <summary> 210 /// 返回当前连接的数据库中所有由用户创建的数据库 211 /// </summary> 212 /// <returns> </returns> 213 public DataTable GetTables() 214 { 215 DataTable data = null; 216 { 217 connection.Open(); 218 data = connection.GetSchema("Tables"); 219 } 220 return data; 221 } 222 }
标签:
原文地址:http://www.cnblogs.com/JaCkQinChao/p/5256579.html