DB2Connection Class
Represents an open connection to a database.
- Namespace:
IBM.Data.DB2
- Assembly:
IBM.Data.DB2
(inIBM.Data.DB2.dll
)
.NET Framework 2.0 3.0, 3.5 and 4.0 Inheritance hierarchy
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Data.Common.DbConnection
IBM.Data.DB2.DB2Connection
.NET Framework 2.0 3.0, 3.5 and 4.0 Syntax
[Visual Basic]
NotInheritable Public Class DB2Connection
Inherits DbConnection
Implements ICloneable, IDbConnection
[C#]
public sealed class DB2Connection : Component, ICloneable,
IDbConnection
[C++]
public __gc __sealed class DB2Connection : public Component,
ICloneable, IDbConnection
[JScript]
public class DB2Connection extends Component implements
ICloneable, IDbConnection
Remarks
A DB2Connection object represents a unique connection to the database specfied in the connection string. In the case of a client/server database system, it is equivalent to a network connection to the server.
The DB2Connection object uses native resources. You should always explicitly close any open DB2Connection objects by calling Close or Dispose before the DB2Connection object goes out of scope. Not doing so leaves the freeing of these native resources to garbage collection, which may not free them immediately. This, in turn, may eventually cause the IBM® Data Server Provider for .NET or data server to run out of resources or reach a maximum limit, resulting in sporadic failures. For example, you might encounter Maximum Connections-related errors while a number of connections are waiting to be deleted by the garbage collector. Explicitly closing the connections by calling Close or Dispose allows a more efficient use of native resources, enhancing scalability and improving overall application performance.
If one of the Execute methods of the DB2®Command class results in a fatal DB2Exception , the DB2Connection may close. However, the user can reopen the connection and continue.
An application that creates an instance of the DB2Connection object can require all direct and indirect callers to have adequate permission to the code by setting declarative or imperative security demands. DB2Connection creates security demands by using the DB2Permission object. Users can verify that their code has adequate permissions by using the DB2PermissionAttribute object. Users and administrators can also use the Code Access Security Policy Tool (Caspol.exe) to modify security policy at the machine, user, and enterprise levels. For more information, see "Securing Applications" in the Microsoft .NET Framework SDK documentation.
Example
[Visual Basic, C#] The following example creates a DB2Command and an DB2Connection. The DB2Connection is opened and set as the DB2Command.Connection property. The example then calls DB2Command.ExecuteNonQuery, and closes the connection. To accomplish this, the ExecuteNonQuery is passed a connection string and a query string that is an SQL INSERT statement.
[Visual Basic]
Public Sub InsertRow(myConnectionString As String)
' If the connection string is null, use a default.
If myConnectionString = "" Then
myConnectionString = "DATABASE=SAMPLE;"
End If
Dim myConn As New DB2Connection(myConnectionString)
Dim myInsertQuery As String = "INSERT INTO STAFF (ID, NAME) Values(...)"
Dim myDB2Command As New DB2Command(myInsertQuery)
myDB2Command.Connection = myConn
myConn.Open()
myDB2Command.ExecuteNonQuery()
myConn.Close()
End Sub
[C#]
public void InsertRow(string myConnectionString)
{
// If the connection string is null, use a default.
if(myConnectionString == "")
{
myConnectionString = "DATABASE=SAMPLE;";
}
DB2Connection myConn = new DB2Connection(myConnectionString);
string myInsertQuery = "INSERT INTO STAFF (ID, NAME) Values(...)";
DB2Command myDB2Command = new DB2Command(myInsertQuery);
myDB2Command.Connection = myConn;
myConn.Open();
myDB2Command.ExecuteNonQuery();
myConn.Close();
}
Thread safety
Any public static (Shared
in
Visual Basic) members of this type are safe for multithreaded operations.
Any instance members are not guaranteed to be thread safe.