无法将ntext数据从记录集中写入XML文件

时间:2019-03-28 15:06:39

标签: sql-server vbscript ado

我正在尝试从VBScript运行SQL查询,获取数据并将其写入XML文件。我有一个表,其中包含4列,其中包含ntext数据。我正在单独运行此查询:

SELECT CONVERT(xml, column_name) 
FROM table 
WHERE <condition>

当我在数据库上单独运行它时,它没有错误,并以正确的XML格式返回了值。

现在,当我从VBScript运行它时,出现此错误:

  

C:\ Users \ nipaul \ Desktop \ New folder \ ExportRuleSQLQuery.vbs(37,2)
  Microsoft VBScript运行时错误:类型不匹配:'obj2.WriteLine'

这似乎是我试图将记录集写入文件的那一行。

代码如下:

Dim Arg, DS_var, UID_var, PWD_var, IC_var, FriendlyRuleName
Dim rstResult
Set Arg = WScript.Arguments

DS_var  = Arg(0)
UID_var = Arg(1)
PWD_var = Arg(2)
IC_var  = Arg(3)
FriendlyRuleName = Arg(4)

Set obj  = CreateObject("ADODB.Connection")
Set obj1 = CreateObject("ADODB.RecordSet")
Set obj2 = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Users\nipaul\Desktop\filename.xml", 2, True)

Dim dbquery
Dbquery = "SELECT CONVERT(xml, column) FROM table WHERE FriendlyName='" & FriendlyRuleName & "'"

obj.Open "Provider=SQLNCLI;Data Source=" & DS_var & ";UserId=" & UID_var & ";Password=" & PWD_var & ";Initial Catalog= " & IC_var & ";Trusted_Connection=yes;DataTypeCompatibility=80"

obj1.Open dbquery, obj

'While obj1.EOF = False
'    obj2.Writeline obj1
'    obj1.MoveNext
'Wend
'obj1.Save "C:\Users\nipaul\Desktop\Rule.xml", adPersistXML
Do Until obj1.EOF
    rstResult = obj1
    obj2.WriteLine rstResult
    rstUsers.MoveNext
Loop
obj2.Close
Set obj2 = Nothing
obj1.Close
obj.Close
Set obj1 = Nothing
Set obj  = Nothing
Set Arg  = Nothing

我想要该XML文件中查询的输出。

<note> 
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

1 个答案:

答案 0 :(得分:0)

我会尝试明确地说出您要写入文件的字段

rstResult = obj1.Fields(0).Value ' or rstResult = obj1(0) might work because of default properties

删除对XML的转换也可能值得,因为最后您要做的就是将文本写入文本文件。