当前位置:  编程技术>.net/c#/asp.net

C#中bcp批量复制数据到数据库中的实例代码

    来源: 互联网  发布时间:2014-08-30

    本文导语:  在C#编程中,用Bcp批量复制数据,哪怕几百万的数据量,也可以轻松应对哦。 完整代码:     代码示例: using System;      using System.Collections.Generic;      using System.Linq;      using System.Web;      using System.Web.UI;    ...

在C#编程中,用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(@"XMLSStudent.xml")); 
                } 
            } 
            #endregion www.
    
            #region //SqlBulkCopy批量导入数据 
            public void SqlBulkCopy() 
            { 
                DataSet ds = new DataSet(); 
                DataTable sourceData = new DataTable(); 
                ds.ReadXml(Server.MapPath(@"XMLSStudent.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(@"XMLSStudent.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 
        } 
    }

    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 执行bcp命令
  • SQL Server利用bcp命令把SQL语句结果生成文本文件


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3