4

I am importing values like 0000,0002,0023,0034 from a text file. However, the table shows them as 0, 2, 23, 34. Does anyone know why it is removing the leading zeros?

Here is my code:

private DataTable ImportTabFile()
{
    string dataSourcePath = @"C:\Documents and Settings\agordon\Desktop";
    string dataFileName = "ACTIVITYEX.txt";
    string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dataSourcePath + @";Extended Properties=""text;HDR=No;FMT=Delimited""";
    OleDbConnection conn = new OleDbConnection(connString);
    OleDbCommand cmd = conn.CreateCommand();
    cmd.CommandText = String.Format("SELECT * FROM [{0}]", dataFileName);
    DataSet ds = new DataSet();
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);

    da.Fill(ds);

    return ds.Tables[0];
}

How can I keep the leading zeroes during the import?

here is what my schema file looks like:

[ACTIVITYEX.txt]
Format=TabDelimited
ColNameHeader=False
Col13=ErrorCode Text
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

1 Answers1

7

JET will infer types based on the first row. It is probably inferring your fields are INT because 0000 can be converted to a number. As mentioned above, leading zeroes are useless.

You can use schema.ini to explicitly define the column types. As noted in the comments above, you need to specify each column as per the documentation:

You must specify each column by number and designate the column name, data type, and width for fixed-length files.

Alternatively this article has some information on controlling how the types are inferred.

ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
  • +1 beat me to it (and I had my answer ready to post). I was going to add that he can also programmatically construct his DataTable and define the column he's having problems with as string and fill that. – Jay Riggs Jun 02 '11 at 03:23
  • 3
    I normally don't reply to these sorts of questions but I felt sorry for the guy because the comments to his question border on moronic. – ta.speot.is Jun 02 '11 at 03:25
  • And I would not have thought to construct the `DataTable` and explicitly specify the column types. You have a very good point. – ta.speot.is Jun 02 '11 at 03:26
  • I thought it was a good question. I forgot about schema.ini and I'm glad to be reminded of it. – Jay Riggs Jun 02 '11 at 03:26
  • @todd thanks so much for your help,. is it possible to do this without effecting the registry? – Alex Gordon Jun 02 '11 at 03:39