Solved

xml string, Linq and varbinary(max)

Posted on 2010-08-30
11
1,487 Views
Last Modified: 2012-05-10
Hi

using vs2010 asp.net, vb.bet

I have  a string containing an xml document that I need to store in a sql server varbinary(max) column.

How do you:
a) Store the string using linq to sql
b) retrieve and load the string into an xmldocument object.

Thanks in advance

Wing

0
Comment
Question by:WingYip
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 33558659
Insert, Update, and Delete Operations (LINQ to SQL)
http://msdn.microsoft.com/en-us/library/bb386931.aspx

LINQ to SQL (Part 4 - Updating our Database)
http://weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx
0
 
LVL 1

Author Comment

by:WingYip
ID: 33558685
This stuff I know.  The issue is the varbinary datatype and how to store and retrieve.
Thanks
Wing
0
 
LVL 9

Expert Comment

by:puru1981
ID: 33559118
it doesn't matter if the datatype is varbinary or varchar. you will get what you have stored in the same format. one thing is changed. varchar is not case sensitive but varbinary is case sensitive.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:WingYip
ID: 33559328
I've answered my own question now.

I cant just put a string into a varbinary datatype, it needs to be converted to a byte array first and then inserted as you would normally insert linq data.

The question was about the whole operation of saving a string into a varbinary datatype - nobody seemed to latch on to that - thanks for replies anyway.

Wing

Wing.
0
 
LVL 9

Accepted Solution

by:
puru1981 earned 500 total points
ID: 33559475
you don't need to convert the string data to byte array before saving. it is tarnsparent from user.

i am giving you a sample

just create a table with two columns id int and data varbinary(50)

now use a insert command to insert data into the table

insert into tablename values(1,'junk data')
insert into tablename values(2,'junk data2')

now use a select query

select * from tablename

it will show the data inserted above.
0
 
LVL 1

Author Comment

by:WingYip
ID: 33559520
I've done it like this.  Is it unneseary?  What should I have done?

   'Store xml in db
        Dim xo As New bb_xml_order 'linq table
        xo.OrderHeaderId = ord.OrderHeaderId
        Dim enc As System.Text.Encoding = System.Text.Encoding.UTF8
        Dim barray As Byte() = enc.GetBytes(sXmlOrder)
        '''''''''Dim sRetrieve As String = enc.GetString(barray)  
        xo.xmlOrder = barray
        xo.filename = String.Format("{0}.xml", ord.GetXmlFileName)
        db.bb_xml_orders.InsertOnSubmit(xo)
        db.SubmitChanges()
0
 
LVL 9

Expert Comment

by:puru1981
ID: 33559606
there is no need to convert it in binary until you don't want to be unreadable data from the sql query.

it is an overhead of making as bytestream and re arranging to string.
0
 
LVL 1

Author Comment

by:WingYip
ID: 33559647
The overhead is a non issue in this case.
The string will not be queried, just retrieved.  If stored as varchar, I understand there are encoding difficulties as varchar is utf-16 and the xml string is utf-8.

Wing
0
 
LVL 9

Expert Comment

by:puru1981
ID: 33559689
then what you have done is correct.
0
 
LVL 1

Author Comment

by:WingYip
ID: 33559696
thanks for the concern.

You can have the points
0
 
LVL 1

Author Closing Comment

by:WingYip
ID: 33559733
My solution is the correct one but grateful for help of puru1981 so awarding points to him
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

729 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