Calling a Stored Procedure with XML Datatype

前端 未结 9 1255
旧巷少年郎
旧巷少年郎 2021-01-05 07:06

I am simply trying to call a store procedure (SQL Server 2008) using C# and passing XMLDocument to a store procedure parameter that takes a SqlDbType.Xml data type. I am ge

9条回答
  •  暖寄归人
    2021-01-05 07:52

    You need to pass the xml as a string.

    But if you don't need the xml functions in the database, you might consider using varbinary to store the files.


    UPDATE!!!!!

    Thanks. I got it to work. Added the following coded:

    StringWriter sw = new StringWriter(); 
    XmlTextWriter xw = new XmlTextWriter(sw); 
    doc.WriteTo(xw); 
    StringReader transactionXml = new StringReader(sw.ToString()); 
    XmlTextReader xmlReader = new XmlTextReader(transactionXml); 
    SqlXml sqlXml = new SqlXml(xmlReader); 
    

    Converting it to a string was not enough. I got the following error: XML parsing: line 1, character 38, unable to switch the encoding”. So, I converted to string then coverted it to SqlXml and it worked.

提交回复
热议问题