关闭

快速、大量、简单的复制两个数据库之间的数据。

893人阅读 评论(0) 收藏 举报

 protected void Button1_Click(object sender, EventArgs e)
    {
        string sString;
        string nString;

        SqlConnection sConn = new SqlConnection();
        SqlConnection nConn = new SqlConnection();

        SqlCommand sCom = new SqlCommand();
        SqlDataReader sRead;

        sString = ConfigurationManager.ConnectionStrings["sDB"].ConnectionString;
        nString = ConfigurationManager.ConnectionStrings["nDB"].ConnectionString;

        sConn.ConnectionString = sString;
        sCom.Connection = sConn;
        sCom.CommandText = "Select top 500 * from " + TextBox1.Text.Trim();
        sCom.CommandType = CommandType.Text;
        sCom.Connection.Open();

        SqlBulkCopy nBulk;

        nBulk = new SqlBulkCopy(nString, SqlBulkCopyOptions.UseInternalTransaction);
        nBulk.DestinationTableName = TextBox1.Text.Trim();


        //nBulk.ColumnMappings.Add("x", "q");
        //nBulk.ColumnMappings.Add("a", "m");
        //nBulk.ColumnMappings.Add("b", "x");
        //nBulk.ColumnMappings.Add("c", "y");
        //nBulk.ColumnMappings.Add("d", "z");

        nBulk.BulkCopyTimeout = 500000000;

        nBulk.SqlRowsCopied +=new SqlRowsCopiedEventHandler(OnRowsCopied);

        nBulk.NotifyAfter = 1000;

        sRead = sCom.ExecuteReader();

        try
        {
            nBulk.WriteToServer(sRead);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            throw;
        }
        finally
        {
            sRead.Close();
        }

    } 

 

 

 TextBox1.Text.Trim()   用来输入表名。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:458937次
    • 积分:5823
    • 等级:
    • 排名:第4381名
    • 原创:90篇
    • 转载:132篇
    • 译文:2篇
    • 评论:80条
    文章分类