반응형

SqlServer2005 
SqlBulkCopy() 사용하기 
----------------------- 
pubic void BulkInsert() 
{ 
  SqlBulkCopy bcp = new SqlBulkCopy("Data Source=localhost...ConnectionString...."); 
  bcp.DestinationTableName = "테이블명"; //sqlserver2005에 미리 테이블이 있어야 함. 
  bcp.BulkCopyTimeout = 600; //무한정 입력시도 방지 
  bcp.WriteToServer(ds.Tables[0]); 
} 
--------------------------------------------------- 
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Data; 
using System.Data.SqlClient; 

namespace SqlBulkCopyDemo 
{ 
   class Program 
   { 
      private static string connectionString = "Server=(local);Database=Test;Integrated Security=SSPI"; 

      static void Main(string[] args) 
      { 
         using (SqlConnection firstConnection = new SqlConnection(connectionString)) 
         { 
            SqlCommand cmdAnimals = firstConnection.CreateCommand(); 
            cmdAnimals.CommandText = "Select * from Animals"; 
            firstConnection.Open(); 
            SqlDataReader dr = cmdAnimals.ExecuteReader(); 

            using (SqlConnection secondConnection = new SqlConnection(connectionString)) 
            { 
               SqlBulkCopy bc = new SqlBulkCopy(secondConnection); 
               bc.DestinationTableName = "AnimalsCopy"; 
               bc.WriteToServer(dr); 
               bc.Close(); 
               dr.Close(); 
            } 
         } 
      } 
   } 
} 
-------------------------------------------------------------- 
using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Data.SqlClient; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

        string connectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString; 
        SqlConnection myConnection = new SqlConnection(connectionString); 
        SqlCommand myCommand = new SqlCommand("SELECT * FROM Person", myConnection); 
        myConnection.Open(); 
        SqlDataReader dr = myCommand.ExecuteReader(); 

        SqlConnection myNewConnection = new SqlConnection(connectionString); 
        myNewConnection.Open(); 
        SqlBulkCopy bulk = new SqlBulkCopy(myNewConnection); 
        bulk.DestinationTableName = "[Person2]"; 

        bulk.ColumnMappings.Add("Name", "LastName"); 
        bulk.ColumnMappings.Add("Email", "Email"); 
        bulk.ColumnMappings.Add("Picture", "Picture"); 


       try 
        { 
            bulk.WriteToServer(dr); 
        } 
        catch (Exception ex) 
        { 
            Response.Write(ex.Message); 
        } 
        finally 
        { 
            myNewConnection.Close(); 
            dr.Close(); 
            myConnection.Close(); 
            bulk.Close(); 
        } 

    } 

}

 

 

 

  [닷넷 솔루션 ASP.NET 전문교육기관 ]

반응형

+ Recent posts