我尝试制作一个VB脚本,将SQL服务器表转储为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 :(得分:1)
谢谢,这帮助我提出了类似的要求,所以我分享了我的所作所为。
我对您的代码进行了一些更改:
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
答案 1 :(得分:0)
有更好的方法可以将SQL Server表导出到.CSV:请参阅here或here;想想SELECT / INSERT INTO“,。GetRows()或.GetString()。
对代码影响最小的解决方案是使用以下函数:
Function SaveValue(oFld)
If IsNull(oFld.Value) Then
SaveValue = "" ' or whatever you like
Else
SaveValue = oFld.Value
End If
End Function
答案 2 :(得分: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