如何在 VBScript 中逐行读取 CSV 文件 [英] How to read CSV files line by line in VBScript
问题描述
我正在使用一个 ASP 页面,我必须在其中读取一个 CSV 文件并将其插入到数据库表Employee"中.我正在创建一个 TestReader 对象.如何编写一个循环来执行最多正在读取的 CSV 文件的行/记录数?
I am using an ASP page where I have to read a CSV file and insert it into DB table "Employee". I am creating an object of TestReader. How can I write a loop to execute up to the number of rows/records of the CSV file which is being read?
推荐答案
不要尝试自己解析文件,只会让自己头疼.除了在换行符和逗号上拆分之外,还有很多其他内容.
Do not try to parse the file yourself, you'll just give yourself a headache. There's quite a bit more to it than splitting on newline and commas.
您可以使用 OLEDB 在记录集中打开文件并像读取 db 表一样读取它.像这样的:
You can use OLEDB to open up the file in a recordset and read it just as you would a db table. Something like this:
Dim strConn, conn, rs
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath("path to folder") & ";Extended Properties='text;HDR=Yes;FMT-Delimited';"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open strConn
Set rs = Server.CreateObject("ADODB.recordset")
rs.open "SELECT * FROM myfile.csv", conn
while not rs.eof
...
rs.movenext
wend
我的 vbscript 生锈了,所以请验证语法.
My vbscript is rusty, so verify the syntax.
harpo 的评论提出了一个关于字段定义的好观点.定义 schema.ini 文件允许您定义预期字段的数量和数据类型.请参阅:您可以通过定义 schema.ini 文件来处理此问题.请参阅:http://msdn.microsoft.com/en-us/library/ms709353.aspx
edit: harpo's comment brings up a good point about field definitions. Defining a schema.ini file allows you to define the number and datatypes of the expected fields. See: You can handle this by defining a schema.ini file. see: http://msdn.microsoft.com/en-us/library/ms709353.aspx
这篇关于如何在 VBScript 中逐行读取 CSV 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!