![]() Hide this upload button, so that the user will not click again, while its already uploading, as shown below. Now, we have to insert this mapped data into student table, as shown below. In source column, we have given Excel columns and destination column table column fields. Objbulk.DestinationTableName = "Student" Now, we will use SQLbulkcopy to map this Excel data into student table, as shown below. If (Exceldt.Rows = DBNull.Value || Exceldt.Rows = DBNull.Value ) ![]() Here, I am checking two mandatory fields, if any one value is null, then I will delete that row and update the data table. OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ) Įcon.Close() //close Excel connection after adding to data setĭataTable Exceldt = ds.Tables //copy data set to datatableĬheck if any mandatory field is required before uploading to the table, if mandatory field value is empty in Excel, then we can remove that row from the above data table, as shown below. In select statement, column names and Excel sheet column names should be the same and if an Excel sheet has column name with spaces like above Employee Name, then you need to provide this into two square braces.Ĭreate one dataset and fill this data set with this selected items, using oledbdataadpter, as shown below. OleDbCommand Ecom = new OleDbCommand(Query, Econ) using Īnd add Excel connection as below add select query to select the data from this Excel sheet and open this Excel connection, as shown below string Query = string.Format("Select , FROM ", "Sheet1$") Inside this, we are going to write all the functionalities.Ĭreate an Excel connection, using OLEDb. Write the function for importing Excel data, as shown below.Ĭreate function as below private void InsertExcelRecords() If we do this directly, then the form will respond until the task finishes, so we need to use Background worker process here, as shown below.īackgroundWorker bw = new BackgroundWorker This code can be done in an Upload button.īefore beginning, we know that this task is a log task, since we need to take the data from an Excel sheet and we need to insert all this data into the table. Next is the main task, i.e we need to read the data from an Excel sheet and insert all the data into the table. OpenFileDialog od = new OpenFileDialog() Īdd string _path outside this button, so that I can access this string outside this button and assign Excel file path to this _path string, as shown below. To browse, I am calling OpenFileDialog class, given filter and add file path to Text Box. I added the code for browse button to browse an Excel file, as shown below. I designed it, as given below (two buttons, One textbox, one label and one GIF image). I have created win form Application and I added Form named as ImportFromExcel. ![]() Excel sheet, which I have created is given below. I inserted two records, using the script given below. Here, I am explaining how we can export the data from an Excel sheet data into particular SQL table.įor exporting an Excel data, we need an Excel sheet with the data to be exported into SQL table.įor this demo, I have created a database and a table. Sometimes we will get a requirement that we need to export the data from Excel or other source into the database table.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |