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);
}
//}
}
}
}
}

你可能感兴趣的:(copy)