Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello ALL,

I have table1 in database1.mdb with some records, and table2 in database2.mdb with some records. The data structure of table1 and table2 is same. Now i need to insert records from table2 in table1. How can I do this?

I have been trying the ms-access import options but it creates a different table in database1.mdb with records of table2. But I want all the records from table2 in table1.
Posted

INSERT into table1
SELECT * FROM table2

Hope this help
 
Share this answer
 
v2
Comments
Prerak Patel 1-Jul-11 3:09am    
OP uses different access files.
Uday P.Singh 1-Jul-11 3:13am    
the table1 and table2 does not exists in same file.(but the name and structure of 2 tables are same).
primary key may create problem
anyway u can get the data from 2nd table 1 by one and insert it to the other table.

use dataadapter concept.

i provide u the sample implement it in yours.
Do not copy paste but write your own as u will get suggestion from VS Intellisense.

understand it and then implement it.
// query select * from table 1
// query2 select * from table2

<code>
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
SqlDataAdapter da;
SqlCommand cmd;


Datarow dr;
//for 1st table
da = new SqlDataAdapter (query,con);
DataSet ds1 = new DataSet();
da.Fill(ds1);

//for 2nd table
da = new SqlDataAdapter (query2,con);
da.Fill(ds1);




foreach (DataRow dr2 in ds1.Tables[1].Rows)
{
dr=ds1.tables[0].newrow();
dr[0]=dr2[0];
dr[1]=dr2[1];
//similarly for other columns

ds1.tables[0].rows.add(dr);
}


//now update it to database
cmd=new sqlcommandbuilder(da);
da.update(ds1);






dr=ds.tables[0].newrow();

</code>
 
Share this answer
 
Comments
Uday P.Singh 1-Jul-11 5:34am    
sorry, but i don't have to do it by code. And I don't have any primary key in these tables

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900