An example of Object serialization to a database.

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. serialization;
Using system. runtime. serialization. formatters. Binary;
Using system. IO;
Using system. Data;
Using system. Data. sqlclient;

Namespace serializabletest
{
[Serializable]
Public class member {
Public string name = NULL;
Public int age = 0;
Public int Weight = 0;
}

Class Program
{
Static void main (string [] ARGs)
{
// Serializableobj ();
// Deserializableobj ();

// Testsaveimagetodb ();

// Member OBJ = new member ();
// Obj. Age = 20;
// Obj. Weight = 120;
// Obj. Name = "Jhon ";
// Serializabeobjtodb (OBJ );

Deserializabeobjtodb ();

}

Public static void testsaveimagetodb ()
{
String strfilename = @ "E: \ Documents ents and Settings \ jhtest \ My Documents ents \ my pictures \ 2.txt ";
Filestream PIC = new filestream (strfilename, filemode. Open); // sfilename is the image path
Byte [] picbyte = new byte [pic. Length];
PIC. Read (picbyte, 0, picbyte. Length );

Sqlconnection con = new sqlconnection ("database = jdplan; server = jhtest2; uid = sa; Pwd = jhtest123 ;");
String plain text = "insert into test (fileformat, imageformat) values (@ image, @ file )";
// String literal text = "delete from test ";
Sqlcommand cmd = new sqlcommand (plain text, con );
// Of course, binary values cannot be input in the form of string addition. They can only be input in the form of adding parameters.
Cmd. Parameters. Add ("@ image", sqldbtype. Image, convert. toint32 (PIC. Length). value = picbyte;
Cmd. Parameters. Add ("@ file", sqldbtype. Binary, convert. toint32 (PIC. Length). value = picbyte;

Cmd. commandtype = commandtype. text;
Con. open ();
Int returnvalue = cmd. executenonquery ();
Con. Close ();
If (returnvalue> 0)
{
Console. writeline ("insert sucess !!! ");
Console. Readline ();
}
}

Public static void serializabeobjtodb (Object OBJ)
{
Memorystream MS = new memorystream ();

MS = serializable. serializebinary (OBJ );

Byte [] mydata = new byte [Ms. Length];
Ms. Position = 0;
Ms. Read (mydata, 0, convert. toint32 (Ms. Length ));

Sqlconnection con = new sqlconnection ("database = jdplan; server = jhtest2; uid = sa; Pwd = jhtest123 ;");
String plain text = "insert into test (fileformat, imageformat) values (@ image, @ file )";
Sqlcommand cmd = new sqlcommand (plain text, con );
// Of course, binary values cannot be input in the form of string addition. They can only be input in the form of adding parameters.
Cmd. Parameters. Add ("@ image", sqldbtype. Image, convert. toint32 (mydata. Length). value = mydata;
Cmd. Parameters. Add ("@ file", sqldbtype. Binary, convert. toint32 (mydata. Length). value = mydata;

Cmd. commandtype = commandtype. text;
Con. open ();
Int returnvalue = cmd. executenonquery ();
Con. Close ();
If (returnvalue> 0)
{
Console. writeline ("insert sucess !!! ");
Console. Readline ();
}
}

Public static void deserializabeobjtodb ()
{
Sqlconnection con = new sqlconnection ("database = jdplan; server = jhtest2; uid = sa; Pwd = jhtest123 ;");
String plain text = "select top 1 fileformat from test ";
Sqlcommand cmd = new sqlcommand (plain text, con );

Cmd. commandtype = commandtype. text;
Con. open ();
Sqldatareader DR = cmd. executereader ();
Byte [] mydata = new byte [10000];
Dr. Read ();
Mydata = (byte []) DR. getvalue (0 );
Con. Close ();

Memorystream MS = new memorystream ();
Ms. Position = 0;
Ms. Write (mydata, 0, convert. toint32 (mydata. Length ));

Member OBJ = (member) serializable. deserializebinary (MS );

Console. writeline ("N1: {0}", obj. Name );
Console. writeline ("N2: {0}", obj. Age );
Console. writeline ("str: {0}", obj. weight );
Console. Readline ();
}

Public static void serializableobj ()
{
// The following is a simple code for executing serialization.
Member OBJ = new member ();
OBJ. Age = 20;
OBJ. Weight = 120;
OBJ. Name = "Jhon ";
Iformatter formatter = new binaryformatter ();
Stream stream = new filestream ("myfile. bin", filemode. Create, fileaccess. Write, fileshare. None );
Formatter. serialize (stream, OBJ );
Stream. Close ();
}
Public static void deserializableobj ()
{

// The following is the deserialization of the obtained file.
Iformatter formatter = new binaryformatter ();
Stream stream = new filestream ("myfile. bin", filemode. Open, fileaccess. Read, fileshare. Read );
Member OBJ = (member) formatter. deserialize (Stream );
Stream. Close ();

// Here's the proof
Console. writeline ("N1: {0}", obj. Name );
Console. writeline ("N2: {0}", obj. Age );
Console. writeline ("str: {0}", obj. weight );
Console. Readline ();
}
}

Public class serializable
{
# Region binary serializers
Public static system. Io. memorystream serializebinary (Object Request)
{
System. runtime. serialization. formatters. Binary. binaryformatter serializer =
New system. runtime. serialization. formatters. Binary. binaryformatter ();
System. Io. memorystream memstream = new system. Io. memorystream ();
Serializer. serialize (memstream, request );
Return memstream;
}

Public static object deserializebinary (system. Io. memorystream memstream)
{
Memstream. Position = 0;
System. runtime. serialization. formatters. Binary. binaryformatter deserializer =
New system. runtime. serialization. formatters. Binary. binaryformatter ();
Object newobj = deserializer. deserialize (memstream );
Memstream. Close ();
Return newobj;
}
# Endregion

// # Region XML serializers
// Public static system. Io. memorystream serializesoap (Object Request)
//{
// System. runtime. serialization. formatters. Soap. soapformatter serializer =
// New system. runtime. serialization. formatters. Soap. soapformatter ();
// System. Io. memorystream memstream = new system. Io. memorystream ();
// Serializer. serialize (memstream, request );
// Return memstream;
//}

// Public static object deserializesoap (system. Io. memorystream memstream)
//{
// Object SR;
// System. runtime. serialization. formatters. Soap. soapformatter deserializer =
// New system. runtime. serialization. formatters. Soap. soapformatter ();
// Memstream. Position = 0;
// Sr = deserializer. deserialize (memstream );
// Memstream. Close ();
// Return SR;
//}
// # Endregion

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.