Contents Index Tutorial: Using the Table Viewer code sample Developing Applications with the .NET Data Provider

ASA Programming Guide
  Using the Adaptive Server Anywhere .NET Data Provider Sample Applications
    Tutorial: Using the Table Viewer code sample

Understanding the Table Viewer sample project


This section illustrates some key features of the Adaptive Server Anywhere .NET data provider by walking through some of the code from the Table Viewer code sample. The Table Viewer project uses the Adaptive Server Anywhere sample database, asademo.db, which is held in your SQL Anywhere installation directory.

For information about the sample database, including the tables in the database and the relationships between them, see The sample database.

In this section the code is described a few lines at a time. Not all code from the sample is included here. To see the whole code, open the sample project at Samples\ASA\ado.net\Tableviewer\TableViewer.csproj.

Declaring controls    The following code declares a TextBox labeled Connection String, a button named btnConnect, a TextBox labeled txtSQLStatement, a button named btnExecute, and a DataGrid labeled dgResults.

Private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtConnectString;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.TextBox txtSQLStatement;
private System.Windows.Forms.Button btnExecute;
private System.Windows.Forms.DataGrid dgResults;

Declaring a global variable    The AsaConnection function is used to declare a global variable. This connection is used for the initial connection to the database, as well as when you click Execute to retrieve the result set from the database.

private AsaConnection    _conn;

For more information about the AsaConnection function, see AsaConnection constructors.

Connecting to the database    The following code provides a default value for the connection string that appears in the Connection String field by default.

this.txtConnectString.Text =
  "Data Source=ASA 9.0 Sample";

The Connection object later uses the connection string ("Data Source=ASA 9.0 Sample") to connect to the sample database.

_conn = new AsaConnection( txtConnectString.Text );
_conn.Open();

For more information about the Connection object, see AsaConnection class.

Defining a query    The following code defines the default query that appears in the SQL Statement field.

this.txtSQLStatement.Text = "SELECT * FROM employee";

Displaying the results    Before the results are fetched, the application checks whether the Connection object has been initialized. If it has, it ensures that the connection state is open.

if( _conn == null || _conn.State !=
  ConnectionState.Open ) {
  MessageBox.Show( "Connect to a database first",
   "Not connected" );
  return;

Once you are connected to the database, the following code creates a new DataSet and uses the DataAdapter object (AsaDataAdapter) to execute a SQL statement (SELECT * FROM employee), and fill the DataSet. The last two lines bind the DataSet to the grid on the screen.

DataSet ds =new DataSet ();
AsaDataAdapter da=new AsaDataAdapter(
     txtSQLStatement.Text, _conn);
da.Fill(ds, "Results")
dgResults.DataSource = ds.Tables["Results"];

Because a global variable is used to declare the connection, the connection that was opened earlier is reused to execute the SQL statement.

For more information about the DataAdapter object, see AsaDataAdapter class.

Error handling    If there is an error when the application attempts to connect to the database, the following code catches the error and displays 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" );
}

Contents Index Tutorial: Using the Table Viewer code sample Developing Applications with the .NET Data Provider