SqlBulkCopy 通过泛型数组批量插入
public void SqlBulkCopy<T>(string tablename, List<T> list)
{
Type recordType = typeof(T);
PropertyInfo[] patternPInfos = recordType.GetProperties();
using (SqlConnection conn2 = new SqlConnection(connString))
{
using (SqlBulkCopy bcp = new SqlBulkCopy(conn2))
{
bcp.DestinationTableName = tablename;
DataTable tempdt = new DataTable();
foreach (var propertyInfo in patternPInfos)
{
tempdt.Columns.Add(propertyInfo.Name, propertyInfo.PropertyType);
}
foreach (var entity in list)
{
DataRow dr = tempdt.NewRow();
foreach (var propertyInfo in patternPInfos)
{
dr[propertyInfo.Name] = propertyInfo.GetValue(entity, null);
}
tempdt.Rows.Add(dr);
}
conn2.Open();
bcp.WriteToServer(tempdt);
conn2.Close();
}
}
}
===============
public T Analyze<T>(string html)
{
if (string.IsNullOrEmpty(html))
throw new Exception("传入的Html为空");
//反射
//TODO:增加缓存;
Type recordType = typeof(T);
Type patternType = this.GetType();
PropertyInfo[] patternPInfos = patternType.GetProperties();
//CreateInstance 创建指定泛型类型参数所指定类型的实例。
T record = Activator.CreateInstance<T>();
foreach (PropertyInfo patternPInfo in patternPInfos)
{
object[] customInfos = patternPInfo.GetCustomAttributes(typeof(PatternAttributes), true);
if (customInfos == null
|| customInfos.Length == 0)
continue;
PatternAttributes patternAtrributes = customInfos.GetValue(0) as PatternAttributes;
//propertyInfo.GetValue(entity,null) 获取实体的值
object patternObjVal = patternType.GetProperty(patternPInfo.Name).GetValue(this, null);
if (patternObjVal == null)
continue;
RegexColumnEntity regexColumn = (RegexColumnEntity)patternObjVal;
//如果没有写规则则跳过
if (string.IsNullOrEmpty(regexColumn.Pattern))
continue;
//提取值
object objVal = Analyze(html, regexColumn, patternAtrributes);
PropertyInfo recordProperty = recordType.GetProperty(patternPInfo.Name);
if (recordProperty != null)
{
try
{
recordProperty.SetValue(record, objVal, null);
}
catch { }
}
}
return record;
}
---------------
public void CheckValuesIsChanged(BaseAnalyzePatternEntity oldT, BaseAnalyzePatternEntity newT, MediaTypeEnum mediatype)
{
Type tType = null;
switch (mediatype)
{
case MediaTypeEnum.NetData:
oldT = oldT as WebNewsAnalyzePatternEntity;
newT = newT as WebNewsAnalyzePatternEntity;
tType = typeof(WebNewsAnalyzePatternEntity);
break;
case MediaTypeEnum.TwintterData:
oldT = oldT as WeiboAnalyzePatternEntity;
newT = newT as WeiboAnalyzePatternEntity;
tType = typeof(WeiboAnalyzePatternEntity);
break;
case MediaTypeEnum.BlogData:
oldT = oldT as BlogAnalyzePatternEntity;
newT = newT as BlogAnalyzePatternEntity;
tType = typeof(BlogAnalyzePatternEntity);
break;
case MediaTypeEnum.SEBBSData:
oldT = oldT as ForumAnalyzePatternEntity;
newT = newT as ForumAnalyzePatternEntity;
tType = typeof(ForumAnalyzePatternEntity);
break;
default:
break;
}
Type regexColumnEntity = typeof(RegexColumnEntity);
//字段属性名 频道 媒体名称
PropertyInfo[] infos = tType.GetProperties();
foreach (PropertyInfo info in infos)
{
//if (oldT != null)
//{
//字段属性值
object oldValue = info.GetValue(oldT, null);
object newValue = info.GetValue(newT, null);
if (oldValue != null)
{
//获取类型里面的方法
MethodInfo ValueInfoMethod = regexColumnEntity.GetMethod("AnalyzeToString");
//有规则字段
if (info.PropertyType.Name == "RegexColumnEntity")
{
object oldregexColumninfoValue = ValueInfoMethod.Invoke(oldValue, null);
object newregexColumninfoValue = ValueInfoMethod.Invoke(newValue, null);
if (!oldregexColumninfoValue.Equals(newregexColumninfoValue))
{
MediaRegexEntity entity = new MediaRegexEntity();
List<string> patterlist = newregexColumninfoValue.ToString().Split(new Char[] { '$', '$', '$', '$', '$' }).ToList();
entity.Pattern = patterlist[0];
entity.Options = patterlist[patterlist.Count - 1];
entity.Name = info.Name;
entity.MediaRegexStatus = newT.Status.ToString() == "1" ? true : false;//MediaRegexStatus
entity.OperatUser = newT.ModifyUser;
entity.SystemType = "AnalyzeSystem";
entity.MediaUrl = newT.WebSite;
entity.Changed = true;
entity.OperatTime = DateTime.Now;
entity.MediaType = ((MediaTypeEnum)mediatype).ToString();
entity.MediaName = newT.Name;
new Dal.MediaRegexDal().Add(entity);
}
//}
}
}
}
}
SqlBulkCopy 通过泛型数组批量插入的更多相关文章
- SqlBulkCopy与触发器,批量插入表(存在则更新,不存在则插入)
临时表:Test /****** 对象: Table [dbo].[Test] 脚本日期: 05/10/2013 11:42:07 ******/ SET ANSI_NULLS ON GO SET Q ...
- C# 批量插入表SQLSERVER SqlBulkCopy往数据库中批量插入数据
#region 帮助实例:SQL 批量插入数据 多种方法 /// <summary> /// SqlBulkCopy往数据库中批量插入数据 /// </summary> /// ...
- .net使用SqlBulkCopy类操作DataTable批量插入数据库数据,然后分页查询坑
在使用SqlBulkCopy类操作DataTable批量插入数据,这种操作插入数据的效率很高,就会导致每一条数据在保存的时间基本一样,在我们分页查询添加的数据是,使用数据的添加时间来排序就会出现每页的 ...
- php 将二维数组批量插入到数据库中
$arr = array( array(,'name'=>'ceshi4'), array(,'name'=>'ceshi5'), array(,'name'=>'ceshi6'), ...
- c# sqlbulkcopy批量插入数据
dt信息中包含数据和表名 public static void SqlBulkInsert(DataTable dt, string connStr) { try { using (var conn ...
- c# 数据库批量插入数据SqlBulkCopy 示例
/// <summary> /// 批量插入数据 /// </summary> public static void SqlBulkInsert(DataTable dt, s ...
- net core天马行空系列-各大数据库快速批量插入数据方法汇总
1.前言 hi,大家好,我是三合.我是怎么想起写一篇关于数据库快速批量插入的博客的呢?事情起源于我们工作中的一个需求,简单来说,就是有一个定时任务,从数据库里获取大量数据,在应用层面经过处理后再把结果 ...
- .Net批量插入数据
1. 一般我们普通数据插入是这样的: 现在我们写一个控制台程序用常规办法添加10000条数据. //以下是批量插入数据的办法 //连接字符串 string str = "Server=.;D ...
- EF结合SqlBulkCopy实现高效的批量数据插入 |EF插件EntityFramework.Extended实现批量更新和删除
原文链接:http://blog.csdn.net/fanbin168/article/details/51485969 批量插入 (17597条数据批量插入耗时1.7秒) using Sys ...
随机推荐
- Android实现小圆点显示未读功能
代码地址如下:http://www.demodashi.com/demo/13541.html 前言 以前我们实现这个功能都是用 BadgeView.java,大体就是将这个java类复制到自己的项目 ...
- struts2中 jsp:forward 失败原因及解决办法
问题:在Struts2中<jsp:forward page="xxx.action"></jsp:forward>失效了,不但调转不过去还报404错误.不知 ...
- 详解Android中那些酷炫返回方式的实现
Android手机都会有返回键,不管是实体键,还是虚拟键.Android用户主要也都是通过这个返回键操控页面返回方式的,不比IOS逼格甚高的只保留一个操作键.这种方式是最普遍的返回方式,还有一种也是比 ...
- unity3d动态操作组件
利用范型,动态操作组件(添加或删除) e.AddComponent<CubeTranslate> ();//动态添加组件 Destroy (e.GetComponent<CubeTr ...
- XML - 十分钟了解XML结构以及DOM和SAX解析方式
引言 NOKIA 有句著名的广告语:"科技以人为本".不论什么技术都是为了满足人的生产生活须要而产生的.详细到小小的一个手机.里面蕴含的技术也是浩如烟海.是几千年来人类科技的结晶, ...
- js返回页面顶部
第一次写博客,不太专业,废话不多说,直接上自己早上做的东东.有不足之处,希望指点. css: body{counter-reset: p;} p{width: 100px;margin: 20px 0 ...
- DirectShow中写push模式的source filter流程 + 源码(内附具体凝视)
尽管网上已有非常多关于DirectShow写source filter的资料.只是非常多刚開始学的朋友总说讲的不是非常清楚(可能当中作者省略了很多他觉得简 单的过程).读者总希望看到象第一步怎么做,第 ...
- thread_线程间协作:wait、notify、notifyAll和Condition
经典模式:生产者-消费者模型:当队列满时,生产者需要等待队列有空间才能继续往里面放入商品,而在等待的期间内,生产者必须释放对临界资源(即队列)的占用权.因为生产者如果不释放对临界资源的占用权,那么消费 ...
- C# 执行多条SQL更新语句,实现数据库事务
class Program { class Result<T> { public T data; public string Message; public bool Success; p ...
- nyoj 740 “炫舞家“ST
“炫舞家“ST 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 ST是一个酷爱炫舞的玩家.TA很喜欢玩QQ炫舞,因此TA也爱屋及乌的喜欢玩跳舞机(Dance Dance ...