using System;

namespace IfxOdbcOledb
{
    class Class1
    {
        const String OdbcConStr = "DSN=dbl;PWD=Blue4You";

        const String OledbConStr = "Provider=\"Ifxoledbc.2\";User ID=gbasedbt;"+
            "Data Source=\"dbl@ids940uc2\";Extended Prop" +
            "erties=;Persist Security Info=True;Password=Blue4You";

        [STAThread]
        static void Main(string[] args)
        {
            MySetupSchema();
            MyDisplayData();
        }

  
        static void MySetupSchema()
        {
            int rc=0;

            System.Data.Odbc.OdbcConnection MyConn1 = null;
            System.Data.Odbc.OdbcCommand    MyCmd1  = null;

            MyConn1 = new System.Data.Odbc.OdbcConnection();
            MyConn1.ConnectionString = OdbcConStr;
            try
            {
                MyConn1.Open();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine( ex.Message );
            }


            MyCmd1  = new System.Data.Odbc.OdbcCommand();
            MyCmd1.Connection = MyConn1;

            MyCmd1.CommandText = "drop table customer";
            rc = MyExecuteNonQuery1( MyCmd1 );

      
            MyCmd1.CommandText =  "create table customer( customer_num integer," +
                "fname char(30), lname char(30), primary key (customer_num) )";
            rc = MyExecuteNonQuery1( MyCmd1 );

            MyCmd1.CommandText = "INSERT INTO customer VALUES ( 101, 'a1a', 'b1b' )";
            rc = MyExecuteNonQuery1( MyCmd1 );

            MyCmd1.CommandText = "INSERT INTO customer VALUES ( 102, 'a2a', 'b2b' )";
            rc = MyExecuteNonQuery1( MyCmd1 );

            MyCmd1.CommandText = "INSERT INTO customer VALUES ( 103, 'a3a', 'b3b' )";
            rc = MyExecuteNonQuery1( MyCmd1 );

            MyCmd1.CommandText = "INSERT INTO customer VALUES ( 104, 'a4a', 'b4b' )";
            rc = MyExecuteNonQuery1( MyCmd1 );

            MyCmd1.CommandText = "INSERT INTO customer VALUES ( 105, 'a5a', 'b5b' )";
            rc = MyExecuteNonQuery1( MyCmd1 );

            // Close the database connection
            MyConn1.Close();
        }

        static int MyExecuteNonQuery1( System.Data.Odbc.OdbcCommand OdbcCmd )
        {
            int rc = 0;
            try
            {
                rc = OdbcCmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine( ex.Message );
            }
            return(rc);
        }

        static void MyDisplayData()
        {
            System.Data.OleDb.OleDbConnection MyConn2 = null;
            System.Data.OleDb.OleDbCommand MyCmd2 = null;

            MyConn2 = new System.Data.OleDb.OleDbConnection();
            MyConn2.ConnectionString = OledbConStr;
            try
            {
                MyConn2.Open();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine( ex.Message );
            }

            MyCmd2  = new System.Data.OleDb.OleDbCommand();
            MyCmd2.Connection = MyConn2;

            try
            {
                MyCmd2.CommandText = "SELECT customer_num, fname, lname FROM customer";
                System.Data.OleDb.OleDbDataReader MyReader = MyCmd2.ExecuteReader();

                // Read before accessing data.
                while ( MyReader.Read() ) 
                {
                    String s =  MyReader.GetInt32(0) + ", " + 
                                MyReader.GetString(1) + ", " + 
                                MyReader.GetString(2); 

                    Console.WriteLine( s );
                }

                // Close Data Reader
                MyReader.Close();

            }
            catch (Exception ex)
            {
                System.Console.WriteLine( ex.Message );
            }

            // Close the database connection
            MyConn2.Close();
        }
    }
}
