?
Solved

xml string, Linq and varbinary(max)

Posted on 2010-08-30
11
Medium Priority
?
1,492 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
Independent Software Vendors: 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 1500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

800 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