[英]Parsing CSV using OleDb using C# - Error column contain values with in double quotes those are not loaded in datatable
I am using OleDb to convert csv file as data table in c#. 我正在使用OleDb将csv文件转换为c#中的数据表。
I have problem with columns which has value having double quotes("), in this case after double quotes values and remaining columns values are not loaded in datatable. 我对具有双引号(“)的值的列有问题,在这种情况下,双引号值和剩余的列值未加载到数据表中之后。
here this is my code, 这是我的代码,
private DataTable ReadFileByDataTable (string sFileName) { 专用DataTable ReadFileByDataTable(字符串sFileName){
DataTable dtPOFileData = new DataTable(); DataTable dtPOFileData = new DataTable();
using (OleDbConnection cnnOleDB = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Test;Extended Properties='text;HDR=Yes;FMT=Delimited;'"))
{
cnnOleDB.Open();
using (OleDbDataAdapter adpPOFileData = new OleDbDataAdapter("SELECT * FROM ["test.csv"]", cnnOleDB))
{
adpPOFileData.Fill(dtPOFileData);
}
}
return dtPOFileData;
}
thanks in advance. 提前致谢。
I have tried this delimiter option and solve my problem OleDbConnection cnnOleDB = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Test;Extended Properties='text;HDR=Yes;FMT=Delimited(,)';'" 我已经尝试使用此定界符选项并解决了我的问题OleDbConnection cnnOleDB = new OleDbConnection(“ Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\\ Test; Extended Properties ='text; HDR = Yes; FMT = Delimited(, ) ';'”
here full version of the code, 这里是完整版本的代码,
private DataTable ReadFileByDataTable (string sFileName) { DataTable dtPOFileData = new DataTable(); 专用DataTable ReadFileByDataTable(字符串sFileName){DataTable dtPOFileData = new DataTable();
using (OleDbConnection cnnOleDB = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Test;Extended Properties='text;HDR=Yes;FMT=Delimited(,)';"))
{
cnnOleDB.Open();
using (OleDbDataAdapter adpPOFileData = new OleDbDataAdapter("SELECT * FROM ["test.csv"]", cnnOleDB))
{
adpPOFileData.Fill(dtPOFileData);
}
}
return dtPOFileData;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.