Connecting to Remote Sybase SQL Anywhere from C#

I recently had to complete a small project that required connecting to a remote Sybase SQL Anywhere 11 instance using C#. The first challenge was to find the correct Sybase SQL Anywhere 11 ODBC connection string.

The result of Googling for “sql anywhere odbc connection string” gives several possibilities.

The one which worked, provided options for a default local engine or remote engine that is not running.   Even though neither applied, together with some colleague who deserve titles as local Sybase gurus, we were able to determine the following connection string:

Driver={SQL Anywhere 11};DatabaseName=my_db_name;EngineName=my_server_name;uid=username;pwd=password;LINKs=tcpip(host=host_ip_address)

After installing SQL Anywhere 11, and ensuring that there is a valid SQL Anywhere 11 ODBC driver as shown below

sybase-odbc-driver

I was able to connect using the following C# code:


var connectionString = @"Driver={SQL Anywhere 11};DatabaseName=my_db_name;EngineName=my_server_name;uid=username;pwd=password;LINKs=tcpip(host=host_ip_address)";
 using (var cn = new OdbcConnection(connectionString))
 {
 var cmd = new OdbcCommand("select * from dbo.users", cn);
 cn.Open();

 using (var reader = cmd.ExecuteReader())
 {
 while (reader.Read())
 {
    for (var i = 0; i < reader.FieldCount; i++)
    {
      // option here is to read a name of the column
     Console.WriteLine(reader[i]);

    }
   }
 }
  cn.Close();
}

to query database and grab some objects from the table.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s