108 lines
4.0 KiB
C#
108 lines
4.0 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Data.SqlClient;
|
||
using System.Linq;
|
||
|
||
namespace BLL
|
||
{
|
||
public static class SqlBulkHelper
|
||
{
|
||
///<summary>
|
||
///连接数据库
|
||
///</summary>
|
||
///<returns>返回SqlConnection对象</returns>
|
||
public static SqlConnection GetCon()
|
||
{
|
||
return new SqlConnection(Funs.ConnString);
|
||
}
|
||
|
||
#region SqlBulkCopy批量插入数据
|
||
/// <summary>
|
||
/// 执行SqlBulkCopy批量插入,执行事务。
|
||
/// </summary>
|
||
/// <param name="connectionString">数据连接</param>
|
||
/// <param name="TableName">表名</param>
|
||
/// <param name="dt">要插入的数据</param>
|
||
/// <returns></returns>
|
||
public static int SqlBulkCopy(string connectionString, string TableName, DataTable dt)
|
||
{
|
||
using (SqlConnection conn = new SqlConnection(connectionString))
|
||
{
|
||
using (SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction))
|
||
{
|
||
try
|
||
{
|
||
sqlbulkcopy.DestinationTableName = TableName;
|
||
for (int i = 0; i < dt.Columns.Count; i++)
|
||
{
|
||
sqlbulkcopy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName);
|
||
}
|
||
sqlbulkcopy.WriteToServer(dt);
|
||
return 1;
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
return 0;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 批量插入
|
||
/// </summary>
|
||
/// <typeparam name="T">泛型集合的类型</typeparam>
|
||
/// <param name="conn">连接对象</param>
|
||
/// <param name="tableName">将泛型集合插入到本地数据库表的表名</param>
|
||
/// <param name="list">要插入大泛型集合</param>
|
||
public static int BulkInsert<T>(SqlConnection conn, string tableName, IList<T> list)
|
||
{
|
||
int result = 0;
|
||
|
||
try
|
||
{
|
||
if (conn.State != ConnectionState.Open)
|
||
{
|
||
conn.Open(); //打开Connection连接
|
||
}
|
||
using (var bulkCopy = new SqlBulkCopy(conn))
|
||
{
|
||
bulkCopy.BatchSize = result = list.Count;
|
||
bulkCopy.DestinationTableName = tableName;
|
||
bulkCopy.BulkCopyTimeout = 600;
|
||
var table = new DataTable();
|
||
var props = TypeDescriptor.GetProperties(typeof(T)).Cast<PropertyDescriptor>().Where(propertyInfo => propertyInfo.PropertyType.Namespace.Equals("System")).ToArray();
|
||
foreach (var propertyInfo in props)
|
||
{
|
||
bulkCopy.ColumnMappings.Add(propertyInfo.Name, propertyInfo.Name);
|
||
table.Columns.Add(propertyInfo.Name, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType);
|
||
}
|
||
var values = new object[props.Length];
|
||
foreach (var item in list)
|
||
{
|
||
for (var i = 0; i < values.Length; i++)
|
||
{
|
||
values[i] = props[i].GetValue(item);
|
||
}
|
||
table.Rows.Add(values);
|
||
}
|
||
bulkCopy.WriteToServer(table);
|
||
}
|
||
if (conn.State != ConnectionState.Closed)
|
||
{
|
||
conn.Close(); //关闭Connection连接
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
result = 0;
|
||
}
|
||
|
||
return result;
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
} |