从api调用捕获xml并使用sqlxmlbulkload写入数据库表

时间:2014-04-22 16:34:16

标签: asp.net sql-server xml streamwriter

我想要捕获从api调用返回的xml,并使用SQLXMLBulkLoad将其直接写入SQLserver表。到目前为止,我能够捕获xml并将其写入文件,然后使用SQLXMLBulkLoad读取文件并将其加载到SQLServer中的表中。这有效,但首先保存到文件显然效率低下。该文档指出SQLXMLBulkLoad可以接受来自ADO流对象的输入,但我无法弄清楚如何使其工作。有人可以帮忙吗?道歉是答案太明显了!我到目前为止使用的代码是:

Imports System.Net
Imports System.Text
Imports System.IO

Partial Class _Default
Inherits System.Web.UI.Page



Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim strURL As String = "http://myapicall...."

    Dim strRequest As HttpWebRequest = HttpWebRequest.Create(strURL)
    Dim strResponse As HttpWebResponse = strRequest.GetResponse()
    strRequest.KeepAlive = True
    Dim objStreamReader As StreamReader = New StreamReader(strResponse.GetResponseStream())
    Dim strSourceCode As String = objStreamReader.ReadToEnd()

    Dim objStreamWriter As StreamWriter

    'Pass the file path and the file name to the StreamWriter constructor.
    objStreamWriter = New StreamWriter("myfile.xml")

    'Write a line of text.
    objStreamWriter.WriteLine(strSourceCode)

    'Close the file.
    objStreamWriter.Close()


    Dim objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkLoad.4.0")
    objBL.ConnectionString = "provider=SQLOLEDB.1;data source=c9laptop;database=msite2;uid=dnndev706conn;pwd=password"
    objBL.ErrorLogFile = "d:\c9webs\msite2\error.log"
    objBL.KeepIdentity = False

    objBL.Execute("myschema.xml", "myfile.xml")
    MsgBox("Completed file import!")
    objBL = Nothing
    'reader.Close()
    strResponse.Close()

End Sub

End Class

如何从api调用中直接将xml从SQLXMLBulkload接受的ADO对象中获取?谢谢你的帮助。

0 个答案:

没有答案