Advertisement

09.15.2007 at 02:44PM PDT, ID: 22831189
[x]
Attachment Details

Saving data to xml fields with C# and SQL Server 2005 Database

Asked by andrepires in SQL Server 2005, Extensible Markup Language (XML), C# Programming Language

Tags: xml, sql, data, field, save

Hello Experts!
Suppose I have a table in my sql server 2005 database that contains an xml field (using the xml data type).
Now suppose that I use the following code to read data from that field:

protected bool LoadXml(SqlConnection cn, XmlDocument doc)
{
        //Reading the xml from the database
        string sql =  @"SELECT Id, XmlField  FROM TABLE_WITH_XML_FIELD WHERE Id = @Id";
        SqlCommand cm = new SqlCommand(sql, cn);
        cm.Parameters.Add(new SqlParameter("@Id",1));
        using (SqlDataReader dr = cm.ExecuteReader())
        {
                 if (dr.Read())
                {
                          SqlXml MyXml= dr.GetSqlXml(dr.GetOrdinal("XmlField"));
                          doc.LoadXml( MyXml.Value);
                          return true;
                }
                else
                {
                          return false;
                }
         }
 }

Ok... I can read it.
My question is: How can I save data to that xml field?

Regards,

AndreStart Free Trial
 
 
[+][-]09.15.2007 at 07:55PM PDT, ID: 19899348

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Server 2005, Extensible Markup Language (XML), C# Programming Language
Tags: xml, sql, data, field, save
Sign Up Now!
Solution Provided By: Rejojohny
Participating Experts: 1
Solution Grade: A
 
 
[+][-]09.16.2007 at 05:09AM PDT, ID: 19900259

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_1_20070628