C#上传csv,列数据类型问题 [英] C# upload csv , column data type problem

查看:167
本文介绍了C#上传csv,列数据类型问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码从csv文件读取数据.但是在被视为数字列的列中无法读取字符串值.
例如,有一个包含字符串值"AAA"的列,此列的其余部分包含数字值,例如1,2,3.因此,当我读取它时,字符串值将被读取为",而不是正确的结果"AAA".

请告诉我如何处理此问题,谢谢.

//Fetch the location of CSV file 

string filePath = Server.MapPath("UploadedCSVFiles") + "\\";

string strSql = "SELECT * FROM [" + fileInfo.Name + "]";

string strCSVConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";" + "Extended Properties=''text;HDR=YES;''";



// load the data from CSV to DataTable 



OleDbDataAdapter adapter = new OleDbDataAdapter(strSql, strCSVConnString);

DataTable dtCSV = new DataTable();

DataTable dtSchema = new DataTable();


adapter.FillSchema(dtCSV, SchemaType.Mapped);

adapter.Fill(dtCSV);



而且我想知道是否可以将Jet设置为将所有列都视为字符串数据类型.选择之后,我将字符串转换为数字.

解决方案

我会尝试使用此 FileHelpers库 [ ^ ]而不是处理Jet

我更喜欢在不使用JET的情况下手动操作CSV文件,这样您就可以更好地控制文件的读取方式以及数据结构的填充方式.您可以构建适当的CSV解析器库,而无需涉及JET,或者如Mark所指出的那样,使用不使用JET的现有库.我相信CSV是最简单的文件格式,您可以使用String/StringBuilder类,数组,列表等轻松操作...

干杯.


I use the code below to read data from a csv file. But the string values can''t be read where the columns which are considered as a number column.
For example, there is a column which contains a string value "AAA", and the rest of this column contains number value such as 1,2,3. So when I read it, the string value will be read as "" instead of correct result "AAA".

please tell me how to deal with this problem, thanks.

//Fetch the location of CSV file 

string filePath = Server.MapPath("UploadedCSVFiles") + "\\";

string strSql = "SELECT * FROM [" + fileInfo.Name + "]";

string strCSVConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";" + "Extended Properties=''text;HDR=YES;''";



// load the data from CSV to DataTable 



OleDbDataAdapter adapter = new OleDbDataAdapter(strSql, strCSVConnString);

DataTable dtCSV = new DataTable();

DataTable dtSchema = new DataTable();


adapter.FillSchema(dtCSV, SchemaType.Mapped);

adapter.Fill(dtCSV);



And I wonder if I can set the Jet to let all the columns are considered as string data type. And after select , I convert the string to number.

解决方案

I would try using this FileHelpers Library[^] rather than dealing with the Jet


I prefer to manipulate CSV files manually without using JET, that way you have more control over how the file is read and how your data structures are populated. You can build a proper CSV parser library without ever involving JET or as Mark pointed out using an existing library that doesn''t use JET. I believe CSV is the simplest file format that you can easily manipulate using String/StringBuilder classes, arrays, Lists etc...

Cheers.


这篇关于C#上传csv,列数据类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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