Understand Simple Sample Project

Some key features of Adaptive Server ADO.NET Data Provider are illustrated through some of the code from the Simple code sample, which uses the Adaptive Server sample database, pubs2.

See the Adaptive Server Enterprise Installation Guide to find out how to install the pubs2 database.

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 Adaptive Server 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()

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()

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()

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 Adaptive Server 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
Related concepts
AseConnection Class
AseCommand Class
AseDataReader Class
AseException Class