Solved

save / reload datatable with sql server 2005 xml datatype

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 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