Contents Index Using the .NET provider in a Visual Studio .NET project Connection pooling

ASA Programming Guide
  Developing Applications with the .NET Data Provider

Connecting to a database


Before you can carry out any operations on the data, your application must connect to the database. This section describes how to write code to connect to an Adaptive Server Anywhere database.

For more information, see AsaConnection class and ConnectionString property.

To connect to an Adaptive Server Anywhere database

  1. Allocate an AsaConnection object.

    The following code creates an AsaConnection object named conn:

    AsaConnection conn = new AsaConnection(

    You can have more than one connection to a database from your application. Some applications use a single connection to an Adaptive Server Anywhere database, and keep the connection open all the time. To do this, you can declare a global variable for the connection:

    private AsaConnection      _conn;

    For more information, see the sample code in Samples\ASA\ado.net\TableViewer\TableViewer.csproj and Understanding the Table Viewer sample project.

  2. Specify the connection string used to connect to the database.

    For example:

    "Data Source=ASA 9.0 Sample;UID=DBA;PWD=SQL" );

    For a complete list of connection parameters, see Connection parameters.

    Instead of supplying a connection string, you could prompt users for their user ID and password if you wish.

  3. Open a connection to the database.

    The following code attempts to connect to a database. It autostarts the database server if necessary.

    conn.Open();
  4. Catch connection errors.

    Your application should be designed to catch any errors that occur when attempting to connect to the database. The following code demonstrates how to catch an error and display its message:

    try {
        _conn = new AsaConnection( txtConnectString.Text );
        _conn.Open();
      } catch( AsaException ex ) {
        MessageBox.Show( ex.Errors[0].Source + " : "
         + ex.Errors[0].Message + " (" +
         ex.Errors[0].NativeError.ToString() + ")",
             "Failed to connect" );

    Alternately, you can use the ConnectionString property to set the connection string, rather than passing the connection string when the AsaConnection object is created:

    AsaConnection   _conn;
        _conn = new AsaConnection();
        _conn.ConnectionString =
         "Data Source=ASA 9.0 Sample;UID=DBA;PWD=SQL";
        _conn.Open();
  5. Close the connection to the database. Connections to the database stay open until they are explicitly closed using the conn.Close() method.

Visual Basic .NET connection example 

The following Visual Basic .NET code opens a connection to the Adaptive Server Anywhere sample database:

Private Sub Button1_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click
    ' Declare the connection object
    Dim myConn As New _
      iAnywhere.Data.AsaClient.AsaConnection()
    myConn.ConnectionString = _
      "Data Source=ASA 9.0 Sample;UID=DBA;PWD=SQL"
    myConn.Open()
    myConn.Close()
End Sub

Connection pooling
Checking the connection state

Contents Index Using the .NET provider in a Visual Studio .NET project Connection pooling