Solved

xml string, Linq and varbinary(max)

Posted on 2010-08-30
11
1,472 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

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