In this article, we will see how we can Import CSV File data to a Database in ASP.NET C# using SqlBulkCopy.
Let’s start to build the application which will process our required command.
Step 1. First, create a Database if it does not already exist, and then create a table in which the data is inserted after importing file data.
I Have Created a table, namely StudentCourses, and created three columns inside it.
![threecolumntable]()
Columns are StudentID, Name, and Course, which are old data after importing.
Here is the Script to create a table.
Before inserting Data, Here is our empty table.
![emptytable]()
Step 2. Let’s Build an Interface / Markup of File Upload Control and Button.
I am also using Bootstrap to make UI look good.
Here is the Full code of the UI.
Step 3. Before starting, we will create the connection string in the web. config. Today I will introduce you to the best connection string generator method, which can reduce the error and it also test the connection as well. Right Click on your PC Desktop, and it will open the context menu. Now, Choose New è Text Document, New text documents created now change the extension of a text file from .txt to .udl and Hit Enter.
It will show a prompt like this just press to continue, and the extension of the file has been changed.
![]()
Now double-click the file to open it will look like this.
![doubleclick]()
Now fill in the desired information in respective fields and test the connection. It will give you a prompt of failure or success.
![linkproperties]()
Now press ok at the prompt and then finally hit the OK button of the Data Link Properties Window. If you have selected the check box of Allow saving password, it will give you a confirmation message" Are you sure?"
![datalink]()
After selecting yes, it will print the saved password to the file. Now we will check how it looks like our connection string. Right-click the data link properties file and open it with Notepad. It will show you this final oledb connection string.
![connection string]()
Step 4. So we will start to first check the valid .csv extension file and Upload it to a certain path.
Firstly, check the file Extension by using the Path.GetExtension () Method, and we have saved the file to a Certain path by using Server.MapPath to assign the path and then use SaveAs() Method to upload it to the Path.
![]()
Step 5. In this step, we will do our code behind functionality to first upload to a certain path and then read the file using File.ReadAllText() Method. After that, a connection has been established with the database, and we will use SqlBulkCopy Class to define our destination table and write to the database using the WriteToServer() Method of SqlBulkCopy Class to finish the task.
Here is the full code which can do our desired task. Code Comments are added for well understanding.
After this line of code, we get a success message which assures us that our data dumped successfully.
![localhost]()
Step 6.The output of the CSV to Database Table.
![databasetable]()
Read more articles on C#