Monthly Archives: March 2014

How To Do SQL Server 2008 R2 Replication To Distribute Your Data Or Make Mirror

Replication means making exact copy or synchronize from source database to target with interval of time. So if source data get inserted then for short time target data will get inserted with same data from source.
Same as delete operation, if some records in source data deleted then same data in target table will deleted.

From technical point of view, Replication and Mirroring are different. Replication is synchronization process between databases and intended to be used by day to day application. Whereas mirroring is intended for high availability and data protection of database. So if live database corrupted then mirror database will be used automatically.
However from non technical point of view, they as if similar. Both source & target (secondary) server has same data but replication can have sql query in order to replicate for just specific data and/or combined data.

For some reasons, we want to have a replica for our database. In example, you have two servers with almost or same specification, one server is serving OLTP and other intended to reporting server.
The OLTP server will supply or export its data to Reporting Server, so that overall application load will be devided.

This export data from OLTP to Reporting can be done automatically using SQL Server 2008 R2 Replication. Users do transactional data from their application, data saved to OLTP database.
After that replication do insert or update or delete according source data in short time interval to target which is Reporting database. This makes exact state of data between source & target.

Another example is distribute central data to branches or vice versa. Let’s say you have central data that inputted by head office staffs.
You want some of the data to be distribute to branch servers so that branch users can view central data from their own server.
This also can be done using replication.

SQL Server Replication explanation

Copying some definitions from MS technet site => http://technet.microsoft.com/en-us/library/ms152567.aspx
Replication uses a publishing industry i.e magazine and/or newspaper metaphor to represent the components in a replication topology, which include Publisher, Distributor, Subscribers, publications, articles, and subscriptions.

The Publisher is a database instance that makes data available to other locations through replication. The Publisher can have one or more publications, each defining a logically related set of objects and data to replicate.

The Distributor is a database instance that acts as a store for replication specific data associated with one or more Publishers. Each Publisher is associated with a single database (known as a distribution database) at the Distributor. The distribution database stores replication status data, metadata about the publication, and, in some cases, acts as a queue for data moving from the Publisher to the Subscribers. In many cases, a single database server instance acts as both the Publisher and the Distributor. This is known as a local Distributor. When the Publisher and the Distributor are configured on separate database server instances, the Distributor is known as a remote Distributor.

A Subscriber is a database instance that receives replicated data. A Subscriber can receive data from multiple Publishers and publications. Depending on the type of replication chosen, the Subscriber can also pass data changes back to the Publisher or republish the data to other Subscribers.

An article identifies a database object that is included in a publication. A publication can contain different types of articles, including tables, views, stored procedures, and other objects. When tables are published as articles, filters can be used to restrict the columns and rows of the data sent to Subscribers.

A publication is a collection of one or more articles from one database. The grouping of multiple articles into a publication makes it easier to specify a logically related set of database objects and data that are replicated as a unit.

A subscription is a request for a copy of a publication to be delivered to a Subscriber. The subscription defines what publication will be received, where, and when. There are two types of subscriptions: push and pull.
Continue reading

Create Excel Report Programmatically Without Installing MS Excel. Asp.Net & Win Form C# Using WebService Example

Assuming you only have open source spreadsheet software i.e OpenOffice.org Calc and you want to make Excel report file programmtically.
You dont have MS Office installed in your computer or server. Sure you can do it using ExcelLibrary library from Google Apps.

In order to try examples in this blog post, please download ExcelLibrary library from this url https://code.google.com/p/excellibrary/downloads/list

Here, I give examples implementing it by creating a WebService so that Asp.Net Website or Win From C# can use this WebService.
This makes ExcelLibrary file only have to copy in WebService’s server without distribute it to local / client box.

First of all we need to know about basic of ExcelLibrary usage. Taken from https://code.google.com/p/excellibrary

//create new xls file
string file = "C:\\newdoc.xls";
Workbook workbook = new Workbook();
Worksheet worksheet = new Worksheet("First Sheet");
worksheet.Cells[0, 1] = new Cell((short)1);
worksheet.Cells[2, 0] = new Cell(9999999);
worksheet.Cells[3, 3] = new Cell((decimal)3.45);
worksheet.Cells[2, 2] = new Cell("Text string");
worksheet.Cells[2, 4] = new Cell("Second string");
worksheet.Cells[4, 0] = new Cell(32764.5, "#,##0.00");
worksheet.Cells[5, 1] = new Cell(DateTime.Now, @"YYYY\-MM\-DD");
worksheet.Cells.ColumnWidth[0, 1] = 3000;
workbook.Worksheets.Add(worksheet);
workbook.Save(file);

// open xls file
Workbook book = Workbook.Load(file);
Worksheet sheet = book.Worksheets[0];

 // traverse cells
 foreach (Pair<Pair<int, int>, Cell> cell in sheet.Cells)
 {
     dgvCells[cell.Left.Right, cell.Left.Left].Value = cell.Right.Value;
 }

 // traverse rows by Index
 for (int rowIndex = sheet.Cells.FirstRowIndex; 
        rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
 {
     Row row = sheet.Cells.GetRow(rowIndex);
     for (int colIndex = row.FirstColIndex; 
        colIndex <= row.LastColIndex; colIndex++)
     {
         Cell cell = row.GetCell(colIndex);
     }
 }

Continue reading