Coffeehouse Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Adding XML to SQL via Linq

Back to Forum: Coffeehouse
  • User profile image
    sqlerror

    Anyone know how?

    I can do it using SqlXml datatype without problems like so:

    SqlXml sx = new SqlXml(xnr);
                            insertCommand.CommandText = string.Format("INSERT INTO {0} (JIDFilename, JurisdictionName, JurXml) values('{1}', '{2}', @XmlContent)", table, cfgFile,
                                                                  jurName);
                            insertCommand.Parameters.Add("@XmlContent", SqlDbType.Xml);
                            insertCommand.Parameters["@XmlContent"].Value = sx;
    
                            int rowsAffected = insertCommand.ExecuteNonQuery();

     

    Doing it with LINQ isn't working for me however.

  • User profile image
    sqlerror

    Is the answer submitting a string which is implicitly converted to XML... 

     

    Well that seems to be working.

  • User profile image
    figuerres

    , sqlerror wrote

    Anyone know how?

    I can do it using SqlXml datatype without problems like so:

    1
    2
    3
    4
    5
    6
    7
    SqlXml sx = new SqlXml(xnr);
                            insertCommand.CommandText = string.Format("INSERT INTO {0} (JIDFilename, JurisdictionName, JurXml) values('{1}', '{2}', @XmlContent)", table, cfgFile,
                                                                  jurName);
                            insertCommand.Parameters.Add("@XmlContent", SqlDbType.Xml);
                            insertCommand.Parameters["@XmlContent"].Value = sx;
     
                            int rowsAffected = insertCommand.ExecuteNonQuery();

     

    Doing it with LINQ isn't working for me however.

    can you post an example of the kind of linq statement you are trying ?

    I do some stuff but I generally have the linq to sql or linq to ef using string on the c# side

    but the string is an xml doc.

  • User profile image
    sqlerror

     Used DataContext, similar to this. My model however had the XML variable be of type SqlXml, and Linq did not like this. 

     Another problem was updating. That was another can of worms with a simple fix. 

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.