合并具有相同结构的2个表数据 [英] Merge 2 table data with same structure

查看:97
本文介绍了合并具有相同结构的2个表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在database1.mdb中有一些记录的表1,在database2.mdb中有一些记录的表2. table1和table2的数据结构相同.现在,我需要在table1中插入来自table2的记录.我怎样才能做到这一点?

我一直在尝试ms-access导入选项,但是它在database1.mdb中使用table2的记录创建了一个不同的表.但是我想要table1中table2的所有记录.

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.

推荐答案

INSERT into table1
SELECT * FROM table2


希望此帮助


Hope this help


主键可能会造成问题
无论如何,您都可以从第二张表1中获取数据,然后将其插入到另一张表中.

使用dataadapter概念.

我为您提供了在您自己中实现的示例.
不要复制粘贴,而是自己写,因为您会得到VS Intellisense的建议.

了解它,然后实施它.
//查询表1中的select *
//query2 select * from table2

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


数据行博士;
//用于第一个表
da =新的SqlDataAdapter(query,con);
DataSet ds1 =新的DataSet();
da.Fill(ds1);

//用于第二张桌子
da =新的SqlDataAdapter(query2,con);
da.Fill(ds1);




foreach(ds1.Tables [1] .rows中的DataRow dr2)
{
dr = ds1.tables [0] .newrow();
dr [0] = dr2 [0];
dr [1] = dr2 [1];
//与其他列类似

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


//现在将其更新到数据库
cmd =新的sqlcommandbuilder(da);
da.update(ds1);






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

</code>
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>


这篇关于合并具有相同结构的2个表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆