This tutorial describes how to use PgSqlCommand component to insert data into tables by means of executing SQL queries.
In this tutorial:
This walkthrough supposes that you know how to connect to 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 PgSqlConnection component from toolbox), you have to embed licensing information manually. This is described in the Licensing topic.
Data on server can be modified (inserted, changed 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 PgSqlCommand. Another way is to use design-time features that provide graphical user interface to manage database. We will discuss both ways.
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 |
---|---|---|---|---|---|---|---|
7839 | King | President | Null | 1981-11-17 | 5000 | Null | 10 |
7698 | Blake | Manager | 7839 | 1981-05-01 | 2850 | Null | 30 |
7566 | Jones | Manager | 7839 | 1981-04-02 | 2975 | Null | 20 |
7499 | Allen | Salesman | 7698 | 1981-02-20 | 1600 | 300 | 30 |
7369 | Smith | Clerk | 7566 | 1980-12-17 | 800 | Null | 20 |
7521 | Ward | Salesman | 7698 | 1981-02-22 | 1250 | 500 | 30 |
7654 | Martin | Salesman | 7698 | 1981-09-28 | 1250 | 1400 | 30 |
To insert the first row into table dept you can use the following statement:
INSERT INTO public.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 PgSqlCommand object, assigns the query text and connection to the PgSqlCommand instance. Connection is opened then. The ExecuteNonQuery() method of PgSqlCommand runs SQL statement in the CommandText property and returns 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:
Place PgSqlConnection component on a form designer.
Setup its properties and open connection by changing the State property to Open
- or -
right-click on the component, choose Connect from the popup menu and use the dialog to connect to server.
While setting the connection properties, set its Protocol property to Ver20 to use PgSqlCommand for executing more than one statement at once. Otherwise, you may need to execute each statement separately.
Place PgSqlCommand component on the designer.
Select name of the PgSqlConnection instance in its Connection property.
Select true in its UnpreparedExecute property (that allows you to execute several statements at once with one PgSqlCommand).
In the CommandText property type in the following query:
INSERT INTO public.dept(deptno,dname,loc) VALUES (20,'Sales','Dallas'); INSERT INTO public.dept(deptno,dname,loc) VALUES (30,'Sales2','Chicago');
Right-click on the PgSqlCommand and choose Execute from popup menu.
Note that the last two steps might be easier to do in the PgSqlCommand editor. To invoke it right-click the component and choose CommandText from the PgSqlCommand popup menu.
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, PgSqlLoader is the fastest way to insert data, PgSqlScript is designed for executing series of statements. For more information on these components refer to dotConnect for PostgreSQL reference.
Getting Started | PgSqlCommand Class | PgSqlScript Class | PgSqlLoader Class