如何从 SQL Server 2005 检索 XML 数据?

2024-01-26

我的脚本:

Dim myStream, myConnection, myCommand
Set myStream = CreateObject("ADODB.Stream")
Set myConnection = CreateObject("ADODB.Connection")
Set myCommand = CreateObject("ADODB.Command")
'
myConnection.Open "Provider=SQLOLEDB;Integrated Security=SSPI;" & _
"Persist Security Info=False;Initial Catalog=DSIPAR;Data Source=.\DSIDATA"

myCommand.ActiveConnection = myConnection

myCommand.CommandText = "SELECT itemsgt, item FROM NIFItem"

myStream.Open

myCommand.Properties("Output Stream") = myStream
myCommand.Execute , , adExecuteStream

myStream.Position = 0
myStream.Charset = "ISO-8859-1"

Dim strxml
strxml = myStream.ReadText
MsgBox (strxml)

我可以运行该脚本,并且可以看到查询在我的 SQL Server 实例上执行,但没有任何内容返回到输出流。


要将结果检索到流中,查询需要包含“FOR XML AUTO”。还将文本 adExecuteStream 更改为常量值 1024。

完整代码:

Dim myStream, myConnection, myCommand
Set myStream = CreateObject("ADODB.Stream")
Set myConnection = CreateObject("ADODB.Connection")
Set myCommand = CreateObject("ADODB.Command")
myConnection.Open "Provider=SQLOLEDB;Integrated Security=SSPI;" & _
"Persist Security Info=False;Initial Catalog=DSIPAR;Data Source=.\DSIDATA"
myCommand.ActiveConnection=myConnection

myCommand.CommandText="SELECT itemsgt,item FROM NIFItem FOR XML AUTO"

myStream.Open
myCommand.Properties("Output Stream") = myStream
myCommand.Properties("xml root") = "root"

myCommand.Execute ,,1024 'instead of adExecuteStream
myStream.Position=0
myStream.Charset="ISO-8859-1"
Dim strxml
strxml = myStream.ReadText
MsgBox (strxml)

如果您不需要流而是想要读取 itemsgt 和 item 的值,请尝试以下操作:

Dim myStream, myConnection, myCommand, adoRec
Set myStream = CreateObject("ADODB.Stream")
Set myConnection = CreateObject("ADODB.Connection")
Set myCommand = CreateObject("ADODB.Command")
myConnection.Open "Provider=SQLOLEDB;Integrated Security=SSPI;" & _
"Persist Security Info=False;Initial Catalog=DSIPAR;Data Source=.\DSIDATA"
myCommand.ActiveConnection=myConnection
myCommand.CommandText="SELECT itemsgt,item FROM NIFItem"
SET adoRec = myCommand.Execute()
'Get the first results
If Not adoRec.EOF then
  MsgBox "itemsgt = "  & adoRec(0) & vbcrlf & "item="  &  adoRec(1)
End If

'Iterate through the results
While Not adoRec.EOF
  itemsgt = adoRec(0)
  item = adoRec(1)
  'MsgBox "itemsgt = " & itemsgt & vbcrlf & "item="  & item
  adoRec.MoveNext
Wend
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 SQL Server 2005 检索 XML 数据? 的相关文章

  • SetTimeout() 不会执行该函数

    这是我的代码片段 in VBScript Sub Main Dim timeoutTimer more scripts here more scripts here more scripts here timeoutTimer window
  • 将字符串转换为 UTF-8

    例如 我有一个分配给编码为 ansi 的变量的字符串str 如何将其编码为 UTF 8 你的意思是当它写入文件时 像这样 Set stream CreateObject ADODB Stream stream Open stream Typ
  • 使用VBS脚本检查字符串是否包含特定字符

    我的脚本正在做以下几点 检索所有我选择的文件夹文件 按日期对它们进行分类 从最近的到较早的 在窗口中显示它们 这是我的 VBS 脚本 我检索它here http codes sources commentcamarche net sourc
  • 如何将 VBScript 转换为可执行 (EXE) 文件? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我四处寻找转换 VBScript 的信息 vbs 到可执行文件 并意识到大多数可用的工具实际上是将脚本包装在可执行文件中 尝试了一些工
  • 如何启动 VBS 的交互式控制台?

    与这个问题非常相似 如何启动 Perl 的交互式控制台 https stackoverflow com questions 73667 how can i start an interactive console for perl 我只是希
  • 使用 qtp 功能单击特定链接

    我想通过创建一个在操作中调用的函数来自动执行 Flipkart 的登录过程 Function Website this is the function Systemutil Run iexplore exe http www flipkar
  • 我对一些小概念感到困惑

    我对 VBscript 非常陌生 正在努力学习所有概念 在我的实践过程中 我一直有一个疑问 dim a b c set a CreateObject scripting filesystemobject initiate the file
  • 如何从 HTA 中启动批处理文件

    我有一个基本的 HTA 其中我尝试通过按钮启动批处理文件 但是当我尝试启动批处理文件时 它不会运行 当我按下按钮时 将启动命令提示符窗口 但不会运行脚本 这是代码的简单示例
  • VBScript:从 Scripting.Dictionary 中对项目进行排序

    我有下面的代码 它获取这样的数据 姓名 1 姓名 4 姓名 2 姓名 3 并像这样列出 是一个复选框 姓名 1 姓名 4 姓名 2 姓名 3
  • 为什么 vbscript Mid 函数显示错误“无效的过程调用或参数:Mid”

    您好 下面是我的 ASPClassic 代码 其中 csvData 是一个包含变量的非常大的字符串 现在在 Mid 函数调用的这一行中出现错误 无效的过程调用或参数 Mid 为什么会发生这种情况 Dim dataLen Dim fromLe
  • 根据用户名获取广告详细信息

    我有一个代码可以从 AD 检索用户的详细信息 例如电子邮件地址 电话号码等 我当前使用的代码是 Set objSysInfo CreateObject ADSystemInfo strUser objSysInfo UserName msg
  • 在 C# 中转换 VbScript 函数(Right、Len、IsNumeric、CInt)

    同样 我在 VbScript 中得到了以下代码 您能建议一下 C 中的等效代码吗 Function GetNavID Title getNavID UCase Left Title InStr Title 1 End Function 我已
  • 如何使用 VBScript 读取 .zip 文件的内容而不实际解压文件?

    我有一个以父目录开头的 zip 文件 我需要从文件中读取该目录 然后搜索我的硬盘以查看该目录名称是否已存在 如果存在 我会将其删除并替换为 zip 文件的内容 所有这些我都可以做 除了读取 zip 而不实际解压缩文件 zip 文件的大小可以
  • GetRef 来捕获方法?

    我刚刚发现了 VBScript 的 GetRef 函数 它获取对其参数命名的函数的引用 有没有办法以这种方式获取对方法的引用 我有一种预感 VBScript 不提供这样做所需的复杂绑定 但它肯定会很好 No GetRef不支持类方法
  • 如何在VBS中使用Excel内置常量[重复]

    这个问题在这里已经有答案了 我想使用VBScript而不是VBA来处理EXCEL 但是我在调 用Range Find方法时遇到了代码错误 见下文 Function find range wb domain var Dim sheet Dim
  • VBScript:函数返回一个数组

    我有一些 VBScript 代码 其中函数返回一个数组 function PreProcessFile sFile dim deData 3 populate deData with strings PreProcessFile deDat
  • 如何在 VBScript 中计算每月的最后一个工作日

    如何在 VBScript 中计算该月的最后一个工作日 它用于 Reporting Services 报告 Thanks 怎么样 intMonth 11 Use zero to return last day of previous mont
  • 导出到excel时如何显示前导零?

    我正在通过更改内容类型来创建 Excel 报告 Response ContentType application vnd ms excel 我有包含前导零的值 问题是导出到 Excel 时缺少前导零 e g 000123 gt 123 我知
  • 比较数组中的文件、从文本文件中删除行、函数、日志记录

    所以我创建了这两个数组 Approved Shares 和 Current Shares Reads Approvedshare txt and makes the txt file into an array public objFSO
  • VBS 与 PowerShell:哪个更轻?

    如果我需要一个可以在系统中以最少的努力执行的脚本 我会选择哪个 通过查看任务管理器中的进程 内存 私有工作集 wscript exe 2 068Kpowershell exe 33 144K Thanks 这里存在工作守恒定律 如果机器做的

随机推荐