Loading

SqlBulkCopy 数据批量操作使用的类

private void SqlBulkCopyByDataTable(string connectionString,string TableName,DataTable dt)
        {
            using (SqlConnection sqlconn = 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);
                    }
                    catch(System.Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }

 

posted @ 2016-08-01 15:02  3WLineCode  阅读(388)  评论(0编辑  收藏  举报