Wednesday, December 16, 2009

How to Export data from Excel to SQL using C#

The current example will show you how to export data from Excel to SQL database using C#. It requires OLEDB drivers need to be installed on your system(OLEDB drivers comes by default with office 2007 products) or you can download explicitly from http://www.microsoft.com/downloads/details.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=en.

string strconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0\"";

//OLEDB connection for Excel

OleDbConnection olConn = new OleDbConnection(strconn);
OleDbDataReader orr = default(OleDbDataReader);
olConn.Open();

DataTable dt = olConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);


OleDbCommand od = new OleDbCommand("SELECT * FROM ['" + ConfigurationManager.AppSettings["ExcelSheetName"].ToString() + "$']", olConn);
if (od != null)
{
orr = od.ExecuteReader();
//If Data Exists
if (orr.HasRows)
{
string strconn1 = null;
strconn1 = /* Provide connection string */
SqlBulkCopy sqCopy = new SqlBulkCopy(strconn1);
//Give the Destination table name
sqCopy.DestinationTableName = "SampleTable";
//Here the only condition is it takes the first character of first column of first row and creates column with it.
Eg: if there is a word like "Finance" in the first column of first row it takes the column names as Finance and the second column as F2 and 3rd as F3 .....

//So we need to create the destination table with the same column names and then do mapping
sqCopy.ColumnMappings.Add("F2", "F2");
sqCopy.ColumnMappings.Add("F3", "F3");
sqCopy.ColumnMappings.Add("F4", "F4");
sqCopy.ColumnMappings.Add("F5", "F5");
sqCopy.ColumnMappings.Add("F6", "F6");
sqCopy.ColumnMappings.Add("F7", "F7");
sqCopy.ColumnMappings.Add("F8", "F8");
sqCopy.ColumnMappings.Add("F9", "F9");
sqCopy.ColumnMappings.Add("F10", "F10");
//Then write to the server
sqCopy.WriteToServer(orr);

Sunday, December 6, 2009

XMPP Gateway Works with OpenFire

Now the XMPPGateway works with Openfire.All you need to do is to install the latest patch available for XMPP Gateway at
http://support.microsoft.com/?kbid=977187
and configure openfire to support only TCPDIALBACK