2012-03-26 47 views
0

我儘量讓VB腳本轉儲的SQL Server表到CSV文件VB腳本轉儲的SQL Server表到CSV

Option Explicit 
Dim rs,fieldVals,dbConnIn 
Dim connectString,shell,tmp,fso,ts,line 

Const adOpenDynamic=2 
Const adLockPessimistic=2 
Const adCmdTable=2 
Const adOpenForwardOnly=0 
Const adOpenStatic=3 
Const adLockReadOnly=1 

connectString="Provider=SQLOLEDB;Server=192.168.168.4;Database=MYDB;Uid=sa;Pwd=myPassword;" 
Set dBConnIn = CreateObject("ADODB.Connection") 
dBConnIn.CommandTimeout = 300 
dBConnIn.Open connectString 
' This is just a simple way of getting a record set from and SQL Query 
Set rs=CreateObject("ADODB.RecordSet") 
rs.Open _ 
    "TITULARES", _ 
    dbConnIn, _ 
    adOpenStatic, _ 
    adLockReadOnly, _ 
    adCmdTable 

Set shell=CreateObject("WScript.Shell") 
Set fso=CreateObject("Scripting.FileSystemObject") 
Set ts=fso.OpenTextFile("E:\TITULARES.csv",2,TRUE) 
line="" 
For Each tmp In rs.Fields 
    line=line & tmp.Name & "," 
Next 
ts.WriteLine Left(line,Len(line)-1) 
While Not rs.EOF 
line="" 
For Each tmp In rs.Fields 
    line=line & """" & Replace(tmp.Value,"""","""""") & """," 
Next 
ts.WriteLine Left(line,Len(line)-1) 
rs.MoveNext 
Wend 

rs.Close 
ts.close 

所有的好,而是因爲我的一些SQL表中的字段是NULL或空我得到這個錯誤:

d.vbs(39, 2) Microsoft VBScript runtime error: Invalid procedure call or argument: 'Left' 

任何想法如何解決這個問題?

謝謝!

BTW ...,使工作是這樣的:

Set dBConnIn = CreateObject("ADODB.Connection") 
dBConnIn.CommandTimeout = 300 
dBConnIn.Open connectString 
' This is just a simple way of getting a record set from and SQL Query 
Set rs=CreateObject("ADODB.RecordSet") 
rs.Open _ 
    "TITULARES", _ 
    dbConnIn, _ 
    adOpenStatic, _ 
    adLockReadOnly, _ 
    adCmdTable 

Set shell=CreateObject("WScript.Shell") 
Set fso=CreateObject("Scripting.FileSystemObject") 
Set ts=fso.OpenTextFile("E:\TITULARES.csv",2,TRUE) 
line="" 
For Each tmp In rs.Fields 
    line=line & tmp.Name & "," 
Next 
ts.WriteLine Left(line,Len(line)-1) 
While Not rs.EOF 
line="" 
For Each tmp In rs.Fields 
    If IsNull(tmp.Value) Then 
    line=line & """" & Replace(tmp.Value,"""","""""") & """," 
    Else 
    line=line & """" & tmp.Value & """," 
    End If 
Next 
ts.WriteLine Left(line,Len(line)-1) 
rs.MoveNext 
Wend 

rs.Close 
ts.close 

謝謝大家!

+0

對不起,忘了提,我得到這個錯誤也:d.vbs(36,3)Microsoft VBScript運行時錯誤:使用無效:'Replace'for line:line = line&「」「」&Replace(tmp.Value,「」「」「」「」「」)&「」「,」 – bsteo 2012-03-26 08:12:15

回答

0

有更好的方法將SQL Server表導出到.CSV:請參閱herehere; 。).GetString()認爲SELECT/INSERT INTO的」,.GetRows(或

與您的代碼影響最小的一個解決方案是使用類似功能:

Function SaveValue(oFld) 
    If IsNull(oFld.Value) Then 
    SaveValue = "" ' or whatever you like 
    Else 
    SaveValue = oFld.Value 
    End If 
End Function 
+0

我現在會測試它,回來,謝謝 – bsteo 2012-03-26 11:34:21

+0

關於他們在這種情況下不可用的其他方式,我只有ASP.NET訪問我的服務器上,所以沒有「sqlcmd」或「bcp」(我知道如何使用) – bsteo 2012-03-26 11:39:55

+0

我想我可以但我必須測試它): For Each tmp In rs.Fields If IsNull(tmp.Value)Then line = line&「」「」&Replace(tmp.Value,「」「」「」「 「」「)&」「」, Else line = line&「」「」&tmp.Value&「」「,」 End If Next – bsteo 2012-03-26 13:15:00

1

謝謝你,幫我出有類似的要求,所以我分享我做了什麼

我做了一些修改代碼:

  1. 使用VB.NET - 看起來像你的代碼是VB6,但你可能無論如何發現這很有用。
  2. 解決了空值的問題。
  3. 插入選項以包含列名稱。
  4. 使引號僅對文本列有條件。你可以注意到IF或修改條件。
Imports System.StringComparison 
Imports Microsoft.VisualBasic.Strings 

Private Function ExportToCSV(ByVal stTable As String, ByVal stFile As String, ByVal bColumnNames As Boolean) 

    Dim shell, tmp, fso, ts, line 

    Dim Conn As ADODB.Connection = New ADODB.Connection 
    Dim RS As ADODB.Recordset = New ADODB.Recordset 
    Dim connectstring As String = My.Settings.connSQL 'your connection string here 
    Conn.ConnectionString = My.Settings.connSQL 
    Conn.Open() 
    RS.ActiveConnection = Conn 
    RS.Open("Select * from " & stTable, Conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic) 

    shell = CreateObject("WScript.Shell") 
    fso = CreateObject("Scripting.FileSystemObject") 
    ts = fso.OpenTextFile(stFile, 2, True) 
    line = "" 
    If bColumnNames = True Then 
     For Each tmp In RS.Fields 
      line = line & tmp.Name & "," 
     Next 
     ts.WriteLine(Microsoft.VisualBasic.Strings.Left(line, Len(line) - 1)) 
    End If 

    While Not RS.EOF 
     line = "" 
     For Each tmp In RS.Fields 
      If IsDBNull(tmp.Value) Then 
       line = line & "," 
      Else 
       If RS.Fields(tmp.name).Type = ADODB.DataTypeEnum.adVarChar Or RS.Fields(tmp.name).Type = ADODB.DataTypeEnum.adChar Or RS.Fields(tmp.name).Type = ADODB.DataTypeEnum.adVarWChar Or RS.Fields(tmp.name).Type = ADODB.DataTypeEnum.adWChar Then 
        line = line & """" & tmp.Value & """," 
       Else 
        line = line & tmp.value & "," 
       End If 
      End If 
     Next 
     ts.WriteLine(Microsoft.VisualBasic.Strings.Left(line, Len(line) - 1)) 
     RS.MoveNext() 
    End While 

    RS.Close() 
    ts.close() 
End Function 
0

一個小的輔助函數來轉義特殊字符

'http://en.wikipedia.org/wiki/Comma-separated_values#Basic_rules 
Public Function EscapeCsvData(ByVal data As String) As String 
    If (data.Contains("")) Then 
     data = data.Replace("""", """""") 
    End If 

    If (data.Contains(",")) Then 
     data = String.Format("""{0}""", data) 
    End If 

    If (data.Contains(System.Environment.NewLine)) Then 
     data = String.Format("""{0}""", data) 
    End If 

    Return data 
End Function