Search This Blog

Friday 26 October 2012

SQL Bulk Copy with C#.Net


 string strConnection = ConfigurationManager.ConnectionStrings["Connection"].ToString();
 SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from TableName", con);
        cmd.Connection = con;
        SqlDataReader dr = cmd.ExecuteReader();

        SqlConnection con2 = new SqlConnection(strConnection);
        con2.Open();
        SqlBulkCopy copy = new SqlBulkCopy(con2);
     
        copy.DestinationTableName = "DestinationTable";
        copy.WriteToServer(dr);
        dr.Close();
        con2.Close();
        con.Close();

No comments:

Post a Comment