SqlBulkCopy 複製批量插入資料

iDotNetSpace發表於2009-10-12

using System;

using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string aa = str();

            using (SqlConnection con = new SqlConnection(aa))
            {
                con.Open();
                string sql = "select 姓名,性別,地址 from 學生1";
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    SqlDataReader dr = com.ExecuteReader();


                    using (SqlConnection connn = new SqlConnection(aa))
                    {
                        connn.Open();
                        SqlBulkCopy BulkCopy = new SqlBulkCopy(connn);
                        BulkCopy.DestinationTableName = "學生";
                        BulkCopy.WriteToServer(dr);
                    }
                }
            }
        }


        private static string str()
        {
            return "Data Source=(local); " +
           " Integrated Security=true;" +
           "Initial Catalog=haha;";

        }
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-616327/,如需轉載,請註明出處,否則將追究法律責任。

相關文章