To connect to a database, an SAConnection object must be created. The connection string can be specified when creating the object or it can be established later by setting the ConnectionString property.
A well-designed application should handle any errors that occur when attempting to connect to a database.
A connection to the database is created when the connection is opened and released when the connection is closed.
The following C# code creates a button click handler that opens a connection to the SAP Sybase IQ sample database and then closes it. An exception handler is included.
private void button1_Click(object sender, EventArgs e) { SAConnection conn = new SAConnection("Data Source=Sybase IQ Demo"); try { conn.Open(); conn.Close(); } catch (SAException ex) { MessageBox.Show(ex.Errors[0].Source + " : " + ex.Errors[0].Message + " (" + ex.Errors[0].NativeError.ToString() + ")", "Failed to connect"); } }
The following Visual Basic code creates a button click handler that opens a connection to the SAP Sybase IQ sample database and then closes it. An exception handler is included.
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim conn As New SAConnection("Data Source=Sybase IQ Demo") Try conn.Open() conn.Close() Catch ex As SAException MessageBox.Show(ex.Errors(0).Source & " : " & _ ex.Errors(0).Message & " (" & _ ex.Errors(0).NativeError.ToString() & ")", _ "Failed to connect") End Try End Sub