0

好的,所以我编写的脚本几乎可以正常工作,但我想在其中添加两件事。它当前所做的是您键入与 SQL Server 中数据库上的其他数据相关联的 CallID 号码。当您在 msgbox 中输入数字时,它会检索与该特定数字关联的所有其他列,并将其输出到命令提示符,并将其输出到硬盘驱动器上的文本文件。这很好,但格式很糟糕。我将如何改进文件的格式以使其更具阅读性。目前它只是一条用空格分隔每条数据的线。此外,我还希望能够在与该列关联的每条数据下拥有每个列的名称。任何帮助,将不胜感激。

    Dim strQuery
    strQuery = InputBox("Enter CallID Please:")
    Dim sServer
    Dim sLogin
    Dim sPwd
    Dim sDb
    Dim oCn 
    Dim oRs
    Dim strFullQuery
    Dim strfield
    Const ForReading  = 1

    sServer   = ""
    sLogin    = ""
    sPwd      = ""
    sDb       = ""


    Set oCn = CreateObject( "ADODB.Connection" ) ' set oCn to create an object called ADODB.Connection
    Set oRs = CreateObject( "ADODB.Recordset"  ) ' set oRs to create an object called ADODB.Recordset

    oCn.ConnectionString = "PROVIDER=SQLOLEDB" & _      
                           ";SERVER="   & sServer   & _
                           ";UID="      & sLogin  & _
                           ";PWD="      & sPwd    & _
                           ";DATABASE=" & sDb & " "
                           oCn.ConnectionTimeout=600
                           oCn.open 'Open the connection to the server

    strFullQuery = "select * from dbo.CallLog where CallID=" + strQuery 'this is the SQL statement that runs a query on the DB

    oRs.Open strFullQuery,oCn 'This opens the record set and has two parameters, strFullQuery and oCn

    If oRs.EOF Then 'If open record set is at the end of file then...
      wscript.echo "There are no records to retrieve; Check that you have the correct record number." 'echo that there is no records to retrieve.
    End if

    'if there are records then loop through the fields
    oRs.MoveFirst 'move to the first object in the record set and set it as the current record
    Do Until oRs.EOF ' Do while not open record set is not end of file
    Set objFileSystem    = WScript.CreateObject("Scripting.FileSystemObject") 'Set objFileSystem to create object Scripting.FileSystemObject
    Set objOutPutFile    = objFileSystem.CreateTextFile("c:\test.txt", True) 'Set objOutPutFile to create object objFileSystem.CreateTextFile
    objOutPutFile.WriteLine strColumnNames
    strfield = oRs.GetString

    if strfield <> "" then 'If strfield doesn't equal "" then


      WScript.echo strfield  
      objOutPutFile.WriteLine  strfield &"|"

      'objFileSystem.close
      objOutPutFile.close  
    end If

    'oRs.MoveNext 'Move to the next object in the record set
    Loop
    oCn.Close
4

1 回答 1

1

您可以添加空间来制作固定宽度。假设您知道每个字段不超过 20 个字符:

objOutPutFile.WriteLine  strfield & String(20-Len(strfield)," ") & "|"
于 2013-07-18T20:06:05.723 回答