Solved

save / reload datatable with sql server 2005 xml datatype

Posted on 2006-11-26
2
431 Views
Last Modified: 2008-02-01
I'm trying to save a datatable into a sql 2005 xml field and then reload it later into a datatable.

Can't seem to find a method that works.

Here is my code to save:
***********************************************************
        Dim StringWriter As New System.IO.StringWriter
        datatable.WriteXml(StringWriter)
        Dim conn As New SqlConnection(ShortCuts.ConnectionString)
        Dim cmd As New SqlCommand("sSaveSavedStoredGroup", conn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@Name", saveName)
        cmd.Parameters.AddWithValue("@data", StringWriter.ToString())


It produces the following info in sql:
***********************************************************
<DocumentElement>
  <Table1>
    <idAccount>2351</idAccount>
    <sessionName>Adventure Camp</sessionName>
    <name>ashley, joelle</name>
    <idSession>3949</idSession>
    <age>56</age>
    <gender>Female</gender>
    <idSessionType>1</idSessionType>
    <idSessionAssignment>110481</idSessionAssignment>
    <orderDate>2006-09-03T16:35:41.707-04:00</orderDate>
    <confirmed>true</confirmed>
    <Cabin>?</Cabin>
    <madeBy>Staff</madeBy>
    <idPerson>96033</idPerson>
    <sessionBalance>200.00</sessionBalance>
    <arrived>false</arrived>
  </Table1>
  <Table1>
    <idAccount>42</idAccount>
    <sessionName>Adventure Camp</sessionName>
    <name>AshleyAdded, Daniel</name>
    <idSession>3949</idSession>
    <age>11</age>
    <gender>Male</gender>
    <idSessionType>1</idSessionType>
    <idSessionAssignment>110573</idSessionAssignment>
    <orderDate>2006-11-10T14:19:16.24-05:00</orderDate>
    <confirmed>true</confirmed>
    <Cabin>?</Cabin>
    <madeBy>Staff</madeBy>
    <idPerson>90037</idPerson>
    <sessionBalance>110.00</sessionBalance>
    <arrived>false</arrived>
  </Table1>
  <Table1>
    <idAccount>4711</idAccount>
    <sessionName>Adventure Camp</sessionName>
    <name>test, test</name>
    <idSession>3949</idSession>
    <age>5</age>
    <gender>Male</gender>
    <idSessionType>1</idSessionType>
    <idSessionAssignment>110567</idSessionAssignment>
    <orderDate>2006-10-18T23:17:32.14-04:00</orderDate>
    <confirmed>false</confirmed>
    <Cabin>?</Cabin>
    <madeBy>Staff</madeBy>
    <idPerson>255104</idPerson>
    <sessionBalance>200.00</sessionBalance>
    <arrived>false</arrived>
  </Table1>
</DocumentElement>

Here is the code I'm using to load
******************************************************
        Dim conn As New SqlConnection(ShortCuts.ConnectionString)
        Dim cmd As New SqlCommand("SELECT data FROM tbSavedStoredGroups WHERE idSavedStoredGroup = @idSavedStoredGroup", conn)
        cmd.Parameters.AddWithValue("@idSavedStoredGroup", Me.ddlGroups.SelectedValue)
        conn.Open()
        Dim dt As New DataTable
        dt.Load(cmd.ExecuteReader)

******************************************

When I load it back it returns a one column - and one row with all the data running together sperated by lots of diferent charaters.

What am I doing wrong?

Thanks,
Daniel
0
Comment
Question by:dbashley1
2 Comments
 
LVL 10

Accepted Solution

by:
gangwisch earned 500 total points
ID: 18015102
you need to have an xslt file on how to transform the data that you receive
0
 

Author Comment

by:dbashley1
ID: 18020646
gangwisch,

Thanks you pointed me in the right direction.

Here is the working code to save:
*******************************************
        Dim StringWriter As New System.IO.StringWriter
        datatable.WriteXml(StringWriter, XmlWriteMode.WriteSchema)
        Dim conn As New SqlConnection(ShortCuts.ConnectionString)
        Dim cmd As New SqlCommand("sSaveSavedStoredGroup", conn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@Name", saveName)
        cmd.Parameters.AddWithValue("@data", StringWriter.ToString())


Here is the working code to load:
*******************************************
        Dim conn As New SqlConnection(ShortCuts.ConnectionString)
        Dim cmd As New SqlCommand("SELECT data FROM tbSavedStoredGroups WHERE idSavedStoredGroup = @idSavedStoredGroup", conn)
        cmd.Parameters.AddWithValue("@idSavedStoredGroup", Me.ddlGroups.SelectedValue)
        conn.Open()
        Dim dt As New DataTable
        dt.ReadXml(cmd.ExecuteXmlReader)
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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

862 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

25 Experts available now in Live!

Get 1:1 Help Now