一,概述:
这个DataHelper 类是基于我上个博客里发的SQLDataAccess 这个类做的一个简单的封装,为了结合自己的实体类和数据操作而产生的。
这里面用了 属性类,反射。还有 数据类型的方法扩展。(入门的同学可以看看。)
这里面有几个地方要注意下,一个是 GetEntity<T> 方法里的 ModelDataAttribute 对象,是我自己写的一个实体属性类。
还有 connString.IsNullOrEmpty() 这个是我封装的一个Stirng 的扩展类(StringExtensions)里的方法。这两个类在下面可以找到。
这个DataHelper类,必须基于 SQLDataAccess 和我自己定义的实体类的形式才可以实现。 这里大家只是借鉴下就好了.
yeqw.FrameWork; 我把 ModelDataAttribute StringExtensions 封装到里面了。
二:代码:
DataHelper:(这里写的不是太好,其中的分页只是调用一个分页存储过程。这个Helper里最大的一个问题,就是不支持事务,一直没考虑好怎么把事务封进来)
1 namespace yeqw.DataHelper 2 { 3 using System; 4 using System.Collections.Generic; 5 using System.Data; 6 using System.Data.SqlClient; 7 using System.Reflection; 8 using System.Runtime.InteropServices; 9 using System.Text.RegularExpressions; 10 using yeqw.FrameWork; 11 using yeqw.sql.common.DB; 12 13 public class DBHelper 14 { 15 public static int DbExecuteNonQuery(SqlCommand Command) 16 { 17 using (SQLDataAccess access = new SQLDataAccess()) 18 { 19 return access.ExecuteNonQuery(Command); 20 } 21 } 22 23 public static int DbExecuteNonQuery(SqlCommand Command, string connString) 24 { 25 using (SQLDataAccess access = new SQLDataAccess(connString)) 26 { 27 return access.ExecuteNonQuery(Command); 28 } 29 } 30 31 public static object DbExecuteScalar(SqlCommand Command) 32 { 33 SQLDataAccess access = new SQLDataAccess(); 34 return access.ExecuteScalar(Command); 35 } 36 37 public static DataTable ExecProc_sp_GetPage(string tbName, string colName, int coltype, string orderby, string collist, int pagesize, int page, string condition, out int pages, out int rsCount, out int curCount) 38 { 39 return ExecProc_sp_GetPage(tbName, colName, coltype, orderby, collist, pagesize, page, condition, out pages, out rsCount, out curCount, ""); 40 } 41 42 public static DataTable ExecProc_sp_GetPage(string tbName, string colName, int coltype, string orderby, string collist, int pagesize, int page, string condition, out int pages, out int rsCount, out int curCount, string connstring) 43 { 44 SQLDataAccess access = null; 45 DataTable table; 46 if (connstring.IsNullOrEmpty()) 47 { 48 access = new SQLDataAccess(); 49 } 50 else 51 { 52 access = new SQLDataAccess(connstring); 53 } 54 try 55 { 56 SqlCommand command = GetCommand(tbName, colName, coltype, orderby, collist, pagesize, page, condition); 57 DataSet ds = new DataSet(); 58 access.FillDataSet(command, ds); 59 pages = (int)command.Parameters["@pages"].Value; 60 rsCount = (int)command.Parameters["@rsCount"].Value; 61 curCount = (int)command.Parameters["@curCount"].Value; 62 table = ds.Tables[1]; 63 } 64 catch (Exception exception) 65 { 66 throw new Exception(exception.Message); 67 } 68 finally 69 { 70 access.Dispose(); 71 } 72 return table; 73 } 74 75 76 public static List<T> FillData<T>(SqlCommand Command) where T : class, new() 77 { 78 return FillData<T>(Command, string.Empty); 79 } 80 81 public static List<T> FillData<T>(SqlCommand Command, string connString) where T : class, new() 82 { 83 List<T> list = new List<T>(); 84 SQLDataAccess access = null; 85 if (connString.IsNullOrEmpty()) 86 { 87 access = new SQLDataAccess(); 88 } 89 else 90 { 91 access = new SQLDataAccess(connString); 92 } 93 try 94 { 95 SqlDataReader reader = access.ExecuteReader(Command); 96 while (reader.Read()) 97 { 98 T entity = Activator.CreateInstance<T>(); 99 list.Add(GetEntity<T>(reader, entity)); 100 } 101 if (!reader.IsClosed) 102 { 103 reader.Close(); 104 } 105 } 106 finally 107 { 108 access.Dispose(); 109 } 110 return list; 111 } 112 113 public static T FillObject<T>(SqlCommand Command) where T : class, new() 114 { 115 return FillObject<T>(Command, string.Empty); 116 } 117 118 public static T FillObject<T>(SqlCommand Command, string connString) where T : class, new() 119 { 120 SQLDataAccess access = null; 121 if (connString.IsNullOrEmpty()) 122 { 123 access = new SQLDataAccess(); 124 } 125 else 126 { 127 access = new SQLDataAccess(connString); 128 } 129 SqlDataReader reader = null; 130 try 131 { 132 reader = access.ExecuteReader(Command); 133 if (reader.Read()) 134 { 135 return GetEntity<T>(reader, Activator.CreateInstance<T>()); 136 } 137 } 138 finally 139 { 140 if (!reader.IsClosed) 141 { 142 reader.Close(); 143 } 144 access.Dispose(); 145 } 146 return default(T); 147 } 148 149 private static SqlCommand GetCommand(string tbName, string colName, int coltype, string orderby, string collist, int pagesize, int page, string condition) 150 { 151 string cmdText = "sp_GetPage"; 152 SqlCommand command = new SqlCommand(cmdText) 153 { 154 CommandTimeout = 0, 155 CommandType = CommandType.StoredProcedure 156 }; 157 command.Parameters.Add("@tbName", SqlDbType.VarChar, 100); 158 command.Parameters["@tbName"].Value = tbName; 159 command.Parameters.Add("@colName", SqlDbType.VarChar, 100); 160 command.Parameters["@colName"].Value = colName; 161 command.Parameters.Add("@coltype", SqlDbType.Int, 4); 162 command.Parameters["@coltype"].Value = coltype; 163 command.Parameters.Add("@orderby", SqlDbType.VarChar, 100); 164 command.Parameters["@orderby"].Value = orderby; 165 command.Parameters.Add("@collist", SqlDbType.VarChar, 800); 166 command.Parameters["@collist"].Value = collist; 167 command.Parameters.Add("@pagesize", SqlDbType.Int, 4); 168 command.Parameters["@pagesize"].Value = pagesize; 169 command.Parameters.Add("@page", SqlDbType.Int, 4); 170 command.Parameters["@page"].Value = page; 171 command.Parameters.Add("@condition", SqlDbType.VarChar, 0x7d0); 172 command.Parameters["@condition"].Value = condition; 173 command.Parameters.Add("@pages", SqlDbType.Int); 174 command.Parameters.Add("@rsCount", SqlDbType.Int); 175 command.Parameters.Add("@curCount", SqlDbType.Int); 176 command.Parameters["@pages"].Direction = ParameterDirection.Output; 177 command.Parameters["@rsCount"].Direction = ParameterDirection.Output; 178 command.Parameters["@curCount"].Direction = ParameterDirection.Output; 179 return command; 180 } 181 182 public static DataSet GetDataSet(SqlCommand Command) 183 { 184 SQLDataAccess access = new SQLDataAccess(); 185 DataSet ds = new DataSet(); 186 try 187 { 188 access.FillDataSet(Command, ds); 189 } 190 finally 191 { 192 access.Dispose(); 193 } 194 return ds; 195 } 196 197 public static DataTable GetDataTable(SqlCommand Command) 198 { 199 SQLDataAccess access = new SQLDataAccess(); 200 DataTable table = null; 201 try 202 { 203 table = new DataTable(); 204 table = access.FillDataSet(Command); 205 } 206 finally 207 { 208 access.Dispose(); 209 } 210 return table; 211 } 212 213 public static DataTable GetDataTable(SqlCommand Command, string connString) 214 { 215 SQLDataAccess access = new SQLDataAccess(connString); 216 DataTable table = null; 217 try 218 { 219 table = new DataTable(); 220 table = access.FillDataSet(Command); 221 } 222 finally 223 { 224 access.Dispose(); 225 } 226 return table; 227 } 228 229 public static DataTable GetDataTableSchema(string TableName) 230 { 231 string cmdText = string.Format("select * from {0} where 1=2", TableName); 232 SQLDataAccess access = new SQLDataAccess(); 233 DataTable table = null; 234 try 235 { 236 SqlCommand command = new SqlCommand(cmdText); 237 table = new DataTable(); 238 table = access.FillDataSet(command); 239 } 240 finally 241 { 242 access.Dispose(); 243 } 244 return table; 245 } 246 247 public static DateTime GetDate() 248 { 249 DateTime now = DateTime.Now; 250 string sql = "SELECT getdate()"; 251 using (SQLDataAccess access = new SQLDataAccess()) 252 { 253 SqlDataReader reader = access.ExecuteReader(sql); 254 if (reader.Read()) 255 { 256 now = (DateTime)reader[0]; 257 } 258 if (!reader.IsClosed) 259 { 260 reader.Close(); 261 } 262 } 263 return now; 264 } 265 266 public static T GetEntity<T>(IDataReader reader, T entity) 267 { 268 PropertyInfo[] properties = entity.GetType().GetProperties(); 269 foreach (PropertyInfo info in properties) 270 { 271 object[] customAttributes = info.GetCustomAttributes(true); 272 if (customAttributes.Length != 0) 273 { 274 ModelDataAttribute attribute = (ModelDataAttribute)customAttributes[0]; 275 if (!(reader[attribute.SQLFieldName] is DBNull)) 276 { 277 info.SetValue(entity, reader[attribute.SQLFieldName], null); 278 } 279 } 280 } 281 return entity; 282 } 283 284 public static SqlParameter GetSqlParamseter(string parameterName, object Value) 285 { 286 return GetSqlParamseter(parameterName, SqlDbType.NVarChar, 0, Value); 287 } 288 289 public static SqlParameter GetSqlParamseter(string parameterName, SqlDbType dbType, int size, ParameterDirection Direction) 290 { 291 SqlParameter parameter = new SqlParameter 292 { 293 ParameterName = parameterName, 294 SqlDbType = dbType, 295 Direction = Direction 296 }; 297 if (size != 0) 298 { 299 parameter.Size = size; 300 } 301 return parameter; 302 } 303 304 public static SqlParameter GetSqlParamseter(string parameterName, SqlDbType dbType, int size, object Value) 305 { 306 SqlParameter parameter = new SqlParameter 307 { 308 ParameterName = parameterName, 309 SqlDbType = dbType 310 }; 311 if (((Value == null) || string.IsNullOrEmpty(Value.ToString())) || ((parameter.SqlDbType == SqlDbType.DateTime) && (DateTime.MinValue == Convert.ToDateTime(Value)))) 312 { 313 parameter.Value = DBNull.Value; 314 } 315 else 316 { 317 parameter.Value = Value; 318 } 319 if (size != 0) 320 { 321 parameter.Size = size; 322 } 323 return parameter; 324 } 325 326 public static void SetDBCommandParameters(SqlCommand Comm, object entity) 327 { 328 Comm.Parameters.RemoveAt(0); 329 if (Comm.Parameters.Count > 0) 330 { 331 PropertyInfo[] properties = entity.GetType().GetProperties(); 332 foreach (SqlParameter parameter in Comm.Parameters) 333 { 334 bool flag = false; 335 foreach (PropertyInfo info in properties) 336 { 337 object[] customAttributes = info.GetCustomAttributes(typeof(ModelDataAttribute), true); 338 if (customAttributes.Length != 0) 339 { 340 ModelDataAttribute attribute = (ModelDataAttribute)customAttributes[0]; 341 if (string.Equals(attribute.SQLFieldName, parameter.ParameterName.Replace("@", ""), StringComparison.CurrentCultureIgnoreCase)) 342 { 343 object obj2 = info.GetValue(entity, null); 344 parameter.SqlDbType = attribute.SQLDbType; 345 parameter.Size = attribute.SQLSize; 346 parameter.Direction = attribute.SQLParameterDirection; 347 if (obj2 != null) 348 { 349 parameter.Value = obj2; 350 } 351 else 352 { 353 parameter.Value = DBNull.Value; 354 } 355 flag = true; 356 break; 357 } 358 } 359 } 360 if (!flag) 361 { 362 throw new Exception("没有找到参数值!"); 363 } 364 } 365 } 366 } 367 368 public static void SetSqlParameters(SqlCommand comm, object entity) 369 { 370 comm.Parameters.Clear(); 371 string commandText = comm.CommandText; 372 PropertyInfo[] properties = entity.GetType().GetProperties(); 373 foreach (PropertyInfo info in properties) 374 { 375 object[] customAttributes = info.GetCustomAttributes(typeof(ModelDataAttribute), true); 376 if (customAttributes.Length >= 1) 377 { 378 ModelDataAttribute attribute = customAttributes[0] as ModelDataAttribute; 379 if (Regex.IsMatch(commandText, "@" + attribute.SQLFieldName + @"\b", RegexOptions.Singleline | RegexOptions.IgnoreCase)) 380 { 381 object obj2 = info.GetValue(entity, null); 382 SqlParameter parameter = comm.CreateParameter(); 383 parameter.SqlDbType = attribute.SQLDbType; 384 parameter.ParameterName = "@" + attribute.SQLFieldName; 385 parameter.Size = attribute.SQLSize; 386 parameter.Direction = attribute.SQLParameterDirection; 387 parameter.Value = (obj2 == null) ? DBNull.Value : obj2; 388 comm.Parameters.Add(parameter); 389 } 390 } 391 } 392 } 393 394 public static int UpdateDataTable(string TableName, DataTable dataTable) 395 { 396 SQLDataAccess access = new SQLDataAccess(); 397 SqlCommand selectCommand = new SqlCommand(string.Format("select * from {0} where 1=2", TableName)); 398 return access.UpdateDataTable(selectCommand, dataTable); 399 } 400 } 401 }