[英]Issue with importing .csv using MS Jet OLEDB
I'm importing a CSV file to SQL for an ASP .NET application. 我正在将CSV文件导入SQL以用于ASP .NET应用程序。 I am able to import the .csv, however one column contains null values if there is anything other than numbers in it. 我可以导入.csv,但是如果其中有数字以外的任何一列,则包含空值。
This row imports fine: 该行可以导入:
1109,003,IN,0093219095,3/17/2013,3/21/2013,,Sobeys Warehouse,4819.13,61.37,4880.50,RV,1109-003
The fourth column is NULL in SQL: 第四列在SQL中为NULL:
1109,999,IN,REF 44308/S. DRA,3/18/2013,3/21/2013,,"EC Rebates W/E -02 14, 2013",-200.02,0.00,-200.02,SA,1109-999
All other columns that have text in them import fine, just the fourth one is an issue. 其他所有带有文本的列都可以正常导入,只有第四列是一个问题。 I can't figure out what could possibly be different about the affected column over the others. 我无法弄清楚受影响的列与其他列可能有何不同。 If I replace the text with numbers it imports so its something to do with text data. 如果我用数字替换文本,它将导入,因此与文本数据有关。 SQL field is nvarchar(50) so its not a datatype issue. SQL字段是nvarchar(50),因此它不是数据类型问题。
My connection string ( dir contains the path to the folder): 我的连接字符串( dir包含文件夹的路径):
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + dir + "\\\";Extended Properties='text;HDR=Yes;FMT=Delimited(,)';";
Import code: 导入代码:
public DataTable GetCSV(string path)
{
if (!File.Exists(path))
{
return null;
}
DataTable dt = new DataTable();
string fullPath = Path.GetFullPath(path);
string file = Path.GetFileName(fullPath);
string dir = Path.GetDirectoryName(fullPath);
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + dir + "\\\";Extended Properties='text;HDR=Yes;FMT=Delimited(,)';";
string query = "SELECT * FROM " + file;
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(query, connString);
try
{
da.Fill(dt);
}
catch (InvalidOperationException)
{
}
da.Dispose();
return dt;
}
This solved my issue. 这解决了我的问题。 Thanks to Pondlife for getting me pointed in the right direction. 感谢Pondlife为我指明正确的方向。 I figured it had something to do with data types, just not sure where it was getting walloped. 我发现它与数据类型有关,只是不确定它在哪里崩溃。
http://msdn.microsoft.com/en-us/library/windows/desktop/ms709353%28v=vs.85%29.aspx http://msdn.microsoft.com/en-us/library/windows/desktop/ms709353%28v=vs.85%29.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.