更新时间:2023-12-01 10:57:22
好的,这是你当前问题的解决方案。
但是,如果你的数据库中有这样一个表结构,有两个类似的表,你应该重新考虑你的数据库设计,因为它完全浪费了空间两个如此相似的表格。
我的样本数据DataTable dt1 = new DataTable();
dt1.Columns.Add( slno, typeof ( int ));
dt1.Columns.Add( name, typeof ( string ));
dt1.Columns.Add( year, typeof ( double ));
dt1.Columns.Add( amount, typeof ( int ));
dt1.Columns.Add( bank, typeof ( string ));
dt1.Columns.Add( address, typeof ( string ));
dt1.Columns.Add( company, typeof ( string ));
dt1.Rows.Add( 1 , John, 2014 , 2000 , ING, oruf, Google);
dt1.Rows.Add( 2 , Mik, 2014 , 5000 , ING, tiejd, Micro);
dt1.Rows.Add( 3 , Robin, 2014 , 1000 , INF, trydh, Jen);
DataTable dt2 = new DataTable();
dt2.Columns.Add( slno, typeof ( int ));
dt2.Columns.Add( name, typeof ( string ));
dt2.Columns.Add( year, typeof ( int ));
dt2.Columns.Add( amount, typeof ( int ));
dt2.Columns.Add( bank, typeof ( string ));
dt2.Columns.Add( address, typeof ( string ));
dt2.Rows.Add( 1 , John, 2014 , 2000 , ING, oruf跨度>);
dt2.Rows.Add( 2 , Mik, 2014 , 5000 , ING, tiejd);
dt2.Rows.Add( 3 , 甜瓜, 2013 , 21551 , SBI, hhdh);
假设您有两个名为dt1和dt2的表,则创建一个与dt1具有相同列数的第三个表。DataTable dt3 = new DataTable();
dt3.Columns.Add( slno, typeof ( int ));
dt3.Columns.Add( name, typeof ( string ));
dt3.Columns.Add( year, typeof ( int ));
dt3.Columns.Add( amount, typeof ( int ));
dt3.Columns.Add( bank, typeof ( string ));
dt3.Columns.Add( address, typeof ( string ));
dt3.Columns.Add( company, typeof ( string ));
// 从最小的行获取行数和列数表
int noRows =(dt1.Rows.Count < dt2。 Rows.Count)? dt1.Rows.Count:dt2.Rows.Count;
int noCols =(dt1.Columns.Count < dt2.Columns.Count)? dt1.Columns.Count:dt2.Columns.Count;
for ( int i = 0 ; i < noRows; i ++)
{
DataRow dr1 = dt1.Rows [i];
DataRow dr2 = dt2.Rows [i];
bool equal = true ;
for ( int j = 0 ; j < noCols; j ++)
{
// 如果一列不同,则中断并不添加此行
if (!dr1 [j ] .Equals(dr2 [j]))
{
equal = false ;
break ;
}
}
如果(等于)
{
// 将Table1中的行中的内容添加到Table3中
dt3.LoadDataRow(dr1.ItemArray,真跨度>);
}
}
你应该阅读这个主题。即使它不是你的设计。
数据库规范化 [ ^ ]
3普通表格数据库教程 [ ^ ]
数据库规范化基础知识 [ ^ ]
也许这会帮助你比较表格:
转换设置加入SQL参数 [ ^ ]
如何比较2个表的行 [ ^ ]
比较2个表 [ ^ ]
can you please help me with this..
i have two table
table1
slno name year amount bank address company
1 John 2014 2000 ING oruf Google
2 Mik 2014 5000 ING tiejd Micro
3 Robin 2014 1000 INF trydh Jen
table2
slno name year amount bank address
1 John 2014 2000 ING oruf
2 MIk 2014 5000 ING tiejd
3 melon 2013 21551 SBI hhdh
i want to compare this two table from table1 to table2
If the fields matches like
[Matching fields ] after comparing from table1 to table2
slno name year amount bank address company
1 John 2014 2000 ING oruf Google
2 MIk 2014 5000 ING tiejd Micro
if file is matching then it will Insert those matching fileds from table1 to a new table[table3]
as an update
i am using Asp.net(Charp, C#) and sql server
OK this is a solution of your current problem.
However, if you have such a table structure in your database with two tables this similar, you should reconsider your database design as it is a complete waste of space with two so similar tables.
My sample dataDataTable dt1 = new DataTable(); dt1.Columns.Add("slno", typeof(int)); dt1.Columns.Add("name", typeof(string)); dt1.Columns.Add("year", typeof(double)); dt1.Columns.Add("amount", typeof(int)); dt1.Columns.Add("bank", typeof(string)); dt1.Columns.Add("address", typeof(string)); dt1.Columns.Add("company", typeof(string)); dt1.Rows.Add(1, "John", 2014, 2000, "ING", "oruf", "Google"); dt1.Rows.Add(2, "Mik", 2014, 5000, "ING", "tiejd", "Micro"); dt1.Rows.Add(3, "Robin", 2014, 1000, "INF", "trydh", "Jen"); DataTable dt2 = new DataTable(); dt2.Columns.Add("slno", typeof(int)); dt2.Columns.Add("name", typeof(string)); dt2.Columns.Add("year", typeof(int)); dt2.Columns.Add("amount", typeof(int)); dt2.Columns.Add("bank", typeof(string)); dt2.Columns.Add("address", typeof(string)); dt2.Rows.Add(1, "John", 2014, 2000, "ING", "oruf"); dt2.Rows.Add(2, "Mik", 2014, 5000, "ING", "tiejd"); dt2.Rows.Add(3, "melon", 2013, 21551, "SBI", "hhdh");
Assuming you have two tables called dt1 and dt2, you create a third table that has the same number of columns as dt1.DataTable dt3 = new DataTable(); dt3.Columns.Add("slno", typeof(int)); dt3.Columns.Add("name", typeof(string)); dt3.Columns.Add("year", typeof(int)); dt3.Columns.Add("amount", typeof(int)); dt3.Columns.Add("bank", typeof(string)); dt3.Columns.Add("address", typeof(string)); dt3.Columns.Add("company", typeof(string));
// Get the row and column count from the smallest table int noRows = (dt1.Rows.Count < dt2.Rows.Count) ? dt1.Rows.Count : dt2.Rows.Count; int noCols = (dt1.Columns.Count < dt2.Columns.Count) ? dt1.Columns.Count : dt2.Columns.Count; for (int i = 0; i < noRows; i++) { DataRow dr1 = dt1.Rows[i]; DataRow dr2 = dt2.Rows[i]; bool equal = true; for (int j = 0; j < noCols; j++) { // If one column differs, then break and do not add this row if (!dr1[j].Equals(dr2[j])) { equal = false; break; } } if (equal) { // Add the contents from the row in Table1 into Table3 dt3.LoadDataRow(dr1.ItemArray, true); } }
You should probably read up on this topic. Even if it is not your design.
Database normalization[^]
3 Normal Forms Database Tutorial[^]
Database Normalization Basics[^]
Maybe this'll help you in comparing the tables:
Convert Set To Join SQL Parameter[^]
How To Compare The Rows Of 2 Tables[^]
Compare 2 Tables[^]