Solved

save / reload datatable with sql server 2005 xml datatype

Posted on 2006-11-26
2
428 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

21 Experts available now in Live!

Get 1:1 Help Now