Copy from one database table to another C#

前端 未结 5 709
情话喂你
情话喂你 2021-01-02 10:03

Using C# (vs2005) I need to copy a table from one database to another. Both database engines are SQL Server 2005. For the remote database, the source, I only have execute ac

5条回答
  •  悲&欢浪女
    2021-01-02 10:40

    Perhaps SqlBulkCopy; use SqlCommand.ExecuteReader to get the reader that you use in the call to SqlBulkCopy.WriteToServer. This is the same as bulk-insert, so very quick. It should look something like (untested);

    using (SqlConnection connSource = new SqlConnection(csSource))
    using (SqlCommand cmd = connSource.CreateCommand())
    using (SqlBulkCopy bcp = new SqlBulkCopy(csDest))
    {
        bcp.DestinationTableName = "SomeTable";
        cmd.CommandText = "myproc";
        cmd.CommandType = CommandType.StoredProcedure;
        connSource.Open();
        using(SqlDataReader reader = cmd.ExecuteReader())
        {
            bcp.WriteToServer(reader);
        }
    }
    

提交回复
热议问题