如何通过在VBScript行改为CSV文件的行 [英] How to read CSV files line by line in VBScript

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

问题描述

我使用的是ASP页面,在这里我要读一个CSV文件,并将其插入到数据库表雇员。我创建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在记录打开该文件,并读取它就像你将一个数据库表。事情是这样的:

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.

编辑:哈波的评论带来了约字段定义好点。定义一个schema.ini文件允许你定义预期的字段的数量和数据类型。请参阅:您可以通过定义一个schema.ini文件处理这个问题。请参见:<一href=\"http://msdn.microsoft.com/en-us/library/ms709353.aspx\">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屋!

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