问题
I have a CSV file that has a column that contains strings that look like integers. That is they should be dealt with as strings, but since they are numbers they appear to be imported as integers (dropping off the leading zeroes).
Example Data:
- 0000000000079
- 0000999000012
- 0001002000005
- 0004100000007
The problem I'm seeing is that the last example data point comes through as DBNull.Value. I'm assuming this is because OleDB is treating that column as an integer (the data points come through without their leading zeroes also) and that 0004100000007 is greater than the largest integer value.
Is there some way to say "column [0] is a string, don't read it as an integer"? When reading the data?
The code I am currently using is:
OleDbConnection dbConn = new OleDbConnection(SourceConnectionString);
OleDbCommand dbCommand = new OleDbCommand("SELECT * FROM test.csv", dbConn);
dbConn.Open();
OleDbDataReader dbReader = dbCommand.ExecuteReader();
while (dbReader.Read())
{
if (dbReader[0] != DBNull.Value)
{
// do some work
}
}
回答1:
Try to use GetString() method in the reader when you need to read the column as string:
string myStringValue = reader.GetString(0);
回答2:
Do you have control of the export process? If so can data be exported to CSV with quotes around the string items?
If this is a one of job then just import the file into a predfined SQL table using Integration Services, but I suspect this will be a recurring task.
回答3:
There's a Schema.ini file that needs to be used to specify the info about the file. It includes field types and lengths, whether there are column headers and what the field delimiter is.
Here's the MSDN Info on it.
http://msdn.microsoft.com/en-us/library/ms709353.aspx
回答4:
Have you tried using this CSV reader? It is generally very respected. Perhaps give it a go...
回答5:
I'm no expert but do you cope with fixed file format ? http://csharptutorial.com/blog/exclusive-how-to-export-a-datatable-to-a-fixed-file-format-the-easy-way-using-odbc-or-jet-engine/
来源:https://stackoverflow.com/questions/237763/how-in-net-do-i-import-values-from-a-csv-in-the-format-i-want-using-oledb