I have two datasets (ds1 and ds2). Table1 belongs to ds1. I'd like to copy it (and rename it) and add it to ds2. When I run my program, there is no errors, but there is no dt2 in database2.mdb.
DataTable dt1 =ds1.Table1;
DataTable dt2 = new DataTable();
dt2 = dt1.Copy();
ds2.Tables.Add(dt2);
ds2.AcceptChanges();
Loading
Kirtan PatelPosted Aug 14, 2009, 11:49 AM
it Will Still Work Beacuse when we call ds.Tables.Add() method it will add new table so it is not metter that out destination dataset is empty or not it will add new table
with
DataSet ds2 = new DataSet();
ds2.Tables.Add(ds.Tables[0].Copy());
// Just Add One Data Grid View to test that data have been Copied or NotdataGridView2.DataSource = ds2.Tables[0];
though if you still having problem dot hesitate to ask or mail me the Full code(with database ) in zip you are using at [email protected]
Happy coding :)
Miodrag GrahovacPosted Aug 15, 2009, 2:08 PM
The whole idea of project is that user chooses two datatables from busic.mdb (selected from listBox1, and copied to listBox2), and I have to make mew database which contains choosen tables named Table1, and Table2. It sounds not to complicated, but I am new in c#, and just have stacked.
Master BillaPosted Aug 15, 2009, 1:57 PM
Could send you me your code here , i will work for and send you
thank you
Miodrag GrahovacPosted Aug 15, 2009, 10:48 AM
Should I kill myself?
OleDbCommand olecom = new OleDbCommand("create table TestTable([Id] Number,[Username] Text,[Password] Text)", connection);
This works like a charm, but instead of creating new Test Table, I need my copied table. I have tried SELECT INTO statement, but allways get errors.
Kirtan PatelPosted Aug 14, 2009, 11:29 PM
//Copy Dataset
DataSet ds2 = new DataSet();
ds2.Tables.Add(ds.Tables[0].Copy());
//Create new Database File
//tip : Before Creating New File First Check if Database Exist by System.IO.File.Exsist() so there //will be no error
CatalogClass cat = new CatalogClass();
cat.Create(@"Provider=Microsoft.Jet.Oledb.4.0;Data Source=D:\NewDatabase.mdb");
//Create table in New Created Database
OleDbConnection connection =new OleDbConnection(@"Provider=Microsoft.Jet.Oledb.4.0;Data Source=D:\NewDatabase.mdb");
connection.Open();
OleDbCommand olecom = new OleDbCommand("create table TestTable([Id] Number,[Username] Text,[Password] Text)", connection);
olecom.ExecuteNonQuery();
// Iterate through each row and Write it in table
foreach (DataRow r in ds2.Tables[0].Rows)
{
int id = Convert.ToInt32(r["id"]);
string uname = r["Username"].ToString();
string pass = r["Password"].ToString();
olecom = new OleDbCommand("insert into TestTable([Id],[Username],[Password]) values(@Id,@Username,@Password)", connection);
olecom.Parameters.AddWithValue("@Id", id);
olecom.Parameters.AddWithValue("@Username", uname);
olecom.Parameters.AddWithValue("@Password", pass);
olecom.ExecuteNonQuery();
}
//Close the Connection
connection.Close();
Master BillaPosted Aug 14, 2009, 11:21 PM
You need make your code one this way
DataSet ds1 ; // your source dataset
DataSet ds2; // this is your destination
ds2.Merge(ds1.Tables[0]); //This is easy and cool
thank you
Miodrag GrahovacPosted Aug 14, 2009, 3:13 PM
Kirtan PatelPosted Aug 14, 2009, 2:57 PM
- Open a new Visual C# .NET console application.
- In Solution Explorer, right-click the
References node and select Add
Reference.
- On the COM tab, select Microsoft
ADO Ext. 2.7 for DDL and Security, click Select to
add it to the Selected Components, and then click
OK.
Creating Database File is Easy but after that addind Table is some long procedure then SQL ServerHere is Code How you wil Create Access Database File Programatically
Kirtan PatelPosted Aug 14, 2009, 2:33 PM
Creating new Data Base file in Access is some cumbersome process while its not case with sql Server
in Sql Server we can create database by just passing Query and Execute it by Giving Proper Credentials :)
though friend i will let you know when i found somthing interesting short coding for that task also :)
Miodrag GrahovacPosted Aug 14, 2009, 2:30 PM
Kirtan PatelPosted Aug 14, 2009, 1:55 PM
Take a look at here for creating Access database .
http://blog.jrpsoftware.com/PermaLink,guid,edf0af2e-838a-4781-9543-13b22dee0ae9.aspx
Happy Coding :)
Miodrag GrahovacPosted Aug 14, 2009, 12:33 PM
Could you, please, help me to solve the whole problem? How can I make database2.mdb from that new dataset (ds2)?
I need something like: [code]con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=D:/database2.mdb";[/code]
Miodrag GrahovacPosted Aug 14, 2009, 11:43 AM
ds2.Tables.Add(ds1.Tables[0].Copy());
but, still nothing is copied to my destination database.
Miodrag GrahovacPosted Aug 14, 2009, 11:37 AM
Master BillaPosted Aug 14, 2009, 10:57 AM
I think , you can do in easy way
DataSet ds1;
DataSet ds2;
ds2.Tables.Add(ds1.Tables[0]); // this is add first table to ds2 table collection.
Kirtan PatelPosted Aug 14, 2009, 9:16 AM