使用 ADO.NET 二次封装ORM框架的数据库操作帮助类
简介
工作中大量需要多种不同数据格式互相转换,通过ADO.NET实现可视化数据转换工具,目前支持关系型数据库SqlServer、Oracle、MySql、Access、SQLite。
设计
简易的ORM框架,多种数据库操作封装为一套帮助类中,后期使用不需要过多考虑数据库类型,以及减少在代码中拼写SQL语句。近乎通用的连接方式以及增删改查,支持事务处理。
帮助类、官方文档及其调用方式
SQLServer:
SqlServerHelper 引用 System.Data.SqlClient 库
调用方式:
1 2 3 4
| SqlServerHelper sqlHelper = new SqlServerHelper(); sqlHelper.SqlServerConnectionString(string server, string database, string uid, string pwd); sqlHelper.Open(); sqlHelper.Close();
|
Oracle:
OracleHelper 引用 System.Data.OracleClient 库
调用方式:
1 2 3 4
| OracleHelper sqlHelper = new OracleHelper(); sqlHelper.OracleConnectionString(string Source, string Id, string Password); sqlHelper.Open(); sqlHelper.Close();
|
MySQL:
MySqlHelper 引用 MySql.Data.MySqlClient 库
调用方式:
1 2 3 4
| MySqlHelper sqlHelper = new MySqlHelper(); sqlHelper.MySqlConnectionString(string server, string id, string password, string database); sqlHelper.Open(); sqlHelper.Close();
|
Access:
AccessHelper 引用 System.Data.OleDb 库
调用方式:
1 2 3 4 5
| AccessHelper sqlHelper = new AccessHelper(); sqlHelper.AccessConnectionPath_Office2003(string source); sqlHelper.AccessConnectionPath_Office2007(string source); sqlHelper.Open(); sqlHelper.Close();
|
SQLite:
SQLiteHelper 引用 System.Data.SQLite 库
调用方式:
1 2 3 4
| SQLiteHelper sqlHelper = new SQLiteHelper(); sqlHelper.SQLiteConnectionPath(string source); sqlHelper.Open(); sqlHelper.Close();
|
数据转换帮助类
由于每种数据库的字段类型、字符格式长度等不一致,所以专门写了一个用于互相兼容的帮助类,用于自动识别源数据库与目标数据库差异,自动修改。
数据转换帮助类
删除DataTable中的空行
数据源以C#基础类型DataTable传递,在实际使用中存在空行导致异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
public static DataTable RemoveEmpty(DataTable dtDataSource) { try { List<DataRow> listRemove = new List<DataRow>(); for (int i = 0; i < dtDataSource.Rows.Count; i++) { bool IsNull = true; for (int j = 0; j < dtDataSource.Columns.Count; j++) { if (!string.IsNullOrEmpty(dtDataSource.Rows[i][j].ToString().Trim())) { IsNull = false; } } if (IsNull) { listRemove.Add(dtDataSource.Rows[i]); } } for (int i = 0; i < listRemove.Count; i++) { dtDataSource.Rows.Remove(listRemove[i]); } } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); } return dtDataSource; }
|
DataTable与List互相转换
实际使用中List比DataTable更方便使用,提供互相转换方法方便开发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
public static List<T> ConvertToList<T>(DataTable dtDataSource) where T : class,new() { try { List<T> listT = new List<T>(); foreach (DataRow drDataSource in dtDataSource.Rows) { T t = new T(); PropertyInfo[] propertyInfos = t.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in propertyInfos) { string tempName = propertyInfo.Name; if (dtDataSource.Columns.Contains(tempName)) { if (!propertyInfo.CanWrite) continue; object value = drDataSource[tempName]; if (value != DBNull.Value) { if (propertyInfo.GetMethod.ReturnParameter.ParameterType.Name == "Int32") { value = Convert.ToInt32(value); } propertyInfo.SetValue(t, value, null); } } } listT.Add(t); } return listT; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); } return null; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
public static DataTable ConvertDataTable(IList listDataSource) { try { DataTable dataTable = new DataTable(); if (listDataSource.Count > 0) { PropertyInfo[] propertyInfos = listDataSource[0].GetType().GetProperties(); foreach (PropertyInfo propertyInfo in propertyInfos) { dataTable.Columns.Add(propertyInfo.Name, propertyInfo.PropertyType); } foreach (var vDataSource in listDataSource) { ArrayList arrayList = new ArrayList(); foreach (PropertyInfo propertyInfo in propertyInfos) { arrayList.Add(propertyInfo.GetValue(vDataSource, null)); } dataTable.LoadDataRow(arrayList.ToArray(), true); } } return dataTable; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); } return null; }
|
String转Unicode,并去除’\ufeff’非法字符
个别数据中存在有非法字符,避免异常,转换时批量处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
public static string RemoveIllegal(string strSource) { return UnicodeToString(StringToUnicode(strSource)); }
public static string StringToUnicode(string strSource) { StringBuilder stringBuilder = new StringBuilder(); byte[] bytes = Encoding.Unicode.GetBytes(strSource); for (int i = 0; i < bytes.Length; i += 2) { stringBuilder.AppendFormat("\\u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0')); } return stringBuilder.Replace("\\ufeff", string.Empty).ToString(); }
public static string UnicodeToString(string strSource) { return new Regex(@"\\u([0-9A-F]{4})", RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace(strSource, x => string.Empty + Convert.ToChar(Convert.ToUInt16(x.Result("$1"), 16))); }
|
字段类型转换
由于每种数据库字段类型及字段长度和主键不一致,根据每种目标数据库做单独修改
代码过长,请查阅目录下方法 TypeProcessing