Sybase: Access the database with C# using ODBC

I needed to write a very short C# program to access a Sybase ASE database and extract some information.

First had to download the appropriate version of ASE. It contains a directory called \archives\odbc. There is a setup.exe. Just run it.

Now there a new driver available:

create new data sourceThere is no need to add a data source to access ASE from an ODBC connection using C#. Just went there to check whether the driver was properly installed.

Then just create a program connecting to ASE using the following connection string:

Driver={Adaptive Server Enterprise};server=THE_HOSTNAME;port=2055;db=THE_DB_NAME;uid=sa;pwd=THE_SA_PASSWORD;

If you omit the db=… part, you’ll just land in the master database.

With this connection, you can then execute statements.

Here a sample code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            String errorMsg;
            OdbcConnection con = Connect("sa", "sa.pwd", "2055", "192.168.190.200", "mydb", out errorMsg);
            Console.WriteLine(errorMsg);
            if (con != null)
            {
                Console.WriteLine("In database {0}", con.Database);
                OdbcCommand command = con.CreateCommand();
                command.CommandText = "SELECT name FROM sysobjects WHERE type='U' ORDER BY name";
                OdbcDataReader reader = command.ExecuteReader();
                int fCount = reader.FieldCount;
                for (int i = 0; i < fCount; i++)
                {
                    String fName = reader.GetName(i);
                    Console.Write(fName + ":");
                }
                Console.WriteLine();
                while (reader.Read())
                {
                    for (int i = 0; i < fCount; i++)
                    {
                        String col = reader.GetValue(i).ToString();
                        Console.Write(col + ":");
                    }
                    Console.WriteLine();
                }
                reader.Close();
                command.Dispose();
                Close(con);
                Console.WriteLine("Press any key too continue...");
                Console.ReadLine();
            }
        }

        private static OdbcConnection Connect(String strUserName, String strPassword, String strPort, String strHostName, String dbName, out String strErrorMsg)
        {
            OdbcConnection con = null;
            strErrorMsg = String.Empty;
            try
            {
                String conString = "Driver={Adaptive Server Enterprise};server=" + strHostName + ";" + "port=" + strPort + ";db=" + dbName + ";uid=" + strUserName + ";pwd=" + strPassword + ";";
                con = new OdbcConnection(conString);
                con.Open();
            }
            catch (Exception exp)
            {
                con = null;
                strErrorMsg = exp.ToString();
            }

            return con;
        }

        private static void Close(OdbcConnection con)
        {
            con.Close();
        }
    }
}

That was really easy ! Not that it’d have been more difficult with Java or PHP, but I’d have expected to waste ours making mistakes and trying to debug it…

6 thoughts on “Sybase: Access the database with C# using ODBC

  1. Hi,

    Myself Prasad from Bangalore, I am basically from java background.
    .Net(C#) and Sybase is very new for me. I need to write a c# code to connect to Sybase db and pull some data.
    Thank you very much for tour post, but i did not get any information about the ASE driver required for the DB connection.

    1) From where i can download the Sybase ASE ODBC driver for windows (is driver is free)?
    2) In your post you say “First had to download the appropriate version of ASE”, are you talking about driver or some thing else?

    I need only the client related stuffs, DB is already installed in a remote system.
    My program need to connect from client windows box to remote DB.

    quick response is very much appreciated.

    Thanks in advance

    Regards,
    Prasad

  2. Hi,

    Myself Prasad from Bangalore, India. Thank you very much for the post.
    I am from Java background and very much new to c# and Sybase ASE.
    Can you please let me know from where I can download ASE (Sybase ASE ODBC driver for windows)?
    I am trying to connect to remote Sybase DB and pull some data from my windows box.

    1) is Sybase ASE ODBC driver for windows free?
    2) is there any dll for drivers in c# as we get JAR’s in Java?

    Quick response very much appreciated.

    Thanks in advance.

    Regards,
    Prasad

  3. Hi,

    This is in continuation with my previous post.
    I downloaded Sybase Adaptive Server Enterprise (ASE) 15.0.3 Developer’s Edition on Windows from below link
    http://download.sybase.com/eval/1503/ase1503_winx64.zip
    When I extracted the zip, there is no any “\odbc” folder inside “\archives”, as you have explained in the beginning of the post.

    please help.

    Regards,
    Prasad

  4. Hello Henri.

    Nice post.
    I tried to connect to Sybase 9 and had the follow error: “Error [IM002] [Microsoft][ODBC Driver Manager] Data Source Name Not Found And No Default Driver Spec…”

    I have a driver available called “Contabil” and the connection test works fine.
    Any help?

    Thanks in advance.

  5. am trying to hit a remote Sybase database using ODBC. I have written a C# program with the following code

    try
    {
    String conString = “Driver={Adaptive Server Enterprise};server=ipAddress;port=portNumber;db=databaseName;uid=strUserName;pwd=strPassword”;
    con = new OdbcConnection(conString);
    con.Open();
    }
    catch (Exception exp)
    {
    con = null;

    }
    After the connection times out ,the exception message says :- ERROR [08001] [Sybase][ODBC Driver]Client unable to establish a connection

    Can anybody help me in finding out the reason for this exception..?

    1. Try to use dsn=[your dsn name] into your connection string, i was trying to use ‘driver’ in my conection string and ended fail all the time regardless any other conditions already showed up the connection is good,
      And tjen i tried delete driver and use dsn , and it worked!

Leave a Reply

Your email address will not be published. Required fields are marked *