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);
}html
foreach (var entity in list)
{
DataRow dr = tempdt.NewRow();緩存
foreach (var propertyInfo in patternPInfos)
{
dr[propertyInfo.Name] = propertyInfo.GetValue(entity, null);this
}
tempdt.Rows.Add(dr);
}
conn2.Open();
bcp.WriteToServer(tempdt);
conn2.Close();
}
}
}spa
===============htm
public T Analyze<T>(string html)
{string
if (string.IsNullOrEmpty(html))
throw new Exception("傳入的Html爲空");it
//反射
//TODO:增長緩存;
Type recordType = typeof(T);io
Type patternType = this.GetType();
PropertyInfo[] patternPInfos = patternType.GetProperties();table
//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); } //} } } } }