Calling a Stored Procedure with XML Datatype

2020-03-02 05:01发布

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 getting error: Failed to convert parameter value from a XmlDocument to a String. Below is code sample. How do you pass an XML Document to a store procedure that is expecting an XML datatype? Thanks.

        XmlDocument doc = new XmlDocument();
        //Load the the document with the last book node.
        XmlTextReader reader = new XmlTextReader(@"C:\temp\" + uploadFileName);
        reader.Read();
        // load reader 
        doc.Load(reader);

        connection.Open();

        SqlCommand cmd = new SqlCommand("UploadXMLDoc", connection);

        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add("@Year", SqlDbType.Int);
        cmd.Parameters["@Year"].Value = iYear;
        cmd.Parameters.Add("@Quarter", SqlDbType.Int);
        cmd.Parameters["@Quarter"].Value = iQuarter;
        cmd.Parameters.Add("@CompanyID", SqlDbType.Int);
        cmd.Parameters["@CompanyID"].Value = iOrganizationID;
        cmd.Parameters.Add("@FileType", SqlDbType.VarChar);
        cmd.Parameters["@FileType"].Value = "Replace";
        cmd.Parameters.Add("@FileContent", SqlDbType.Xml);
        cmd.Parameters["@FileContent"].Value = doc;
        cmd.Parameters.Add("@FileName", SqlDbType.VarChar);
        cmd.Parameters["@FileName"].Value = uploadFileName;
        cmd.Parameters.Add("@Description", SqlDbType.VarChar);
        cmd.Parameters["@Description"].Value = lblDocDesc.Text;
        cmd.Parameters.Add("@Success", SqlDbType.Bit);
        cmd.Parameters["@Success"].Value = false;
        cmd.Parameters.Add("@AddBy", SqlDbType.VarChar);
        cmd.Parameters["@AddBy"].Value = Page.User.Identity.Name;

        cmd.ExecuteNonQuery();
        connection.Close();

9条回答
三岁会撩人
2楼-- · 2020-03-02 05:09
public static string SerializeToXml(T obj)
{
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

    ns.Add("","");

    StringWriter Output = new StringWriter(new StringBuilder());
    XmlSerializer ser = new XmlSerializer(obj.GetType);
    ser.Serialize(Output, obj, ns);

    return Output.ToString();
}
查看更多
够拽才男人
3楼-- · 2020-03-02 05:12

you can create a XML string using following code

var doc = new XDocument();
doc.Add(new XElement("x", input.Select(x => new XElement("v", x))));
return doc.ToString();

and then pass this doc string to stored procedure as a parameter

查看更多
forever°为你锁心
4楼-- · 2020-03-02 05:12

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.

查看更多
Summer. ? 凉城
5楼-- · 2020-03-02 05:13

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.

查看更多
叼着烟拽天下
6楼-- · 2020-03-02 05:13

To do this with an XDocument, XElement or other XNode, try the following:

XDocument doc = new XDocument(
    new XElement("Person", 
        new XAttribute("Name", "John")));
cmd.Parameters.Add("@FileContent", SqlDbType.Xml);
cmd.Parameters["@FileContent"].Value = new SqlXml(doc.CreateReader());
查看更多
ら.Afraid
7楼-- · 2020-03-02 05:17

Other way to do it if you don't mind loosing the xml declaration (version and encoding) is just:

XML.DocumentElement.OuterXml 'where XML is a XMLDocument
查看更多
登录 后发表回答