CSV,Excel文件阅读 [英] CSV, Excel File Reading

查看:90
本文介绍了CSV,Excel文件阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在两个数据库中都有相同的表(一个是源,另一个是目标)。我想比较这两个表使用C#amd找出哪些记录被插入以及哪些更新。基于此我必须生成最终的CSV文件或Excel文件,必须将其插入目标数据库中的同一个表中。





提前致谢。

Hi,

I have same table in both two databases(one is ource and other is target). I want to compare those two tables using C# amd find out which records are inserted and which are updated.Based on that I have to generate the final CSV file or Excel file, which has to be inserted into the target database into the same table.


Thanks in advance.

推荐答案

您可以比较金额行...



如果他们的新行中有更多行,则表示已添加记录。这只有在您知道其他数据相同的情况下才有效。



You can compare the amount of lines ...

If their are more lines in your new CSV then that means records have been added. This will only work if you know that other data is the same.

[] oldCSV = System.IO.File.ReadAllLines("LOCATION");
[] newCSV = System.IO.File.ReadAllLines("LOCATION");

if ((newCSV.Length - 1) > (oldCSV.Length)) {
    //their are differences so get the new differences
    for (diff = Convert.ToInt32(((newCSV.Length - 1) > (oldCSV.Length))); diff <= newCSV.Length; diff++) {
        //do what you want with the changes
    }
} else {
    //no changes
}





如果你想比较每一行,看看是否确定价值已经改变,然后你可以遍历它们。





If you want to compare each line to see if a certain value has been changed then you can loop through both of them.

[] oldCSV = System.IO.File.ReadAllLines("LOCATION");
    [] newCSV = System.IO.File.ReadAllLines("LOCATION");

    foreach (object oldRecord_loopVariable in oldCSV) {
        oldRecord = oldRecord_loopVariable;
        foreach (object newRecord_loopVariable in newCSV) {
            newRecord = newRecord_loopVariable;
            if (oldRecord == newRecord) {
            //no record changed
            } else {
                //something is not the same
            }
        }
    }


这篇关于CSV,Excel文件阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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