Understanding the Simple sample project

This section illustrates some key features of the SQL Anywhere .NET Data Provider by walking through some of the code from
the Simple code sample. The Simple code sample uses the SQL Anywhere sample database, demo.db, which is held in your SQL Anywhere samples directory.

For information about the location of the SQL Anywhere samples directory, see Samples directory.

For information about the sample database, including the tables in the database and the relationships between them, see SQL Anywhere 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 all the
code, open the sample project in samples-dir\SQLAnywhere\ADO.NET\SimpleWin32.

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

Defining a query A SQL statement is executed using an SACommand object. The following code declares and creates a command object using the
SACommand constructor. This constructor accepts a string representing the query to be executed, along with the SAConnection
object that represents the connection that the query is executed on.

Displaying the results The results of the query are obtained using an SADataReader object. The following code declares and creates an SADataReader
object using the ExecuteReader constructor. This constructor is a member of the SACommand object, cmd, that was declared previously.
ExecuteReader sends the command text to the connection for execution and builds an SADataReader.

SADataReader reader = cmd.ExecuteReader();

The following code loops through the rows held in the SADataReader object and adds them to the listbox control. Each time
the Read method is called, the data reader gets another row back from the result set. A new item is added to the listbox for
each row that is read. The data reader uses the GetString method with an argument of 0 to get the first column from the result
set row.

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

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

Error handling Any errors that occur during execution and that originate with SQL Anywhere .NET Data Provider objects are handled by displaying
them in a window. The following code catches the error and displays its message: