NOTE: This page is about data providers that work with the .NET Framework 1.1, see the DataDirect Connect for ADO.NET product page for information about data providers that support .NET Framework 2.0 and higher.
Since the release of our first ADO.NET managed data providers in June 2002, DataDirect Technologies has been fully committed to boosting operation performance in real-world environments such as DB2.
The material covered here provides code examples that can reduce application development time by providing working C# source code examples showing typical database access tasks in ADO.NET, using the DataDirect Connect for .NET DB2 data provider.
To ensure that these code examples work, you must create the sample tables. In addition, you must modify the connection strings in the examples to work in your environment. Finally, make sure your project includes the "using" directive for System.Data and DDTek.DB2:
All of the samples in this document use the emp and/or dept tables. You can create the tables in the DB2 command center or using the DB2 data provider.
Copy the following statements in the script window of the DB2 command center and execute them:
In the following code, you create the sample tables using the data provider. DB2Connection Conn;
The DataReader provides the fastest but least flexible way to retrieve data from the database. Data is returned as a read-only, forward-only stream of data that is returned one record at a time.
If you need to retrieve many records rapidly, using a DataReader requires less memory than a DataSet, which needs to use large amounts of memory to hold the results.
The following code example, which uses the emp table, shows how to execute a simple query on a DB2 database and read the results using a DataReader:
The following code example, which uses the emp table, shows how to use a local transaction:
The following code example uses the emp table with the CommandBuilder to create a SQL statement on DB2:
When updating a row at the data source, the DataSet uses the SQL provided in UpdateCommand of the Data Adapter.
The Update statement can use parameters that contain the unique identifier, such as the primary key, and the columns to be updated, as shown in the following example:
The parameterized query statements define the parameters that will be created. Refer to the DataDirect Connect for .NET User's Guide and Reference for more information about using parameters with the DB2 data provider. The following code example uses the emp table to show how to provide an UpdateCommand to a DataAdapter for use in synchronizing changes made to a DataSet with the actual data on the DB2 server. The example uses the Parameters.Add method to create the parameters for the Update statement, fills a DataSet, programmatically makes changes to the DataSet, then synchronizes the changes back to the database.
You call stored procedures using a Command object. When you issue a command on a stored procedure, you must either set the CommandType of the Command object to StoredProcedure, or use ODBC/JDBC escape syntax. For information on using the ODBC/JDBC escape syntax with the data provider, refer to the DataDirect Connect for .NETUser's Guide.In the following example, you set the CommandType of the Command object to StoredProcedure as you execute a stored procedure on a DB2 database and retrieve the value of an output parameter to the stored procedure. This example requires the emp table.
You can create the following stored procedure using either the data provider or the DB2 Command center.
To use the data provider to create the stored procedure, execute the following code:
Or, use the DB2 Command Center to create it by executing the following SQL:
CREATE PROCEDURE GetEmpSalary (in inempno int ,out outsal char(7)) Language SQL reads SQL data SELECT char(sal) INTO outsal from emp where empno = inempno Executing the stored procedure
The following code example executes the GetEmpSalary stored procedure using the data provider and retrieves the value of an output parameter:
You can use the ExecuteScalar method of the Command object to return a single value, such as a sum or a count, from the database. The ExecuteScalar method returns the value of the first column of the first row of the result set. If you know the result set has only one row and one column, you can use this method to speed up retrieval of the value.
The following code example shows how to retrieve the count of a specified group from the emp table: