批量插入table
2011-04-07 16:00 yuejianjun 阅读(208) 评论(0) 编辑 收藏 举报 /// <summary>
/// 批量插入table
/// </summary>
/// <param name="TableName"></param>
/// <param name="dt"></param>
/// <returns></returns>
public static int ExecuteSqlTranWithSqlBulkCopy( 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;
}
}
}
}