封装List集合一个批量导入数据库的工具类
1 public class CommonDal 2 { 3 #region 数据导入相关 4 /// <summary> 5 /// 批量导入数据 6 /// </summary> 7 /// <param name="dt">将要导入的数据源</param> 8 /// <param name="TbName">目标表名称</param> 9 public static void Bulk_ImportDatable(DataTable dt, string TargetTableName) 10 { 11 using (SqlConnection destinationConnection = DBHelper.CreateConnection()) 12 { 13 using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection)) 14 { 15 try 16 { 17 bulkCopy.DestinationTableName = TargetTableName;//要插入的表的表明 18 for (int i = 0; i < dt.Columns.Count; i++) 19 { 20 bulkCopy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName); 21 } 22 bulkCopy.WriteToServer(dt); 23 } 24 catch (Exception ex) 25 { 26 Console.WriteLine(ex.Message); 27 } 28 } 29 } 30 31 } 32 #endregion 33 #region 数据表操作相关 34 #region 增 35 /// <summary> 36 /// 向数据表中插入一条数据 37 /// </summary> 38 /// <typeparam name="T">插入数据的参数类型,注意名称和表明保持一致</typeparam> 39 /// <param name="obj">插入的数据对象</param> 40 /// <returns></returns> 41 public int InsertInfo<T>(T obj) where T : class 42 { 43 using (IDbConnection conn = DBHelper.CreateConnection()) 44 { 45 return conn.Insert<T>(obj); 46 } 47 } 48 #endregion 49 #region 删除 50 /// <summary> 51 /// 通过表名称truncate掉表数据 52 /// </summary> 53 /// <param name="tablename">需要清除数据的表名称</param> 54 /// <returns></returns> 55 public bool truncateTableByName(string tablename) 56 { 57 using (IDbConnection conn = DBHelper.CreateConnection()) 58 { 59 string sql = " TRUNCATE TABLE " + tablename; 60 DynamicParameters param = new DynamicParameters(); 61 param.Add("TableName", tablename); 62 return conn.Execute(sql, param) > 0; 63 } 64 } 65 /// <summary> 66 /// 将主表数据清除并且清除部分关联信息表的关联数据 67 /// </summary> 68 /// <param name="truncateTableName">要清除数据的主表名称</param> 69 /// <param name="deleteTableName">要清除关联数据的关联信息表名称</param> 70 /// <param name="sqlWhereList">清除关联信息数据的Where条件</param> 71 public void truncateAndDeleteTable(string truncateTableName, string deleteTableName, List<SqlWhereModel> sqlWhereList) 72 { 73 truncateTableByName(truncateTableName); 74 deleteTableByName(deleteTableName, sqlWhereList); 75 } 76 77 /// <summary> 78 /// 通过表名称和Where条件delete掉表数据 79 /// </summary> 80 /// <param name="TableName">需要清除数据的表名称</param> 81 /// <param name="paramModel">Where参数集合</param> 82 /// <returns></returns> 83 public bool deleteTableByName(string TableName, List<SqlWhereModel> paramModel) 84 { 85 using (IDbConnection conn = DBHelper.CreateConnection()) 86 { 87 //sql语句拼接时使用StringBulider 防止长度限制 88 StringBuilder sql = new StringBuilder("delete from " + TableName); 89 StringBuilder sbwhere = new StringBuilder(" where 1=1 "); 90 DynamicParameters parameters = new DynamicParameters(); 91 paramModel.ForEach(p => 92 { 93 sbwhere.AppendLine(" and " + p.ParamName + " = @" + p.ParamValue); 94 parameters.Add(p.ParamValue); 95 }); 96 97 return conn.Execute(sql.AppendLine(sbwhere.ToString()).ToString(), parameters) > 0; 98 } 99 } 100 /// <summary> 101 /// 通过表名称delete掉表数据 102 /// </summary> 103 /// <param name="TableName">需要清除数据的表名称</param> 104 /// <returns></returns> 105 public bool deleteTableByName(string TableName) 106 { 107 using (IDbConnection conn = DBHelper.CreateConnection()) 108 { 109 //sql语句拼接时使用StringBulider 防止长度限制 110 StringBuilder sql = new StringBuilder(@"delete from " + TableName); 111 return conn.Execute(sql.ToString()) > 0; 112 } 113 } 114 #endregion 115 #region 修改 116 117 #endregion 118 #region 查询 119 /// <summary> 120 /// 通过id判断是否存在记录 121 /// </summary> 122 /// <param name="dc">传入id</param> 123 /// <returns>返回泛型对象</returns> 124 public T getExistsById<T>(long id) where T : class 125 { 126 using (IDbConnection coon = DBHelper.CreateConnection()) 127 { 128 return coon.Get<T>(id); 129 } 130 } 131 /// <summary> 132 /// 通过name查询是否存在 133 /// </summary> 134 /// <typeparam name="T"></typeparam> 135 /// <param name="Name"></param> 136 /// <returns></returns> 137 public T getExistsByName<T>(string Name) where T : dtModelBase 138 { 139 using (IDbConnection coon = DBHelper.CreateConnection()) 140 { 141 142 var predicate = Predicates.Field<T>(f => f.Name, Operator.Eq, Name); 143 IEnumerable<T> list = coon.GetList<T>(predicate); 144 return list.FirstOrDefault(); 145 } 146 } 147 /// <summary> 148 /// 通过标准名Ename查询是否存在 149 /// </summary> 150 /// <typeparam name="T"></typeparam> 151 /// <param name="Name"></param> 152 /// <returns></returns> 153 public T getExistsByEName<T>(string EName, string Tbname) where T : class 154 { 155 using (IDbConnection coon = DBHelper.CreateConnection()) 156 { 157 string sql = "select * from " + Tbname + " where EName =@EName"; 158 IEnumerable<T> list = coon.Query<T>(sql, new { EName }); 159 return list.FirstOrDefault(); 160 } 161 } 162 /// <summary> 163 /// 读取数据源 164 /// </summary> 165 /// <returns></returns> 166 public List<T> GetSourceDt<T>() where T : class 167 { 168 IEnumerable<T> list = null; 169 using (IDbConnection conn = DBHelper.CreateConnection()) 170 { 171 list = conn.GetList<T>().ToList(); 172 } 173 return list.ToList(); 174 } 175 #endregion 176 #endregion 177 }
1 public class SqlWhereModel 2 { 3 public string ParamName { get; set; } 4 public string ParamValue { get; set; } 5 }
结合上一篇List和DataTable相互转换的类直接 进行数据库批量插入操作调用示例
1 DataTable dt = ListTranDataTableHelper.ToDataTable(listv, DatableProperty.PropertyRenameDic); 2 CommonDal.Bulk_ImportDatable(dt, targetTableName);
分享、奉献、勤奋、好学