Articles → .NET → Bulk Upload In Asp.Net
Bulk Upload In Asp.Net
Software Requirement
- visual studio (2005 or above) is installed on your machine
- SQL Server (2005 or above) is installed on your machine
Prerequisite Knowledge
- How to create web project using visual studio?
- What are web forms?
- What are web controls?
- What are events in web application?
- Basics of ADO.NET
- Basics of SQL server objects like table.
Steps For Execution
- Create a new web project
- Create a new table
- Create an excel file to upload
- Add web controls on the page
- Add code on button click
Create A New Web Project
Click to Enlarge
Create A New Table
Click to Enlarge
Create An Excel File To Upload
Click to Enlarge
Add Web Controls On The Page
- File upload control – to select the excel file
- Button – to write the logic of bulk upload.
Click to Enlarge
Add Code On Button Click
protected void btnUpload_Click(object sender, EventArgs e) {
string excelConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HRD=YES;IMEX=1'", FileUpload1.PostedFile.FileName);
using(OleDbConnection connection = new OleDbConnection(excelConnectionString)) {
OleDbCommand command = new OleDbCommand(("Select * FROM [Sheet1$]"), connection);
connection.Open();
using(DbDataReader dr = command.ExecuteReader()) {
using(SqlBulkCopy bulkCopy = new SqlBulkCopy("your_Connection_string")) {
bulkCopy.DestinationTableName = "Member";
bulkCopy.ColumnMappings.Add("Member Name", "member_name");
bulkCopy.WriteToServer(dr);
}
}
}
}
string excelConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HRD=YES;IMEX=1'", FileUpload1.PostedFile.FileName);
using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
OleDbCommand command = new OleDbCommand(("Select * FROM [Sheet1$]"), connection);
using (DbDataReader dr = command.ExecuteReader())
using (SqlBulkCopy bulkCopy = new SqlBulkCopy("your_Connection_string"))
bulkCopy.DestinationTableName = "Member";
bulkCopy.ColumnMappings.Add("Member Name", "member_name");
bulkCopy.WriteToServer(dr);
Output
Click to Enlarge
Click to Enlarge