开发者

Importing "0000" or "0030" into DataTable

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?

H开发者_StackOverflow社区ere 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜