There is quite a good guide on MSDN: A practical guide. Insert new records in the database , which has the following items:
Insert new entries using table adapters
At this point, everything is quite simple. There are two approaches for inserting data into the desired DataTable in a dataset using TableAdapter.Update and TableAdapter.Insert . To do this, you need an instance of DataTable , which will be used as a table adapter.
To insert new records into the database using the TableAdapter.Update method:
- Add new entries to the desired
DataTable tables by creating new DataRow rows and adding them to the Rows collection. For more information, see the Practical Guide. Adding rows to a DataTable . - After adding new rows to the
DataTable call the TableAdapter.Update method. You can control the amount of data to update by transferring the entire DataSet , DataTable , DataRow array, or a single DataRow .
To insert new records into the database using the TableAdapter.Insert method:
- Call the
Insert Table Adapter method, passing values for each column as parameters.
Inserting new entries using command objects
That is, new entries will be inserted directly into the database using command objects. That is, create a SqlConnection , open a connection, execute a command, and close. In principle, what you already do at home.
To insert new entries into the database using command objects:
- Create a new command object by setting its
Connection , CommandType and CommandText .
Note:
You must have access to the database to which you are trying to connect, as well as permission to insert records into the desired table. Well, I think this is understandable.
Regarding the approach with OleDbConnection , of course, at first glance, it seems inconvenient and cumbersome, but if everything is carried out into separate methods and beautifully decorated, for example, you can make universal methods for inserting, selecting and deleting objects and putting it into a separate class, something similarity Helper, you will not notice the bulkiness that envelops you now, it looks not so bad. I think it is from this and begin the search for other approaches.
Also, I think the following links will be helpful:
- Spreadsheet Adapter Overview
- Class SqlCommand
- Class DataTable
OleDbConnectionapproach didn’t suit you, but if you put everything into separate methods and arrange it beautifully, it looks not so bad. Perhaps you will be interested in the following page: A practical guide. Insert new records into the database - Denis BubnovBulk Insert- Denis BubnovBulk Insert- Denis Bubnov