Calling a Stored Procedure with XML Datatype

前端 未结 9 1114
旧巷少年郎
旧巷少年郎 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:56

    you can add parameter in more simpler way in this way we don't have to pass object type to parameter sql manages it as passed value

    SqlXml sqlXml = new SqlXml(xmlReader); 
    cmd.Parameters.AddWithValue("@FileContent"], strxml);
    
    0 讨论(0)
  • 2021-01-05 07:56

    Or, with the fewest lines of code, read your XmlDocument straight into an XmlNodeReader and use that to initialise you SqlXml parameter value:

    SqlXml sqlXml= new SqlXml(new XmlNodeReader(doc));
    cmd.Parameters.Add("@FileContent", sqlXml);
    
    

    Note that you don't need to add the parameter with the type, then set the value - if you pass a type which SqlParameter recognises (in this case a SqlXml object), the type will be inferred.

    0 讨论(0)
  • 2021-01-05 08:01

    In .NET Framework 4.5.2, I was able to pass a System.Xml.XmlDocument (variable name "xdoc") object using the following simple code:

    XmlTextReader xreader = new XmlTextReader(new StringReader(xdoc.OuterXml));
    cmd.Parameters.Add(new SqlParameter("@xmlOptions", new SqlXml(xreader)));
    
    0 讨论(0)
提交回复
热议问题