-
December 7th, 2009, 09:41 PM
#1
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?
-
December 8th, 2009, 03:38 AM
#2
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.
-
December 8th, 2009, 08:48 AM
#3
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!
-
December 8th, 2009, 09:34 AM
#4
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.
-
December 8th, 2009, 10:17 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|