CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2009
    Posts
    144

    Populate database from dataset!

    Hello, i want to clone a database table from one database to another...

    i read successfully all contents of the original table to a dataset like this:

    Code:
    OdbcCommand cmd;
                    DataSet ds;
                    System.Data.Odbc.OdbcDataAdapter da;
                    string MyString;
    
                    using (OdbcConnection cn = new OdbcConnection())
                    {
                       
    cn.ConnectionString = ("Driver={Microsoft Access Driver (*.mdb)};DBQ="file.mdb";");
                        ds = new DataSet();
    
                        MyString = "select * from Table";
    
                        da = new System.Data.Odbc.OdbcDataAdapter(MyString, cn.ConnectionString);
                        cmd = new OdbcCommand(MyString, cn);
                        da.Fill(ds, "Table");
      
                    }
    then i want to "transfer,clone,copy" the contents of the original table to the destination, can that be done?

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Populate database from dataset!

    If you have set appropriate table adapter properly, it should be sufficient to call Clone() on the DataTable (don't forget cast the result, because Clone() returns object), open connection to destination database and call Update() on cloned table.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Jun 2009
    Posts
    144

    Re: Populate database from dataset!

    i changed my code to this:

    Code:
        OdbcCommand cmd;
                    DataSet ds;
                    System.Data.Odbc.OdbcDataAdapter da;
                    string MyString;
                    DataTable dt = new DataTable();
                    DataTable dtc = new DataTable();
    
    using (OdbcConnection cn = new OdbcConnection())
                    {
    
                        //Drop table
    
                        cn.ConnectionString = ("Driver={Microsoft Access Driver (*.mdb)};DBQ=" + desttextBox.Text + ";");
                        ds = new DataSet();
    
                        MyString = "delete from table;";
    
                        da = new System.Data.Odbc.OdbcDataAdapter(MyString, cn.ConnectionString);
                        cmd = new OdbcCommand(MyString, cn);
                        da.Fill(ds, "Table");
                      
                        //Drop table
    
                        //Read and fill
    
                        cn.ConnectionString = ("Driver={Microsoft Access Driver (*.mdb)};DBQ=" + sourcetextBox.Text + ";");
                        ds = new DataSet();
    
                        MyString = "select * from Table";
    
                        da = new System.Data.Odbc.OdbcDataAdapter(MyString, cn.ConnectionString);
                        cmd = new OdbcCommand(MyString, cn);
                        da.Fill(ds, "Table");
    
                        dt = ds.Tables["Table"];
                        dtc = dt.Copy();
                       
    
                        //fill
    
                        cn.ConnectionString = ("Driver={Microsoft Access Driver (*.mdb)};DBQ=" + desttextBox.Text + ";");
                        ds = new DataSet();
    
                        MyString = "select * from Table";
    
                        da = new System.Data.Odbc.OdbcDataAdapter(MyString, cn.ConnectionString);
                        cmd = new OdbcCommand(MyString, cn);
                        da.Fill(ds, "Table");
    
                        ds.Tables.Remove("Table");
                        ds.Tables.Add(dtc);
                        myTableAdapter.Update(myDataSet.Table);
    
       
                    }
    this doesn't work.. it only deletes all rows from destination table!

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Populate database from dataset!

    Code:
                        //fill
    
                        cn.ConnectionString = ("Driver={Microsoft Access Driver (*.mdb)};DBQ=" + desttextBox.Text + ";");
                        da = new System.Data.Odbc.OdbcDataAdapter(MyString, cn.ConnectionString);
    
                        myTableAdapter.Update(dtc);
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  5. #5
    Join Date
    Jun 2009
    Posts
    144

    Re: Populate database from dataset!

    i already tried this... i create my tableadapter by dragging datasource to my form. and if i try your update it gives me this error:


    Code:
    cannot convert from 'System.Data.DataTable' to 'ProjectName.DataSet1.myDataTable'
    i know that in order to update a database from a dataset i need tableadapter.update but i don't know if i create it correctly, dragging it from datasource seems not to work very good

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured