Contents Index Tutorial: Using the Simple code sample Tutorial: Using the Table Viewer code sample

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

Understanding the Simple 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 Simple code sample. The Simple code sample 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\SimpleWin32\Simple.csproj.

Declaring controls    The following code declares a button named btnConnect and a ListBox named listEmployees.

private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.ListBox listEmployees;

Connecting to the database    The btnConnect_Click method declares and initializes a connection object (new AsaConnection).

private void btnConnect_Click(object sender,
   System.EventArgs e)
   AsaConnection conn = new AsaConnection(
    "Data Source=ASA 9.0 Sample;UID=DBA;PWD=SQL" );

The AsaConnection object uses the connection string to connect to the sample database.

conn.Open();

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

Executing a query    The following code uses the Command object (AsaCommand) to define and execute a SQL statement (SELECT emp_lname FROM employee). Then, it returns the DataReader object (AsaDataReader).

AsaCommand cmd = new AsaCommand(
     "select emp_lname from employee", conn );
AsaDataReader reader = cmd.ExecuteReader();

For more information about the Command object, see AsaCommand class.

Displaying the results    The following code loops through the rows held in the AsaDataReader object and adds them to the ListBox control. The DataReader uses GetString( 0 ) to get the first value from the row.

Each time the Read method is called, the DataReader gets another row back from the result set. A new item is added to the ListBox for each row that is read.

listEmployees.BeginUpdate();
while( reader.Read() ) {
     listEmployees.Items.Add( reader.GetString( 0 ) );
}
listEmployees.EndUpdate();

For more information about the AsaDataReader object, see AsaDataReader class.

Finishing off    The following code at the end of the method closes the reader and connection objects.

reader.Close();
conn.Close();

Error handling    Any errors that occur during execution and that originate with Adaptive Server Anywhere .NET data provider objects are handled by displaying them in a message box. The following code catches the error and displays its message:

catch( AsaException ex ) {
    MessageBox.Show( ex.Errors[0].Message );
}

For more information about the AsaException object, see AsaException class.


Contents Index Tutorial: Using the Simple code sample Tutorial: Using the Table Viewer code sample