Solved

Inserting XML into SQL Server Table

Posted on 2014-04-09
4
254 Views
Last Modified: 2014-04-11
I have a function as below however I now need to write a similar one that inserts the XML into a SQL server 2008 table.  How is this achieved?


  Public Shared Function WriteXMLToDisk(ByVal Despatches As Generic.List(Of XMLDespatch), ByVal FilePath As String) As Boolean
        'XElement xmlCustomers = new XElement(" customers", from c in customers where c.Country = = Countries.Italy select new XElement(" customer", new XElement(" name", c.Name), new XElement(" city", c.City), new XElement(" country", c.Country)));

        Try
            Dim XMLDespatches As XElement = New XElement("Despatches", From d In Despatches _
                                                                             Select New XElement("Despatch", _
                                                                             New XAttribute("LongCode", d.LongCode), _
                                                                             New XAttribute("Memo", d.Memo), _
                                                                             New XAttribute("OtherBranch", d.OtherBranch), _
                                                                             New XAttribute("TransactionDate", d.TransactionDate), _
                                                                             New XAttribute("TransactionType", d.TransactionType), _
                                                                             New XAttribute("Units", d.Units.ToString)))

            If Not Directory.Exists(CommonAttributes.DespatchesPath) Then
                Directory.CreateDirectory(CommonAttributes.DespatchesPath)
            End If

            If File.Exists(FilePath) Then
                File.Delete(FilePath)
            End If

            XMLDespatches.Save(FilePath, SaveOptions.None)
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

Open in new window

0
Comment
Question by:Alyanto
  • 2
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39989272
can you please clarify what exactly the issue is?
to save "xml" into a database, you need the following scripts:
* build the XML (same as above, except that you don't .Save to a file, but I presume the object has a property returning the full xml), so no issue here?
* connect to the database, which has the table with the xml column
* performing the INSERT / UPDATE using the above xml string
0
 
LVL 1

Author Comment

by:Alyanto
ID: 39990264
So what you are saying is that it is no diferent to anyother database transaction.

Declare  a SQLConnection, Declare a SQLSQLCommand etc... and then run an insert query or Stored Proc.  Nothing special, no specific way of couching the parameters etc?
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39990726
exactly. xml is for a database, behind the hoods, nothing else than text, following some rules about the tags, but for your code, still is just a plain text string.
0
 
LVL 1

Author Closing Comment

by:Alyanto
ID: 39993807
Cheers, soled the problem for me
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now