Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

12 October 2014

XML Serialization and Deserialization using generics

I love generics, and I when I come across a requirement where I had  to serialize a variety of objects to xml and deserialize back to the object, generics was the way to go, and resulted in following methods.

        // De-Serializes the request into type object
        public static XmlDocument SerializeToXml(T objectToSerialize)
        {
            XmlDocument serializedXml = new XmlDocument();
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

            using (MemoryStream momoryStream = new MemoryStream())
            {
                XmlTextWriter xmlTextWriter = new XmlTextWriter(momoryStream, null);

                xmlSerializer.Serialize(xmlTextWriter, objectToSerialize);
                // Rewind the Stream.
                momoryStream.Seek(0, SeekOrigin.Begin);
                // load from stream;
                serializedXml.Load(momoryStream);

            }
            return serializedXml;
        }


        // De-Serializes the request into class object
        public static T DeserializeXml(XmlNode xmlToDesearialized)
        {
            if (xmlToDesearialized == null) throw new ArgumentNullException("xmlToDesearialized");
            T deserializedObject = default(T);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

            using (StringReader stringReader = new StringReader(xmlToDesearialized.OuterXml))
            {
                XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
                deserializedObject = (T)xmlSerializer.Deserialize(xmlTextReader);
            }
            return deserializedObject;
        }

24 December 2009

How to Compact Access Database Programmatically

Add a reference of “Microsoft Jet and Replication Objects 2.6 Library” in your project.



Now write the following function to compact the Access database with .mdb extension.

C#

private void CompactDatabase()
{

   //Making object of the JRO.JetEngine
   JRO.JetEngine objJetEngine = new JRO.JetEngine();

   //Specifying the connection strings for the Source and Target databases

   string SourceDBConnectionString = "Data Source=\"" + "SourceDBPath" + "\";Jet OLEDB:Database Password=" + "Password";

   string TargetDBConnectionString = "Data Source=\"" + "destFilePath" + "\";Jet OLEDB:Database Password=" + "Password";

   //Calling the CompactDatabase function of the JRO.JetEngine object
   objJetEngine.CompactDatabase(SourceDBConnectionString, TargetDBConnectionString);

}

VB.Net

Private Sub CompactDatabase()
   'Making object of the JRO.JetEngine
   Dim objJetEngine As New JRO.JetEngine()

   'Specifying the connection strings for the Source and Target databases

   Dim SourceDBConnectionString As String = "Data Source=""" & "SourceDBPath" & """;Jet OLEDB:Database Password=" & "Password"

   Dim TargetDBConnectionString As String = "Data Source=""" & "destFilePath" & """;Jet OLEDB:Database Password=" & "Password"

   'Calling the CompactDatabase function of the JRO.JetEngine object
   objJetEngine.CompactDatabase(SourceDBConnectionString, TargetDBConnectionString)

End Sub

The above code will compact only the Access databases of 2003 and earlier versions, (with .mdb extensions). If you want to compact the Access databases 2007 version, please visit the following post:-
http://techieyogi.blogspot.com/2009/11/how-to-compact-access-2007-database.html





 

23 November 2009

How to Compact Access 2007 Database Programmatically

Add a reference to “Microsoft Office 12.0 Access Database Engine Object Library”.



And write a following code to compact the Access 2007 Database:-

// Create a new object of DBEngine

Microsoft.Office.Interop.Access.Dao.DBEngine objDbEngine = new Microsoft.Office.Interop.Access.Dao.DBEngine();

// Call the CompactDatabase method on object of DBEngine

objDbEngine.CompactDatabase(“C:\SourceDB.accdb”, “C:\TargetDB.accdb”, null, null, ";pwd=passwordOfDatabaseIfAnyElseNULL");

For more information about the function parameters refer to the following link:-

http://msdn.microsoft.com/en-us/library/bb220986.aspx