The PgSqlDataTable class extends the Devart.Common.DbDataTable class to provide a single object that can be used to access and update data from a data source. When you use multiple table select command, you will receive the exception, that insert command cannot be generated for multiple tables. In this case you should generate insert command by yourself. You have to assign two update commands to your myDataTable.InsertCommand and add 'unprepared execute=True' to your ConnectionString.
We will consider that both table1 and table2 have two fields: field1 and field2 are the fields of table1, and field3 and field1 are the fields of table2.
Creating InsertCommand:
Code: Select all
myDataTable.InsertCommand = New PgSqlCommand("INSERT INTO table1 (field1, field2) VALUES (:field1, field2); INSERT INTO table2 (field3, field1) VALUES (:field3, :field1,)",Database.Connection)
myDataTable.InsertCommand.Parameters.Add(New PgSqlParameter("field1", PgSqlType.Int, 0, "field1"))
myDataTable.InsertCommand.Parameters.Add(New PgSqlParameter("field2", PgSqlType.Int, 0, "field2"))
myDataTable.InsertCommand.Parameters.Add(New PgSqlParameter("field3", PgSqlType.Int, 0, "field3"))
Then add a new row and update the datatable:
Code: Select all
myDataTable.Rows.Add(New Object() {10, 11, 12})
myDataTable.Update()
New rows will be inserted to table1 and table2.
You can create this command in design time. For this you should perform the following:
1. Drag PgSqlDataTable on your form.
2. Double click on it.
3. Specify the select command for the first table.
4. Open the third tab "Command Generator" and generate the insert command.
5. Copy it to the Clipboard.
6. Perform the 1-4 steps for the second table.
7. Paste the first command after the second one. Separate the commands with the semicolon ';' character.
8. Click the Edit button.
9. Switch to the Parameters tab.
10. Specify the source column if it is not specified automatically.