Solved

xml string, Linq and varbinary(max)

Posted on 2010-08-30
11
1,461 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
  • 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
 
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
Free Trending Threat Insights Every Day

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.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

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

19 Experts available now in Live!

Get 1:1 Help Now