如何在VBScript中逐行读取CSV文件

如何在VBScript中逐行读取CSV文件

本文介绍了如何在VBScript中逐行读取CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是一个ASP页面,我必须读取一个CSV文件,并将其插入到DB表Employee。我创建一个TestReader的对象。

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?

推荐答案

如何编写一个循环以执行多达正在读取的CSV文件的行数/记录数?不要尝试自己解析文件,你只会给自己头痛。有更多的东西比拆分换行和逗号。

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在记录集中打开文件,并像读取数据库表一样读取它。像这样:

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文件来处理此问题。请参阅:

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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:13