Coffeehouse Thread

4 posts

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. 

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.