Understanding the Simple sample project

This section illustrates some key features of ASE ADO.NET Data Provider by walking through some of the code from the Simple code sample, which uses the ASE sample database, pubs2. See the Adaptive Server Enterprise Installation Guide to find out how to install the pubs2 database.

This section describes portions of the code. To see all of the code, open the sample project:

For C#:

<install dir>\Samples\CSharp\Simple\Simple.csproj

For Visual Basic .NET:

<install dir>\Samples\VB.NET\Simple\Simple.vbproj

Declaring imports

At the beginning of the program, it declares the import statement to import ASE ADO.NET Data Provider information:

For C#:

using Sybase.Data.AseClient;

For Visual Basic .NET:

Imports Sybase.Data.AseClient

Connecting to the database

The btnConnect_Click method declares and initializes a connection object called new AseConnection:

For C#:

AseConnection conn = new AseConnection(
   "Data Source='" + host + 
      "';Port='" + port + 
      "';UID='" + user + 
      "';PWD='" + pass + 
      "';Database='pubs2';" );

For Visual Basic .NET:

Dim conn As New AseConnection( _
     "Data Source='" + host + _
     "';Port='" + port + _
     "';UID='" + user + _
     "';PWD='" + pass + _
     "';Database='pubs2';")

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

For C#:

conn.Open();

For Visual Basic .NET:

conn.Open()

For more information about the AseConnection object, see “AseConnection class”.

Executing a query

The following code uses the Command object (AseCommand) to define and execute a SQL statement. Then, it returns the DataReader object (AseDataReader):

For C#:

AseCommand cmd = new AseCommand( "select au_lname from 
     authors", conn );
AseDataReader reader = cmd.ExecuteReader();

For Visual Basic .NET:

Dim cmd As New AseCommand( _
     "select au_lname from authors", conn)
Dim reader As AseDataReader  = cmd.ExecuteReader()

For more information about the Command object, see “AseCommand class”.

Displaying the results

The following code loops through the rows held in the AseDataReader 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:

For C#:

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

For Visual Basic .NET:

listAuthors.BeginUpdate()
While reader.Read()
     listAuthors.Items.Add(reader.GetString(0))
End While
listAuthors.EndUpdate()

For more information about the AseDataReader object, see “AseDataReader class”.

Finishing off

The following code at the end of the method closes the reader and connection objects:

For C#:

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

For Visual Basic .NET:

reader.Close()
conn.Close()

Error handling

Any errors that occur during execution and that originate with ASE ADO.NET Data Provider objects are displayed in a message box. The following code catches the error and displays its message:

For C#:

catch( AseException ex ) { 	
     MessageBox.Show( ex.Message ); 	
}

For Visual Basic .NET:

Catch ex As AseException
     MessageBox.Show(ex.Message)
End Try

For more information about the AseException object, see “AseException class”.