一个CSV文件大概有100万条数据需要处理,用分号;分割,比如下面的例子:
id;name;class;email;tel
000001;zhao;A;[email protected];12345677
000002;qian;A;[email protected];12345678
000003;sun;C;[email protected];12345679
000004;li;B;[email protected];12345680现在需要用程序把他们读出,(然后根据ID插入/更新到另一个系统)问题是100万条数据,用什么方法读取更高效?是一次性加载到一个LIST里面然后在执行插入/更新操作呢?还是一条一条的读取在执行插入/更形操作?还有一个很重要的要求,就是假若插入/更新操作有问题,那么要求系统记录下来出现问题的ID和出现的问题,并继续下一条操作。求源代码(插入/更新操作可以直接用文字注明)
id;name;class;email;tel
000001;zhao;A;[email protected];12345677
000002;qian;A;[email protected];12345678
000003;sun;C;[email protected];12345679
000004;li;B;[email protected];12345680现在需要用程序把他们读出,(然后根据ID插入/更新到另一个系统)问题是100万条数据,用什么方法读取更高效?是一次性加载到一个LIST里面然后在执行插入/更新操作呢?还是一条一条的读取在执行插入/更形操作?还有一个很重要的要求,就是假若插入/更新操作有问题,那么要求系统记录下来出现问题的ID和出现的问题,并继续下一条操作。求源代码(插入/更新操作可以直接用文字注明)
解决方案 »
- chart控件的使用
- c# 的 bitmap类 对应于C++ 中的 什么类?
- 怎么用C#合并文件并排序?
- oracle返回插入ID问题
- 请问一下如何保护自己写的控件,也就是如何使用licenses.licx
- 如何下Microsoft Visual Studio Express Editions
- C#GDI+中如何改变MainMenu 那一行的菜单及窗体的颜色?
- 请问一个关于用webform的页面打开winform程序的问题
- Excel读取的问题
- XmlValidatingReader 对XSD进行验证,到底是验证什么?
- 请问:.net 2.0 ADO.net技术 与.net 4.0 ADO.net技术的区别?
- 请问在C#中如何读取一张光盘的Media ID?
感觉出错的记录应该还要包括错误条目在文件的偏移
static void Main(string[] args)
{
string path = "data.csv";
try
{
StreamReader sr = new StreamReader(path);
string line;
while ((line = sr.ReadLine()) != null)
{
string[] arrStr = line.Split(';');
if (arrStr.Length != 5)
{
throw new ApplicationException("列数有误:" + line);
}
string aaaa = arrStr[0];
if(!Regex.IsMatch(aaaa,"^\\d{6}$"))
{
throw new ApplicationException("id有误:" + arrStr[0]);
}
if (!Regex.IsMatch(arrStr[2], "^[A-Z]$"))
{
throw new ApplicationException("class有误:" + arrStr[2]);
}
if (!Regex.IsMatch(arrStr[3], @"^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$"))
{
throw new ApplicationException("email有误:" + arrStr[3]);
}
// ......
Console.WriteLine(line);
} sr.Dispose();
sr.Close(); // 关闭流
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
} Console.ReadKey();
}
2; id有误
1034; email有误
...
有个问题,如果发现有你所说的错误,需要遍历时改过来吗?
如果需要把改写好好也存到csv里面,就还需要一个写的方法: public static void WriteCSV(string filePathName,bool append, List<String[]> ls)
{
StreamWriter fileWriter=new StreamWriter(filePathName,append,Encoding.Default);
foreach(String[] strArr in ls)
{
fileWriter.WriteLine(String.Join (“;",strArr) );
}
fileWriter.Flush();
fileWriter.Close();
}
不过,我这类也有一个封装好的读的方法,给你参考: public static List<String[]> ReadCSV(string filePathName)
{
List<String[]> ls = new List<String[]>();
StreamReader fileReader=new StreamReader(filePathName);
string strLine="";
while (strLine != null)
{
strLine = fileReader.ReadLine();
if (strLine != null && strLine.Length>0)
{
ls.Add(strLine.Split(','));
//Debug.WriteLine(strLine);
}
}
fileReader.Close();
return ls;
}
这个csv文件的第一行是行名,应该被忽略掉的;
而且读取文件出错的时候,记录下出错的id和错误信息,然后继续读取下一行数据。目前的程序直接退出了。
例如先把数据在程序里整理了一下,然后写入文件A.txt,然后数据库调用bulkinsert 一次性写入
经测试40万的数据量7秒就搞定
这个csv文件的第一行是行名,应该被忽略掉的;"
第一行一看就知道是列名,读起来也没关系。 List<String[]> ls;
无非在你使用的时候从1的索引开始!列名称留着,说不定到时还有用途