Embed presentation
Download to read offline
![using System.Data.OleDb;
using System.Data.SqlClient;
OleDbConnection x;
OleDbCommand y;
OleDbDataReader z;
protected void Button1_Click(object sender, EventArgs e)
{
FileUpload1.SaveAs(Server.MapPath("~/File/")+FileUpload1.FileName);
x = new OleDbConnection("provider=microsoft.ace.oledb.12.0;data source="
+ Server.MapPath("~/File/") + FileUpload1.FileName + "; extended
properties=excel 12.0");
x.Open();
y = new OleDbCommand("select * from [sheet1$]", x);
z = y.ExecuteReader();
SqlConnection x1 = new SqlConnection("data source=AGILE867E8D92B;initial catalog=master;integrated security=sspi");
x1.Open();
SqlBulkCopy sb = new SqlBulkCopy(x1);
sb.DestinationTableName = "emp";
sb.WriteToServer(z);
x.Close();
x1.Close();
}
--------------------------](https://image.slidesharecdn.com/bulkcopy-131218035553-phpapp01/75/Bulk-copy-1-2048.jpg)

This code uploads an Excel file to the server, connects to it using OleDb to read data from the first sheet, and bulk copies the data into a SQL Server table called "emp" using SqlBulkCopy. It opens an OleDbConnection to the file, executes a query to select all data from the first sheet, and copies the results to a SqlConnection into the target table, then closes the connections.
![using System.Data.OleDb;
using System.Data.SqlClient;
OleDbConnection x;
OleDbCommand y;
OleDbDataReader z;
protected void Button1_Click(object sender, EventArgs e)
{
FileUpload1.SaveAs(Server.MapPath("~/File/")+FileUpload1.FileName);
x = new OleDbConnection("provider=microsoft.ace.oledb.12.0;data source="
+ Server.MapPath("~/File/") + FileUpload1.FileName + "; extended
properties=excel 12.0");
x.Open();
y = new OleDbCommand("select * from [sheet1$]", x);
z = y.ExecuteReader();
SqlConnection x1 = new SqlConnection("data source=AGILE867E8D92B;initial catalog=master;integrated security=sspi");
x1.Open();
SqlBulkCopy sb = new SqlBulkCopy(x1);
sb.DestinationTableName = "emp";
sb.WriteToServer(z);
x.Close();
x1.Close();
}
--------------------------](https://image.slidesharecdn.com/bulkcopy-131218035553-phpapp01/75/Bulk-copy-1-2048.jpg)