Wednesday 21 December 2011

How to import MS Excel data to SQL Server table using c#.net



If you already have data in MS Excel file, and want to migrate your MS Excel data to SQL Server table, follow below steps
1. Lets take an example to import the data to SQL Server table, I am going to import student information data from ms excel sheet to tStudent SQL table,
My Excel sheet structure is looks like 

2. Now design a tStudent table in SQL server
Create Table
(
StudentName varchar(64),
RollNo varchar(16),
Course varchar(32),
your ms excel sheet and SQL table is ready, now its time to write c# code to import the excel sheet intotStudent table 
3.
Add these two name space in your class file
using System.Data.OleDb;
using System.Data.SqlClient;
Use following code
public void importDataFromExcel(string excelFilePath)
//Declare Variables - Edit these based on your particular situation
 string sSQLTable = "tDataMigrationTable";
 // make sure your sheet name is correct, here sheet name is Sheet1, so you can change your sheet name if have different
string myExcelDataQuery = "Select StudentName,RollNo,Course from [Sheet1$]"
try
{
//Create our connection strings
string sExcelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath + ";Extended Properties=" + "\"Excel 8.0;HDR=YES;\"";

string sSqlConnectionString = "SERVER=MyDatabaseServerName;USER ID=DBUserId;PASSWORD=DBUserPassword;DATABASE=DatabaseName;CONNECTION RESET=FALSE"
//Execute a query to erase any previous data from our destination table
string sClearSQL = "DELETE FROM " + sSQLTable;
SqlConnection SqlConn = new SqlConnection(sSqlConnectionString);
SqlCommand SqlCmd = new SqlCommand(sClearSQL, SqlConn);
SqlConn.Open();
SqlCmd.ExecuteNonQuery();
SqlConn.Close(); 
//Series of commands to bulk copy data from the excel file into our SQL table
OleDbConnection OleDbConn = new OleDbConnection(sExcelConnectionString);
OleDbCommand OleDbCmd = new OleDbCommand(myExcelDataQuery, OleDbConn); 
OleDbConn.Open(); 
OleDbDataReader dr = OleDbCmd.ExecuteReader();
SqlBulkCopy bulkCopy = new SqlBulkCopy(sSqlConnectionString); 
bulkCopy.DestinationTableName = sSQLTable; 
while (dr.Read())
{
bulkCopy.WriteToServer(dr);
OleDbConn.Close();
}
catch (Exception ex)
{
//handle exception
} 
In above function you have to pass ms excel file path as a parameter, if you want to import your data by providing client an access to select the excel file and import, then you might have to use asp.net file control, and upload the excel file on the server in some temp folder, then use the file path of the upload excel file and pass the path in above function. Once data import is completed then you can delete temporary file. 
The above method , first delete the existing data from the destination table, then import the excel data into the same table.

1 comment:

  1. Simple accounting using the wizard, so that we can import any data source, simple accounting, including general journal entries, or any other form of accounting documents from your spreadsheet program, such as accountants program.

    Excel courses sydney

    ReplyDelete

Contact Us:

Email:

Vinodkumar434@gmail.com,
vinodtechnosoft@gmail.com

Skype Name:

vinodtechnosoft