导入和QUOT; 0000"或QUOT; 0030"到数据表 [英] Importing "0000" or "0030" into DataTable

查看:131
本文介绍了导入和QUOT; 0000"或QUOT; 0030"到数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个文本文件导入像0000,0002,0023,0034值。然而,该表显示了他们为0,2,23,34,没有人知道为什么它去掉前导零?

下面是我的code:

 私人数据表ImportTabFile()
{
    字符串dataSourcePath = @C:\ Documents和Settings \ agordon \桌面;
    字符串dataFileName =ACTIVITYEX.txt;
    字符串CONNSTRING = @供应商= Microsoft.Jet.OLEDB.4.0;数据源=+ dataSourcePath + @;扩展属性=文字; HDR =无; FMT =分隔符号
    OleDbConnection的康恩=新的OleDbConnection(CONNSTRING);
    OleDbCommand的CMD = conn.CreateCommand();
    cmd.CommandText =的String.Format(SELECT * FROM [{0}],dataFileName);
    的DataSet ds为新的DataSet();
    OleDbDataAdapter的DA =新OleDbDataAdapter的(CMD);

    da.Fill(DS);

    返回ds.Tables [0];
}
 

我怎样才能在导入过程中保持前导零?

这里是我的架构文件看起来像:

  [ACTIVITYEX.txt]
格式= TabDelimited
ColNameHeader = FALSE
Col13 =错误code文本
 

解决方案

JET将根据第一行推断类型。这可能是你的推断字段是 INT ,因为 0000 可以转换为数字。正如上面提到的,领先的零是没有用的。

您可以使用 的Schema.ini 明确定义的列类型。正如上述评论指出的那样,你需要指定每列按文档:

  

您必须通过一些指定每个列 并指定列名,数据类型和宽度固定长度的文件

或者<一href="http://www.tableausoftware.com/support/knowledge-base/tableau-does-not-correctly-recognize-excel-columns"相对=nofollow>这篇文章对控制的类型是如何推断出一些信息。

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

解决方案

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.

这篇关于导入和QUOT; 0000&QUOT;或QUOT; 0030&QUOT;到数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆