Store C # object in SQL Server column
I have a class in C # that is used to store data (like a struct). It has subclasses as members, which have other subclasses, etc. Each of them contains data (various strings, ints, float, etc.)
Is there a way to store one such C # object in a database in a single column? What type of column would it be and how can I use it?
The only thing I can think of is to convert my structure to a sequence of bytes using an algorithm and store it in a binary type field.
I would use a decoding algorithm to put it back and make it an object again.
However, is there such an algorithm in C # that can be used?
source to share
see this quetion If you like, you can use the VARBINARY (MAX) field type in SQL Server. You can store any type of object up to 2 GB in size there.
To access it, you can use ADO.NET - something like this:
object yourMysteryObject = (whatever you like it to be);
MemoryStream memStream = new MemoryStream();
StreamWriter sw = new StreamWriter(memStm);
sw.Write(yourMysteryObject);
SqlCommand sqlCmd = new SqlCommand("INSERT INTO TableName(VarBinaryColumn) VALUES (@VarBinary)", sqlConnection);
sqlCmd.Parameters.Add("@VarBinary", SqlDbType.VarBinary, Int32.MaxValue);
sqlCmd.Parameters["@VarBinary"].Value = memStream.GetBuffer();
sqlCmd.ExecuteNonQuery();
source to share
You can serialize an object to JSON using JSON.NET ( http://json.codeplex.com/ ) and store the JSON data in a database. When you need to re-moisten an object with data, it can be done with JSON.NET again ...
The documentation is here and it's easy to use and very fast: http://james.newtonking.com/projects/json/help/
source to share