Can anyone provide a quick solution for INSERT the entire excel sheet into a access database.
This is a click button event that just opens a excel sheet in a windows form. What I like to see is some example how to INSERT the same excel data into a access db table within this click event. If possible within the same SQL query.
private void openToolStripButton_Click(object sender, EventArgs e)
{
openFileDialog();
}
private void openFileDialog()
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
System.Data.DataSet myDataSet;
System.Data.OleDb.OleDbDataAdapter MyCommand;
openFileDialog.Filter = "Microsoft Excel|*.xls|Microsoft Access|*.mdb";
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Title = "Select file";
openFileDialog.ShowDialog();
string filename = openFileDialog.FileName;
try
{
String connectString = "Provider=Microsoft.JET.OLEDB.4.0;data source=" + openFileDialog.FileName + ";Extended Properties=Excel 8.0";
System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(connectString);
myConnection.Open();
//Get SheetNames
DataTable sheetTable = myConnection.GetSchema("Tables");
DataRow rowSheetName = sheetTable.Rows[0];
String sheetName = rowSheetName[2].ToString();
MyCommand = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [" + sheetName + "]", myConnection);
//MyCommand.TableMappings.Add("Table", "TestTable");
myDataSet = new System.Data.DataSet();
MyCommand.Fill(myDataSet);
dataGridView1.DataSource = myDataSet.Tables[0];
myConnection.Close();
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
finally
{
toolStripStatusLabel1.Text = "Done";
}
}
}