2013-07-18 76 views
0

好的,我寫的腳本幾乎全功能,但我想添加兩件事。它目前的功能是鍵入與SQL Server中的數據庫上的其他數據關聯的CallID號碼。當您在msgbox中輸入數字時,它會檢索與該特定號碼關聯的所有其他列,並將其輸出到命令提示符,並將其輸出到硬盤上的文本文件中。這很好,但格式很糟糕。我將如何去改進文件的格式,以便更多地閱讀。目前,這只是一個空間分隔每條數據的路線。此外,我希望能夠做的還有與該列關聯的每條數據下的每個列的名稱。任何幫助,將不勝感激。這裏是我的代碼,省略了敏感信息:VBScript/SQL格式化問題

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 
+0

您可以包含您的數據現在的樣子以及您希望它的樣子的示例嗎? – aphoria

+0

它只是繼續在第一行然後到下一行。它只是一堆文本和數字,只是由空白分隔。我想在每一條數據下顯示列名,也許在每條數據之間添加一些分隔符。 –

+0

任何人都可以幫助我嗎? –

回答

1

您可以添加空間來製作固定寬度。假設您知道每個字段的字符數將爲20個字符或更少:

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