Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing an object in SQL Server - Plz Help

Status
Not open for further replies.

Smeat

Programmer
Joined
Mar 27, 2004
Messages
193
Location
GB
Hi All

Appologise for the blunt title.

I'm trying to store a c# object in a SQL table, can anybody tell me which data type I must use to do this.

Thanks in advance for any help

Smeat
 
You'll need to be more specific. What do you mean by an object? Is it code that you are trying to store in the SQL Server, a compilled EXE, etc?

Denny

--Anything is possible. All it takes is a little research. (Me)

[noevil]
(My very old site)
 
Hi

To add some contaxt to my question:

I am writing a session and authentication framework for our .NET web apps as the build in stuff doesn't meet our needs.

What I am attempting to do is serialize objects (c# classes) and save them into the database so that they may be retrieved from another web page.

I originally looked for the blob data types but it doesnt appear in sql 2k so am currently looking at the 'text' data type.

Is there a more appropriate data type available for an object of a potentially large size.

Many Thanks

Smeat
 
What you are trying to do is "Object to Relational Mapping" and there are C# classes and frameworks for doing this.

Do a google search of: Object Relational Mapping.

TJR
 
HI TJRTech

Thanks for your thoughts though what I am trying to is not related to OR Mapping, it is a custom session and authentication framework for all of our future applications.

My problem is that I am not sure which data type is best suited to storing a serialized object (self contained class written in c#) within SQL Server.

Regards

Smeat
 
If it is plain text then the "text" data type will be your best choice. If it's unicode text then the "ntext" data type. If it's binary data you'll want to use the "image" data type.

Denny

--Anything is possible. All it takes is a little research. (Me)

[noevil]
(My very old site)
 
Smeat, the reason I recommend OR mapping is because typically you don't want to serialize an object to a database. If you really want to serialize an object, serialize it to XML, which is possible with C# and the .NET framework and store the XML on disk (in a file). Of course, you could serialize to XML and store the text in an large VARCHAR string, but I wouldn't recommend that because you have an 8K max in SQL Server.

The reason I recommended OR mapping is because often you need to access values of properties in the SQL database in an adhoc query format, and to do that, you need to map from objects and property values to relational tables and columns.

Hope that helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top