2017-04-15 51 views
1

我想插入使用存储过程从XML文件中的数据,存储过程如下:加载XML文件来存储过程

CREATE PROCEDURE xmlreadtest 
@xmldoc xml 
AS 
BEGIN 

    INSERT INTO Page (KeyId) 
    SELECT [Key].value('@Id[1]', 'VARCHAR (100)') 
    FROM @xmldoc.nodes('//Page/Key') AS TEMPTABLE([Key]) 

END 

和Visual Basic中调用的程序:

Function ModfiyData() 

    Dim xmldocM As New XmlDocument 
    xmldocM.Load("C:\20170326.66.xml") 
    Dim SQLComm As New SqlCommand 
    Dim dbconn As New SqlConnection(con) 
    dbconn.Open() 
    SQLComm.Connection = dbconn 
    SQLComm.CommandText = "xmlreadtest" 
    SQLComm.CommandType = CommandType.StoredProcedure 
    SQLComm.Parameters.AddWithValue("@xmldoc", xmldocM) 
    SQLComm.ExecuteNonQuery() 
    dbconn.Close() 

End Function 

当我运行该应用程序,它给错误:

No mapping exists from object type System.Xml.XmlDocument to a known managed provider native type.

任何想法我怎么能解决这个问题.. 我正在使用vb 2015和sql数据库文件。

+0

我会尝试将其恢复为varchar – McNets

回答

1

尝试铸造XmlDocumentSqlXML

Dim xmldocM As New XmlDocument 
xmldocM.Load("C:\20170326.66.xml") 

Dim sw as new StringWriter() 
Dim xw as new XmlTextWriter(sw) 
xmldocM.WriteTo(xw) 
Dim transactionXml as new StringReader(sw.ToString()) 
Dim xmlReader AS new XmlTextReader(transactionXml) 
Dim XmlParamValue as new SqlXml(xmlReader) 

Dim SQLComm As New SqlCommand 
Dim dbconn As New SqlConnection(con) 
dbconn.Open() 
SQLComm.Connection = dbconn 
SQLComm.CommandText = "xmlreadtest" 
SQLComm.CommandType = CommandType.StoredProcedure 
SQLComm.Parameters.AddWithValue("@xmldoc", XmlParamValue) 
SQLComm.ExecuteNonQuery() 
dbconn.Close() 
+1

是它的工作..谢谢。 –

+0

你的问题很好。我把它提高了。祝你好运 – Hadi