关闭

[置顶] bcp批量复制数据到数据库中

875人阅读 评论(0) 收藏 举报

1.Bcp批拷贝效率高,几百万数据小意思。完全没有什么压力。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace SqlBulkCopyDemo
{
    public partial class Test : System.Web.UI.Page
    {
        public string SQLCONN = System.Configuration.ConfigurationManager.AppSettings["SQLCONN"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CreateXML();
                //SqlBulkCopy();
            }
        }

        #region //创建XML文件
        public void CreateXML()
        {
            using (SqlConnection conn = new SqlConnection(SQLCONN))
            {
                SqlDataAdapter da = new SqlDataAdapter("Select * from Student with(nolock)", conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                ds.WriteXml(Server.MapPath(@"XMLS\Student.xml"));
            }
        }
        #endregion

        #region //SqlBulkCopy批量导入数据
        public void SqlBulkCopy()
        {
            DataSet ds = new DataSet();
            DataTable sourceData = new DataTable();
            ds.ReadXml(Server.MapPath(@"XMLS\Student.xml"));
            sourceData = ds.Tables[0];
            using (SqlConnection conn = new SqlConnection(SQLCONN))
            {
                conn.Open();
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(SQLCONN))
                {
                    bulkCopy.ColumnMappings.Add("StudName", "StudName");
                    bulkCopy.ColumnMappings.Add("Sex", "Sex");
                    bulkCopy.ColumnMappings.Add("Age", "Age");
                    bulkCopy.ColumnMappings.Add("Birthday", "Birthday");
                    bulkCopy.ColumnMappings.Add("Tel", "Tel");
                    bulkCopy.ColumnMappings.Add("Email", "Email");
                    bulkCopy.ColumnMappings.Add("Address", "Address");
                    bulkCopy.ColumnMappings.Add("AddTime", "AddTime");
                    bulkCopy.DestinationTableName = "Student";
                    bulkCopy.WriteToServer(sourceData);
                }
            }
        }
        #endregion

        #region //SqlBulkCopy批量导入数据(事务)
        public void TransactionSqlBulkCopy()
        {
            DataSet ds = new DataSet();
            DataTable sourceData = new DataTable();
            ds.ReadXml(Server.MapPath(@"XMLS\Student.xml"));
            sourceData = ds.Tables[0];
            using (SqlConnection conn = new SqlConnection(SQLCONN))
            {
                conn.Open();
                SqlTransaction Transaction=conn.BeginTransaction();//开启事务
                using (SqlBulkCopy BulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default,Transaction))
                {
                    try
                    {
                        BulkCopy.ColumnMappings.Add("StudName", "StudName");
                        BulkCopy.ColumnMappings.Add("Sex", "Sex");
                        BulkCopy.ColumnMappings.Add("Age", "Age");
                        BulkCopy.ColumnMappings.Add("Birthday", "Birthday");
                        BulkCopy.ColumnMappings.Add("Tel", "Tel");
                        BulkCopy.ColumnMappings.Add("Email", "Email");
                        BulkCopy.ColumnMappings.Add("Address", "Address");
                        BulkCopy.ColumnMappings.Add("AddTime", "AddTime");
                        BulkCopy.DestinationTableName = "Student";
                        BulkCopy.WriteToServer(sourceData);
                        BulkCopy.ColumnMappings.Clear();
                        Transaction.Commit();//事务提交
                    }
                    catch
                    {
                        Transaction.Rollback();//事务回滚
                    }
                    finally
                    {
                        Transaction.Dispose();//事务释放
                    }
                }
            }
        }
        #endregion
    }
}

14
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:134886次
    • 积分:2229
    • 等级:
    • 排名:第16893名
    • 原创:65篇
    • 转载:9篇
    • 译文:4篇
    • 评论:23条
    最新评论