SqlBulkCopy 复制批量插入数据

 

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;";

        }
    }
}

 

posted @ 2009-09-15 13:03  孤冰  阅读(237)  评论(0编辑  收藏  举报