raj kadam

raj kadam

  • NA
  • 92
  • 7.5k

Importing Access Database fil into SQL Server 2012 using c#

Apr 4 2015 6:15 AM
public void importdatafromexcel(string excelfilepath)
{
//string ssqltable = "tdatamigrationtable";
//string myexceldataquery = "select id,p_name,city from [medi1$]";
string excelconnectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=medi1.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
using (OleDbConnection connection = new OleDbConnection(excelconnectionstring))
{
OleDbCommand command = new OleDbCommand("Select id,p_name,city FROM [medi1$]", connection);
connection.Open();
using (DbDataReader dr = command.ExecuteReader())
{
string sqlconnectionString = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlconnectionString))
{
bulkCopy.DestinationTableName = "ExcelData";
bulkCopy.WriteToServer(dr);
}
}
}

Answers (1)