none
Bulk Copy RRS feed

  • Question

  • Hello guys,

    i have the following issue, i'm trying to "Bulk Copy" data from one table to another,

    i know the alternatives of using "insert into x select * from y" but regardless of that, I'm trying to know how to make the below code work.

    after i run it, i'm getting the following error:

    Update unable to find TableMapping['WORLD_CUR2'] or DataTable 'WORLD_CUR2'.

     

    Code Snippet

    SqlConnection GLB_DB = new SqlConnection();

    GLB_DB.ConnectionString = "server=SQLSERV;database=MYDB;uid=USER;pwd=PWD;";

    GLB_DB.Open();

    SqlDataAdapter DA_FROM = new SqlDataAdapter("select * from WORLD_CUR",GLB_DB);

    DataSet DS_FROM = new DataSet("DS_FROM");

    DA_FROM.Fill(DS_FROM,"WORLD_CUR");

    SqlDataAdapter DA_TO = new SqlDataAdapter("select * from WORLD_CUR2 WHERE 1=0",GLB_DB);

    DataSet DS_TO = new DataSet("DS_TO");

    DA_TO.Fill(DS_TO,"WORLD_CUR2");

    DS_TO = DS_FROM.Copy();

    DA_TO.Update(DS_TO,"WORLD_CUR2");

    GLB_DB.Close();

     

     

    Using SQL Server 2000, and VS.NET2003.

    Thanks in advance

    Thursday, March 6, 2008 2:14 PM

Answers

All replies

  • Look at this example of Bulk copy, maybe it will help you:

    using System.Data.SqlClient;

    class Program
    {
    static void Main()
    {
    string connectionString = GetConnectionString();
    // Open a sourceConnection to the AdventureWorks database.
    using (SqlConnection sourceConnection =
    new SqlConnection(connectionString))
    {
    sourceConnection.Open();

    // Perform an initial count on the destination table.
    SqlCommand commandRowCount = new SqlCommand(
    "SELECT COUNT(*) FROM " +
    "dbo.BulkCopyDemoMatchingColumns;",
    sourceConnection);
    long countStart = System.Convert.ToInt32(
    commandRowCount.ExecuteScalar());
    Console.WriteLine("Starting row count = {0}", countStart);

    // Get data from the source table as a SqlDataReader.
    SqlCommand commandSourceData = new SqlCommand(
    "SELECT ProductID, Name, " +
    "ProductNumber " +
    "FROM Production.Product;", sourceConnection);
    SqlDataReader reader =
    commandSourceData.ExecuteReader();

    // Open the destination connection. In the real world you would
    // not use SqlBulkCopy to move data from one table to the other
    // in the same database. This is for demonstration purposes only.
    using (SqlConnection destinationConnection =
    new SqlConnection(connectionString))
    {
    destinationConnection.Open();

    // Set up the bulk copy object.
    // Note that the column positions in the source
    // data reader match the column positions in
    // the destination table so there is no need to
    // map columns.
    using (SqlBulkCopy bulkCopy =
    new SqlBulkCopy(destinationConnection))
    {
    bulkCopy.DestinationTableName =
    "dbo.BulkCopyDemoMatchingColumns";

    try
    {
    // Write from the source to the destination.
    bulkCopy.WriteToServer(reader);
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    }
    finally
    {
    // Close the SqlDataReader. The SqlBulkCopy
    // object is automatically closed at the end
    // of the using block.
    reader.Close();
    }
    }

    // Perform a final count on the destination
    // table to see how many rows were added.
    long countEnd = System.Convert.ToInt32(
    commandRowCount.ExecuteScalar());
    Console.WriteLine("Ending row count = {0}", countEnd);
    Console.WriteLine("{0} rows were added.", countEnd - countStart);
    Console.WriteLine("Press Enter to finish.");
    Console.ReadLine();
    }
    }
    }

    private static string GetConnectionString()
    // To avoid storing the sourceConnection string in your code,
    // you can retrieve it from a configuration file.
    {
    return "Data Source=(local); " +
    " Integrated Security=true;" +
    "Initial Catalog=AdventureWorks;";
    }
    }
    It is from location

    http://msdn2.microsoft.com/en-us/library/1y8tb169(VS.80).aspx

    greet
    Thursday, March 6, 2008 3:12 PM
  • i changed my code to the following, the error is gone, but WORLD_CUR2 isn't being filled with WORLD_CUR's data

    it tried it with the commented lines too, with the same result.

     

    Code Snippet

    SqlDataAdapter DA_FROM = new SqlDataAdapter("select * from WORLD_CUR",GLB_DB);

    DataSet DS_FROM = new DataSet();

    DA_FROM.Fill(DS_FROM,"T");

    SqlDataAdapter DA_TO = new SqlDataAdapter("select * from WORLD_CUR2 WHERE 1=0",GLB_DB);

    DataSet DS_TO = new DataSet();

    DA_TO.Fill(DS_TO,"T");

    SqlCommandBuilder CB_DS_TO;

    CB_DS_TO = new SqlCommandBuilder(DA_TO);

    DS_TO = DS_FROM.Copy();

    // string x= DS_TO.Tables[0].Rows[0][0].ToString();

    // DS_TO.Tables[0].Rows[0][0] = null;

    //

    // DS_TO.GetChanges();

    // DS_TO.Tables[0].Rows[0][0] = x;

    DA_TO.Update(DS_TO,"T");

     

     

     

    as for using SqlBulkCopy, as previously noted i'm using VS2003, which doesn't have ADO.NET 2.0.
    Friday, March 7, 2008 7:32 AM
  • You may want to try a slightly different approach because I don't think the method you are using is going to work for you:

     

    http://www.knowdotnet.com/articles/datasetmerge.html

     

     

     

    Friday, March 7, 2008 1:31 PM