Dapper 批量添加
来源:互联网 发布:aⅴ淘宝新址 编辑:程序博客网 时间:2024/06/03 21:07
1、方法一
Controller控制器,代码如下
RepositoryBase基类,代码如下
实体类,代码如下
2、方法二
实体类,代码如下
Controller控制器,代码如下
public ActionResult SaveAction(){ List<Languages> list = new List<Languages>(); for (int i = 0; i <= 10; i++) { var item = new Languages { Title = "Title" + i.ToString(), EnglishTitle = "EnglishTitle", CreateBy = 0, CreatedDate = DateTime.Now, ModifiedBy = 0, UpdatedDate = DateTime.Now, State = "A" }; list.Add(item); } _languageSvc.AddBatch(list); return View();}
RepositoryBase基类,代码如下
public virtual void AddBatch(List<T> listModel){ try { if (listModel != null && listModel.Count > 0) { T model = listModel.FirstOrDefault(); var ps = model.GetType().GetProperties(); List<string> @colms = new List<string>(); List<string> @params = new List<string>(); foreach (var p in ps) { if (!p.CustomAttributes.Any(x => x.AttributeType == typeof(PrimaryKeyAttribute)) && !p.CustomAttributes.Any(x => x.AttributeType == typeof(DBIgnoreAttribute))) { @colms.Add(string.Format("[{0}]", p.Name)); @params.Add(string.Format("@{0}", p.Name)); } } var sql = string.Format("INSERT INTO [{0}] ({1}) VALUES({2})", typeof(T).Name, string.Join(", ", @colms), string.Join(", ", @params)); using (var _conn = _dbFactory.OpenDbConnection()) { _conn.Execute(sql, listModel, null, null, null); } } } catch (Exception ex) { throw; }}
实体类,代码如下
[Serializable]public class Languages : IEntityBase<Languages>{ #region table [PrimaryKey] public long ID { get; set; } public string Title { get; set; } public string EnglishTitle { get; set; } public long CreateBy { get; set; } public long ModifiedBy { get; set; } public DateTime CreatedDate { get; set; } public DateTime UpdatedDate { get; set; } public string State { get; set; } #endregion #region DBIgnore [DBIgnore] public bool AppStatus { get; set; } #endregion}
2、方法二
public void InsertBatch(){ try { string sqlStr = "INSERT INTO ED_Data(DataKey,FieldName,Value) VALUES (@DataKey,@FieldName,@Value)"; using (IDbConnection conn = OpenConnection()) { conn.Execute(sqlStr, new[] { new { DataKey = "a", FieldName = "name", Value = "000001" }, new { DataKey = "b", FieldName = "age", Value = "000002" }, new { DataKey = "c", FieldName = "phone", Value = "000003" }, }, null, null, null); } } catch (Exception) { throw; }}
实体类,代码如下
public class ED_Data{ public string TableName { get; set; } public string DataKey { get; set; } public string FieldName { get; set; } public string Value { get; set; }}
0 0
- Dapper 批量添加
- dapper批量插入
- Dapper
- Dapper
- Dapper
- 批量添加
- 批量添加
- 批量添加
- PHP批量操作-批量查询,批量添加
- Mybatis批量查询,批量添加,批量删除
- 批量添加类例子
- 批量添加远程桌面.vbs
- 关于批量添加问题
- 批量添加记录方法
- 批量添加用户
- 批量添加产品js
- JDBC--批量添加数据
- 批量添加域用户
- iOS项目重新名
- [leetcode]160. Intersection of Two Linked Lists
- Android PopupWindow 相关设置
- Unity工程导出iOS包,使用推送,deviceToken获取不到的解决办法
- angular的几种元素隐藏的方式
- Dapper 批量添加
- 在windows上python3.4安装paramiko库
- CentOS7基础配置
- H5选择器
- 【程序员面试金典】链表分割
- Lua 调试(Debug)
- AngularJS操作DOM——angular.element
- javax.el.PropertyNotFoundException: Property 'title' not found on type java.lang.String
- throw error exception 脑补