VBScript / SQL格式问题

时间:2013-07-18 14:18:55

标签: vbscript

好的,我编写的脚本几乎完全正常运行,但我想添加两件事。它目前所做的是输入与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

1 个答案:

答案 0 :(得分:1)

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

objOutPutFile.WriteLine  strfield & String(20-Len(strfield)," ") & "|"