Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.05/5 (4 votes)
See more:
Hi Guyz am trying for c# code for importing ms access table to mssql programmatically, i need code ,could someone provide me the code or link . i tried some which was not functioning properly.
Posted
Comments
Thanks7872 13-Sep-13 3:33am    
Show that code which was not functioning properly. Also explain,what you expected and what happens actually with that code.

private void button2_Click(object sender, EventArgs e)
{
// Create sql connection string
string conString = @"Data Source = MAXCLIENT01; Initial Catalog = dbs_Test1;";
SqlConnection sqlCon = new SqlConnection(conString);
sqlCon.Open();

SqlDataAdapter da = new SqlDataAdapter("select * from vegetable", sqlCon);
System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
da.Fill(dtMainSQLData);
DataColumnCollection dcCollection = dtMainSQLData.Columns;
// Export Data into EXCEL Sheet
/* the foloowing line am getting Error*/
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();


ExcelApp.Application.Workbooks.Add(Type.Missing);
// ExcelApp.Cells.CopyFromRecordset(objRS);
for (int i = 1; i < dtMainSQLData.Rows.Count + 1; i++)
{
for (int j = 1; j < dtMainSQLData.Columns.Count + 1; j++)
{
if (i == 1)
ExcelApp.Cells[i, j] = dcCollection[j - 1].ToString();
else
ExcelApp.Cells[i, j] = dtMainSQLData.Rows[i - 1][j - 1].ToString();
}
}
ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\Users\\User\\Documents\\Vegetable.xlsx");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
MessageBox.Show("Data Exported Successfully into Excel File");

}

private void button1_Click(object sender, EventArgs e)
{
// Create Data Table for MS-Office 2007 or 2003
System.Data.DataTable dtExcel = new System.Data.DataTable();
dtExcel.TableName = "MyExcelData";
string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\\Users\\User\\Documents\\Vegetable.xlsx';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
OleDbConnection con = new OleDbConnection(SourceConstr);
string query = "Select * from [Vegetable$]";
OleDbDataAdapter data = new OleDbDataAdapter(query, con);
data.Fill(dtExcel);

MessageBox.Show("Data Imported Successfully into DataTable");
}
am getting Error as
type or namespace office does not exists.help me out
 
Share this answer
 
What about Google[^]? There are tons of examples.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900