Showing posts with label Serialization. Show all posts
Showing posts with label Serialization. Show all posts

20 August 2010

How to change the identity value of a table

To change the identity value of a table use the statement:

DBCC CHECKIDENT (myTable, reseed, value)


Where
- myTable is the table to change the identity value
- reseed specifies that the identity value should be changed
- value is the new value to use for the identity column

Example:
DBCC CHECKIDENT (Products, reseed, 12)

After this command executed, the next identity value of the Products table will be 13

This command is useful if you delete records at the end of a table and want to keep the identity values sequential.

14 June 2010

Data Contract Serializer

The DataContractSerializer class serializes and deserializes an instance of a type stream or document using a supplied data contract.
The class to be serialized must be marked with the DataContractAttribute attribute.
Properties and fields of the class that are to be serialized must be marked with the DataMemberAttribute.

The DataContractSerializer WriteObject and ReadObject can then be used for serialization / deserialization.

Example:
1) Serialization

MyType myType = new MyType();
DataContractSerializer dcs = new DataContractSerializer(typeof(MyType));
MemoryStream ms = new MemoryStream();
dcs.WriteObject(ms, myType);

string result = Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Position);

2) Deserialization


MyType myType = null;

DataContractSerializer dcs = new DataContractSerializer(typeof(MyType));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(EncodedString));
myType = (MyType)dcs.ReadObject(ms);


Where the EncodedString is the encoded string to decode (result from step 1)