所以我有一种情况需要将一堆数据传递给服务器来创建一堆记录。所以我读了this article并选择了XML方法。所以我已经在sql server 2005中创建了所有存储过程。但是现在我需要从asp.net代码中调用它。只有我所拥有的数据库连接才是odbcconnection。我看到sqlclient似乎有一个原生的sqldbtype.xml。如果我使用odbc怎么办?我是SOL吗?
答案 0 :(得分:2)
我认为你应该使用SQLConnection&相关课程。
话虽如此,您可以使用VS.net Server Explorer,使用ODBC建立与SQL Server的连接,使用存储过程展开树并使用XML参数查看存储过程。
当您在XML参数上右键单击属性时,它应该显示它的ODBC数据类型。
这完全基于我的记忆。我认为,这应该有用。
编辑:看看link是否有帮助。 请参阅此链接上的DataTypeCompatibility字。
答案 1 :(得分:2)
结果我可以使用隐式varchar / ntext进行XML转换。我尝试了Varchar和NText - 它们都有效。另一种方式的转换不支持隐式转换,您必须根据文档进行显式转换。无论如何,这是一个示例函数和它正在调用的存储过程。
ADO.Net代码
Private Sub cmdXMLTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdXMLTest.Click
Dim objConnection As System.Data.Odbc.OdbcConnection
Dim cmdTest As System.Data.Odbc.OdbcCommand
Dim drTest As System.Data.Odbc.OdbcDataReader
Dim intCount As System.Int32
Dim strMatter As System.String = ""
Dim strXML As System.String = "<Root><Matter mmatter=""11415.09951"" /><Matter mmatter=""06625.06771"" /><Matter mmatter=""00931.09846"" /></Root>"
' build odbc command for getting parties
cmdTest = New System.Data.Odbc.OdbcCommand
cmdTest.CommandType = CommandType.StoredProcedure
cmdTest.CommandTimeout = 60
cmdTest.CommandText = "{CALL uspXMLTest (?)}"
cmdTest.Parameters.Add("@list", System.Data.Odbc.OdbcType.NText).Value = strXML
objConnection = New System.Data.Odbc.OdbcConnection("Driver={SQL Server};Server=mysqlserver;Database=son_db;Uid=sa;Pwd=mypassword;")
objConnection.Open()
cmdTest.Connection = objConnection
drTest = cmdTest.ExecuteReader(CommandBehavior.CloseConnection)
Do While drTest.Read()
strMatter = drTest.GetString(0)
intCount = intCount + 1
Loop
MsgBox("Found " & intCount.ToString() & " Matters - Last One = " & strMatter)
End Sub
存储过程
CREATE PROCEDURE uspXMLTest
@list xml
AS
SET NOCOUNT ON
SELECT matter.mmatter, matter.mdesc1
FROM matter
INNER JOIN @list.nodes('/Root/Matter') AS ml(mmatter) ON (matter.mmatter = ml.mmatter.value('@mmatter', 'varchar(15)'))
GO
答案 2 :(得分:0)
对于那些使用XMLTextWriter创建XML的人,您需要使用UTF-16编码(.net中的Unicode)。所以这是创建XML的代码。
' create xml file for folders
strmFolderList = New System.IO.MemoryStream()
wrtFolderList = New System.Xml.XmlTextWriter(strmFolderList, System.Text.Encoding.Unicode)
' start document and add root element
wrtFolderList.WriteStartDocument()
wrtFolderList.WriteStartElement("Root")
' cycle through folders
For intIndex = 0 To m_intAdditionalFolderQuantity - 1
' figure out folder description
strFolderDesc = m_arrFolderDesc(intIndex)
' add element to xml
wrtFolderList.WriteStartElement("Folder")
wrtFolderList.WriteAttributeString("folderdesc", strFolderDesc)
wrtFolderList.WriteAttributeString("insertfolder", ((intIndex + 1) * -1).ToString())
wrtFolderList.WriteEndElement()
Next
' close root element and document
wrtFolderList.WriteEndElement()
wrtFolderList.WriteEndDocument()
wrtFolderList.Close()
'System.Text.Encoding.Unicode.GetString(strmFolderList.ToArray())
这是调用存储过程的代码
cmdAddRequest = New System.Data.Odbc.OdbcCommand
cmdAddRequest.CommandType = CommandType.StoredProcedure
cmdAddRequest.CommandTimeout = 60
cmdAddRequest.CommandText = "{CALL uspAddRequest ( ?, ?, ?, ?, ?, ?, ?, ?, ?)}"
' add parameters to odbc command
cmdAddRequest.Parameters.Add("@ruserid", OdbcType.VarChar, 8).Value = SafeODBCParamString(m_strUID)
cmdAddRequest.Parameters.Add("@rmatter", OdbcType.VarChar, 15).Value = SafeODBCParamString(m_strMatterNumber)
cmdAddRequest.Parameters.Add("@req_tkinit", OdbcType.VarChar, 8).Value = SafeODBCParamString(m_strRequesting)
cmdAddRequest.Parameters.Add("@ret_tkinit", OdbcType.VarChar, 8).Value = SafeODBCParamString(m_strReturnNumber)
cmdAddRequest.Parameters.Add("@requestor", OdbcType.VarChar, 20).Value = SafeODBCParamString(m_strRequestor)
cmdAddRequest.Parameters.Add("@labels_only", OdbcType.Bit).Value = m_blnLabelsOnly
cmdAddRequest.Parameters.Add("@folder_quantity", OdbcType.SmallInt).Value = m_intAdditionalFolderQuantity
cmdAddRequest.Parameters.Add("@folder_list", OdbcType.NText).Value = System.Text.Encoding.Unicode.GetString(strmFolderList.ToArray())
cmdAddRequest.Parameters.Add("@insert_list", OdbcType.NText).Value = System.Text.Encoding.Unicode.GetString(strmInsertList.ToArray())