Sunday, September 9, 2012

Read XML File and Return the Value using C#.NET

Read XML file ..


I know you will get confuse about my topic. But I am writing this post to give basic function to read an XML file and it returns the value of the given node. Sometimes you will need to store your configuration values in xml file because it is one of the basic methods that we can use to store data.

1. Add these libraries

    using System.Xml;
    using System.Xml.XPath;

2. This is the function
    public static string GetValue(string Element , string FilePath)
        {
            XPathNavigator nav;
            XPathDocument docNav;
            XPathNodeIterator NodeIter;
            String strExpression;
            string value = "";

            // Open the XML.
            docNav = new XPathDocument(FilePath);

            // Create a navigator to query with XPath.
            nav = docNav.CreateNavigator();

            /// Find the telement of the configure
         
            strExpression = "/Configure/" +Element;

            // Select the node and place the results in an iterator.
            NodeIter = nav.Select(strExpression);

            //Iterate through the results showing the element value.
            while (NodeIter.MoveNext())
            {
                value = NodeIter.Current.Value.ToString().Trim();
            };
            return (value);
        }
    }

3. We call this function

   string filepath = @"C:\Users\Hasitha\books1.xml";
   string element = "Path";
   Console.WriteLine(GetValue(element ,filepath));

4. This my xml file

    <?xml version='1.0'?>
    <!-- This file represents a sample database information -->
    <Configure>
<Path>c:\temp</Path>
<Server>SQL SERVER</Server>
    </Configure>

5. You can add your own node into xml file then you can modify the strExpression variable

  More about XML , XML Path








Monday, August 27, 2012

Data Compression Techniques in SQL Server 2008 R2

This feature was introduced in SQL Server 2008 and it was enhanced in SQL Server 2008 R2. Basically this feature,   
  •    Helps to reduce the size of the database
  •    Improves the performance of I/O insensitive workload
  •    Requires extra CPU resources on database to compress and decompress the data
Why do we need data compression ?


There are 3 main things that we can obtain using data compression,
  • Save the cost of disk usage
  • Reduce I/O usage
  • Reduce memory usage  
Let me explain this,
As an example take table called "Employee Details". Normally this table can appear in different locations in our database environment such as development environment, QA environment, production environment, backup set, mirror server etc. This means if you can reduce the size of the table you can save overall disk space from these locations. 
Another point, if you can reduce the size of the table that mean you need only few number of data pages to store data (less number of data pages compare with without reduce table size). Less number of data pages means SQL server needs only few number of I/Os to get that data from disk to memory (Logical I/O).Finally less number of data pages require only few memory cells to store that retrieved data.

I think you get a brief idea about above mentioned three points.

Type of Data Compression

Basically SQL server supports two type of data compression. These are,
  • Row Compression
  • Page Compression
In SQL Server 2008 R2 supports Unicode compression and I'm not going to discuss about the Unicode compression in details here.

Row Compression
  • Convert fixed data types to a variable data type
  • No compression algorithm
  • Less compression ratio compared to page compression

Page Compression
  • Super set of Row Compression (SQL Server automatically add Row compression before it goes to Page Compression)
  • Reduce column redundancy
  • Use compression algorithm
  • More compression ratio compared to row compression
I think this is the time to move for the demonstration.

Create to three tables (Uncompress , Compress_Row,Compress_Page)

 

 I inserted the dummy data as well.(Same data I inserted for all table).


Executed select query against both 3 tables seperatly. (Execution time also there)



Executed sp_spaceused to get size of the table(Result also there)

Additionally, I 'll show you how to get estimated saving (Result also  there)

sp_estimate_data_compression_savings 'dbo','UnCompress',null,null,'ROW'
GO
sp_estimate_data_compression_savings 'dbo','UnCompress',null,null,'PAGE'






 There are very simple codes to get the an idea about the database compression

I hope to see you again with another interesting topic.