ADO 提取 CSV 的数据类型问题

发布于 2024-11-04 00:55:49 字数 364 浏览 1 评论 0原文

好的,在使用 ADO 从 CSV 文件中提取邮政编码时遇到问题。发生的情况是,如果第一组邮政编码的长度为 5 位,则数据类型将被分配为记录集中整个字段属性的整数,如果以下邮政编码不是 9 位邮政编码,则不会出现问题带有破折号的代码(99999 与 99999-9999)。当遇到这些 9 位邮政编码时,该字段将生成空值。因此,我只能假设 JET 4 获取一小部分数据样本(前 3 条记录?)并将数据类型分配给该字段。我尝试在运行时设置字段类型,但失败了,因为我不知道我在做什么,或者它超出了我的控制范围。

有几点值得注意: 1.我可以控制提取信息的SQL语句(使用group by子句) 2. 在数据被拉入之前我的控制能力有限。 3.运行语言VB6使用ADO。

有什么建议或指示吗?

Ok, running into a problem with pulling a Zip code from a CSV file using ADO. Whats happening is that if the first set of zip codes are 5 digits long, the datatype is assigned as an integer for the whole field property in the recordset which wouldn't be a problem if the following zip codes weren't the 9 digit zip code with the dash (99999 vs 99999-9999). When these 9 digit zip codes are encountered, the field yields a null. So, I can only assumet that JET 4 takes a small sample of the data (first 3 records?) and assigns the datatype to that field. Ive tried setting the field type during runtime, but failed either because I don't know what I am doing or its beyond my control.

A couple of note worthies:
1. I have control of the SQL statement that pulls the info (which uses a group by clause)
2. I have limited control before the data is pulled in.
3. The runtime language VB6 using ADO.

Any suggestions or pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱冒险 2024-11-11 00:55:49

似乎执行此操作的常见方法是使用 架构.ini文件

当使用文本驱动程序时,
文本文件的格式已确定
通过使用模式信息文件。
架构信息文件始终是
命名为 Schema.ini 并始终保存在
与文本数据相同的目录
来源。架构信息文件
向 IISAM 提供信息
关于文件的一般格式,
列名和数据类型
信息和其他一些数据
特征。 Schema.ini 文件是
始终需要访问
固定长度数据。你应该使用
Schema.ini 文件当你的文本表
包含日期时间、货币或
十进制数据,或任何您认为
想要更多地控制处理
表中的数据。

我使用以下文件设置了一个示例:

test.csv:

normal text,12345
some more ordinary words,12345-9876

Schema.ini

[test.csv]
Format=CSVDelimited
ColNameHeader=False
Col1=Dummy Text
Col2=ZipCode Text

test.vbs

Option Explicit

Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim oConn : Set oConn = CreateObject("ADODB.Connection")
Dim rs : Set rs = CreateObject("ADODB.Recordset")

Dim scriptPath : scriptPath = fso.GetParentFolderName(WScript.ScriptFullName)

oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & scriptPath & ";" & _
           "Extended Properties=""text;HDR=NO"""

rs.Open "SELECT * FROM test.csv", oConn

WScript.Echo rs.Fields(0).Name & "|" & rs.Fields(1).Name

Do Until rs.EOF
    WScript.Echo rs.Fields(0).Value & "|" & rs.Fields(1).Value
    rs.MoveNext
Loop

rs.Close
oConn.Close

输出如下所示:

Dummy|ZipCode
normal text|12345
some more ordinary words|12345-9876

请记住Schema.ini 文件必须与文本文件位于同一文件夹中,并且必须按名称引用文本文件(上例中的 [test.csv])。您可能必须为导入的每个文件动态生成 Schema.ini 文件,或者复制 csv 文件并将其重命名到临时位置。

It seems like the common way to do this is using a Schema.ini file:

When the Text driver is used, the
format of the text file is determined
by using a schema information file.
The schema information file is always
named Schema.ini and always kept in
the same directory as the text data
source. The schema information file
provides the IISAM with information
about the general format of the file,
the column name and data type
information, and several other data
characteristics. A Schema.ini file is
always required for accessing
fixed-length data. You should use a
Schema.ini file when your text table
contains DateTime, Currency, or
Decimal data, or any time that you
want more control over the handling of
the data in the table.

I set up an example using the following files:

test.csv:

normal text,12345
some more ordinary words,12345-9876

Schema.ini

[test.csv]
Format=CSVDelimited
ColNameHeader=False
Col1=Dummy Text
Col2=ZipCode Text

test.vbs

Option Explicit

Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim oConn : Set oConn = CreateObject("ADODB.Connection")
Dim rs : Set rs = CreateObject("ADODB.Recordset")

Dim scriptPath : scriptPath = fso.GetParentFolderName(WScript.ScriptFullName)

oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & scriptPath & ";" & _
           "Extended Properties=""text;HDR=NO"""

rs.Open "SELECT * FROM test.csv", oConn

WScript.Echo rs.Fields(0).Name & "|" & rs.Fields(1).Name

Do Until rs.EOF
    WScript.Echo rs.Fields(0).Value & "|" & rs.Fields(1).Value
    rs.MoveNext
Loop

rs.Close
oConn.Close

And the output looks like:

Dummy|ZipCode
normal text|12345
some more ordinary words|12345-9876

Keep in mind that the Schema.ini file must be in the same folder as the text file and must refer to the text file by name ([test.csv] in the example above). You may have to dynamically generate the Schema.ini file for each file you import, or make a copy and rename of the csv file to a temporary location.

淡写薰衣草的香 2024-11-11 00:55:49

是的,ADO 确实根据第一条记录进行类型强制。破折号“-”是整数类型中的无效字符,因此不会解析它们。您希望整个内容都是一个字符串(实际上,邮政编码是字符串,而不是整数)。

准备数据可能是值得的。编写一个小程序来读取 CSV 并将 -0000 添加到 5 位记录中。那么所有记录都将是 10 位数字,带有 -,并且应该被强制转换为字符串。

Yes, ADO does type coercion based on the first record. Dash '-' is an invalid character in an integer type, so it won't parse them. You want the entire thing to be a string (Really, you do- zip codes are strings, not integers).

It might be worth it to preparse the data. Write a small program to read the CSV and add -0000 to the 5-digit records. Then all records will be 10 digits, with a -, and should be coerced to a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文