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.
private System.Windows.Forms.Button btnConnect; private System.Windows.Forms.ListBox listEmployees; |
Connecting to the database The btnConnect_Click method declares and initializes an SAConnection connection object.
private void btnConnect_Click(object sender, System.EventArgs e) SAConnection conn = new SAConnection( "Data Source=SQL Anywhere 11 Demo;UID=DBA;PWD=sql" ); |
The SAConnection object uses the connection string to connect to the SQL Anywhere sample database when the Open method is called.
conn.Open(); |
For more information about the SAConnection object, see SAConnection class.
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.
SACommand cmd = new SACommand( "SELECT Surname FROM Employees", conn ); |
For more information about the SACommand object, see SACommand class.
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.
listEmployees.BeginUpdate(); while( reader.Read() ) { listEmployees.Items.Add( reader.GetString( 0 ) ); } listEmployees.EndUpdate(); |
For more information about the SADataReader object, see SADataReader class.
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:
catch( SAException ex ) { MessageBox.Show( ex.Errors[0].Message ); } |
For more information about the SAException object, see SAException class.
Send feedback about this page via email or DocCommentXchange | Copyright © 2008, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.0 |