Search This Blog

Sunday, March 9, 2014

Upload Excel data into Sql server database in C# windows forms

To upload your excel data in .net windows forms, design a form like below.

On the brows button write the code to select the file from your hard drive

private void btnUpload_Click(object sender, EventArgs e)
{
OpenFileDialog ofd=new OpenFileDialog();

if(ofd.ShowDialog()==DialogResult.OK)
{
txtFilePath.Text=ofd.FileName;
}

}

private void btnUpload_Click(object sender, EventArgs e)
{
OleDbConnection objCon= new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data
Source=" + txtFilePath.text.trim() + ";Extended Properties=Excel 8.0");
connection.Open();
OleDbDataAdapter adap = new OleDbDataAdapter("select * From [Sheet1$]", connection);
dt = new DataTable();
adap.Fill(dt);
 if(dt.Rows.count>0)
 {
 for(int i=0; i
{ // Here declare Sqlcommand and Find data from dt. } } }

No comments:

Post a Comment