使用SqlBulkCopy导入数据,并且生成insert数据

 protected void Button1_Click(object sender, EventArgs e)

    {

        string execelConnectionStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/Xmas Fusion - 10000 Coupons.xls;Extended Properties=""Excel 8.0;HDR=YES;""";

        using (OleDbConnection conn = new OleDbConnection(execelConnectionStr))

        {

            OleDbCommand cmd = new OleDbCommand("select * FROM [rental Promocodes1$]", conn);

            conn.Open();

            using (IDataReader dr = cmd.ExecuteReader())

            {

                string sqlConnectionString = @"Data Source=192.168.1.6;Initial Catalog=VP_Prod_080627;uid=VP;pwd=clochase";

                // Bulk Copy to SQL Server 

                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))

                {

                    bulkCopy.DestinationTableName = "TempTest";

                    bulkCopy.WriteToServer(dr);

                }

            }

        }

        Response.Write("OK");

    }

    protected void Button2_Click(object sender, EventArgs e)

    {

        string sqlConnectionString = @"Data Source=192.168.1.6;Initial Catalog=VP_Prod_080627;uid=VP;pwd=clochase";

        FileStream file = new FileStream(@"D:/Insert.txt",FileMode.Create);

        StreamWriter w = new StreamWriter(file);

        using (SqlConnection con = new SqlConnection(sqlConnectionString))

        {

            SqlCommand cmd = new SqlCommand("select * from TempTest", con);

            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();

            da.Fill(dt);

            long count = 170001;

            for (int i = 0; i < dt.Rows.Count; i++)

            { 

                string insertString = string.Format("insert tblPromo values({0},'{1}','{2}')",count, dt.Rows[i]["PromoCode"].ToString(),dt.Rows[i]["PromoVersion"].ToString());

                w.WriteLine(insertString);

                count++;

            }

            w.Close();

            file.Close();

        }

        Response.Write("sheng cheng ok ");





    }

你可能感兴趣的:(使用SqlBulkCopy导入数据,并且生成insert数据)