使用vbs在数据库中使用记录命名文件

name a file using a record in the database using vbs

好吧,我正在尝试制作一个可视化脚本,它创建一个文件,其中写入记录集,文件名作为数据库的记录,举个例子,我想要一个查询,查询写在文件中,文件名是table的行之一,这里是我的代码:

Dim Filename 
Dim Connection 
Dim commandoSQL 
Dim Archivo 
Dim Nombre 
Dim objFSO 
Dim outputFile 

Call Main
Sub Main
    Set objFSO=CreateObject("Scripting.FileSystemObject")
    Call startConnection()  Set commandoSQL = Connection.Execute("select rest_def.obj_num, hdr_def.line_01, hdr_def.line_02, hdr_def.line_03, hdr_def.line_04, hdr_def.line_05, hdr_def.line_06, trlr_def.line_01, trlr_def.line_02, trlr_def.line_03, trlr_def.line_04, trlr_def.line_05, trlr_def.line_06, trlr_def.line_07, trlr_def.line_08, trlr_def.line_09, trlr_def.line_10, trlr_def.line_11, trlr_def.line_12 from rest_def inner join hdr_def on hdr_def.obj_num rest_def.obj_num inner join trlr_def on trlr_def.obj_num = hdr_def.obj_num where hdr_def.obj_num = 101")
    'Archivo = "D:\archives\"
    Set Filename = Connection.Execute("Select obj_num FROM rest_def")
    Nombre = Filename.getString
    Archivo = "D:\archives\" + Nombre + ".txt"
    Set outputFile = objFSO.CreateTextFile(Archivo,True)
    outputFile.Write commandoSQL & vbCrLf
    outputFile.Close
    Call closeConnection()
End Sub

Sub startConnection()
    Set Connection = WScript.CreateObject("ADODB.Connection")
    Connection.Open "DSN=milo; UID=dataBase; PWD=password"
End Sub

Sub closeConnection()
    Connection.Close 
End Sub

所以如果 Filename 的数据是 7890,我希望将文件命名为 7890.txt,并在文件中写入所有记录集...请帮忙,这是我第一次尝试使用 vbs.. .

如果您想在 VBScript 中使用 ADODB 对象,我建议您先阅读 ADO API Reference 以熟悉各种对象。

在这种特殊情况下,行

Set Filename = Connection.Execute("Select obj_num FROM rest_def")

returns 一个 ADODB.Recordset 对象,要访问您将使用 Fields 集合的查询结果,并在本例中传递返回字段的序号位置 0 或命名字段 obj_num.

这会将此行从

Nombre = Filename.getString

Nombre = Filename.Fields("obj_num").Value

ADODB.Recordset对象也没有getString()这样的方法。

Main() 子过程更改为

Sub Main
    Set objFSO=CreateObject("Scripting.FileSystemObject")
    Call startConnection()
    Set Filename = Connection.Execute("Select obj_num FROM rest_def")
    'Continue to iterate through the recordset until we reach the last
    'record (EOF).
    Do While Not Filename.EOF
        Nombre = Filename.Fields("obj_num").Value & ""
        Archivo = "D:\archives\" & Nombre & ".txt"
        Set outputFile = objFSO.CreateTextFile(Archivo, True)
        outputFile.Write commandoSQL & vbCrLf
        outputFile.Close
        'Move to next record
        Call Filename.MoveNext()
    Loop
    Call closeConnection()
End Sub

应该可以。这取决于您是否要为返回的每条记录创建一个文件,但根据您有限的解释,这是我的解释。