我儘量讓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
謝謝大家!
對不起,忘了提,我得到這個錯誤也:d.vbs(36,3)Microsoft VBScript運行時錯誤:使用無效:'Replace'for line:line = line&「」「」&Replace(tmp.Value,「」「」「」「」「」)&「」「,」 – bsteo 2012-03-26 08:12:15