views:

522

answers:

4

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
    }
}
A: 

Have you tried using this CSV reader? It is generally very respected. Perhaps give it a go...

Marc Gravell
+2  A: 

Try to use GetString() method in the reader when you need to read the column as string:

string myStringValue = reader.GetString(0);
mnour
I don't frequently do this type of code, I was not aware of the GetString method... Nice.
Brian Schmitt
+1  A: 

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.

Michael Prewecki
+1  A: 

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

JoelHess