This tutorial describes how to use SqlCommand component to insert data into tables by means of executing SQL queries.
In this walkthrough:
This walkthrough supposes that you know how to connect to a server (tutorial Logging onto the server) and that necessary objects are already created on the server (tutorial Creating database objects).
Note that if you do not use design-time (specifically, if you do not place on a form designer SqlConnection component from toolbox), you have to embed licensing information manually. This is described in the Licensing topic.
Data on a server can be modified (inserted, updated or deleted) using Data Manipulation Language (DML), which is a part of SQL. The DML statements can be executed on server by account that has necessary privileges.
There are two ways to manipulate a database. You can build DML statements manually and run them within some component like SqlCommand. Another way is to use design-time features that provide graphical user interface to manage database.
The goal of this tutorial is to insert the following data into tables dept and emp:
Table dept
deptno |
dname |
loc |
---|---|---|
10 | Accounting | New York |
20 | Sales | Dallas |
30 | Sales2 | Chicago |
Table emp
empno |
ename |
job |
mgr |
hiredate |
sal |
comm |
deptno |
---|---|---|---|---|---|---|---|
7369 | Smith | Clerk | 7566 | 1980-12-17 | 800 | Null | 20 |
7499 | Allen | Salesman | 7698 | 1981-02-20 | 1600 | 300 | 30 |
7521 | Ward | Salesman | 7698 | 1981-02-22 | 1250 | 500 | 30 |
7566 | Jones | Manager | 7839 | 1981-04-02 | 2975 | Null | 20 |
7654 | Martin | Salesman | 7698 | 1981-09-28 | 1250 | 1400 | 30 |
7698 | Blake | Manager | 7839 | 1981-05-01 | 2850 | Null | 30 |
7839 | King | President | Null | 1981-11-17 | 5000 | Null | 10 |
To insert the first row into table dept you can use the following statement:
INSERT INTO dept (deptno, dname, loc) VALUES (10,'Accounting','New York')
The following code fragment executes the query:
The sample first creates a connection with hardcoded connection string. Then it creates SqlCommand object, assigns the query text and connection to the SqlCommand instance. Connection is opened then. The ExecuteNonQuery() method of SqlCommand runs SQL statement in the CommandText property and returns the number of rows affected by the query. This method is not intended to run SELECT statements. We will discuss retrieving data in other tutorials.
If the query is executed successfully you are notified about number of affected rows. If some error occurs you get the error message. The connection is closed anyway. It is recommended that you use try ... finally clauses to make sure the connections are closed properly.
Same operations in design time include following steps:
Note that the last two steps might be easier to do in the SqlCommand editor. To invoke it choose CommandText item from SqlCommand popup menu or click on the dots in this property in Properties window.
Actually there are lots of ways to insert data into tables. Any tool or component that is capable of running a SQL query, can be used to manage data. Some components are best for performing certain tasks. For example, SqlLoader is the fastest way to insert data, SqlScript is designed for executing series of statements. For more information on these components refer to dotConnect for SQL Server reference.
dotConnect for SQL Server Tutorials | SqlCommand Class | SqlLoader | SqlScript Class