Feedback
Did this article resolve your question/issue?

   

Article

Is there any way to connect to the database from C# ?

Information

 
TitleIs there any way to connect to the database from C# ?
URL NameIs-there-any-way-to-connect-to-the-database-from-C
Article Number000112693
EnvironmentProduct: OpenEdge
Version: 10.x, 11.x
OS: All supported platforms
Other: SQL
Question/Problem Description
Is there any way to connect to the database from C# ?
Is there any integration with C#?
Is there any documentation on how to access an OpenEdge database from within a C# routine?
Is there a way to directly connect to an OpenEdge database from C#?
How to connect to an OpenEdge database from C#?
Steps to Reproduce
Clarifying Information
Error Message
Defect/Enhancement Number
Cause
Resolution
The following C# routine shows an example how to connect directly to an OpenEdge database from C#. The sample code show show to connect, create a table, insert, update and select data from the table. 
 
using Console = System.Console;
using System.Data.Odbc;

namespace myodbcOE
{
  class myconnnectionDB
  {
    static void Main(string[] args)
    {
      try
        {
          
          string MyConString = "DRIVER={Progress OpenEdge 11.7 Driver};" +
            "DSN=sports2000;" + 
	    "HOST=localhost;" +
  	    "PORT=<port-number>;" + 
            "DB=sports2000;" +
            "UID=<someuser>;" +
            "PWD=<somepassord>;" + 
	    "OPTION=3";

	  Console.WriteLine("\n " +  MyConString);

	   // Also this format can be used to connect
	   //string MyConString = @"DSN=sports2000;UID=user;PWD=password";
        
          OdbcConnection MyConnection = new OdbcConnection(MyConString);
          MyConnection.Open();

          Console.WriteLine("\n !!! success, connected successfully !!!\n");

          //Display connection information
          Console.WriteLine("Connection Information:");
          Console.WriteLine("\tConnection String:" +
                            MyConnection.ConnectionString);
          Console.WriteLine("\tConnection Timeout:" +
                            MyConnection.ConnectionTimeout);
          Console.WriteLine("\tDatabase:" +
                            MyConnection.Database);
          Console.WriteLine("\tDataSource/Server:" +
                            MyConnection.DataSource);
          Console.WriteLine("\tDriver:" +
                            MyConnection.Driver);
          Console.WriteLine("\tServerVersion:" +
                            MyConnection.ServerVersion);

          //Create a sample table
	 
         // Drop table, but first time will fail since the table doesn't exist
         /* OdbcCommand MyCommand =
            new OdbcCommand("DROP TABLE pub.my_odbc_net",
                            MyConnection);
          MyCommand.ExecuteNonQuery();*/
          

          OdbcCommand MyCommand =  new OdbcCommand("CREATE TABLE pub.my_odbc_net(id int, name varchar(20), idb int)",MyConnection);
          MyCommand.ExecuteNonQuery();

          //Insert
          MyCommand.CommandText =
            "INSERT INTO pub.my_odbc_net VALUES(10,'Some Data', 300)";
          Console.WriteLine("INSERT, Total rows affected:" +
                            MyCommand.ExecuteNonQuery());;

          //Insert
          MyCommand.CommandText =
            "INSERT INTO pub.my_odbc_net VALUES(20,'Progress',400)";
          Console.WriteLine("INSERT, Total rows affected:" +
                            MyCommand.ExecuteNonQuery());

          //Insert
          MyCommand.CommandText =
            "INSERT INTO pub.my_odbc_net VALUES(20,'OpenEdge',500)";
          Console.WriteLine("INSERT, Total rows affected:" +
                            MyCommand.ExecuteNonQuery());

          //Update
          MyCommand.CommandText =
            "UPDATE pub.my_odbc_net SET id=999 WHERE id=20";
          Console.WriteLine("Update, Total rows affected:" +
                            MyCommand.ExecuteNonQuery());

          //COUNT(*)
          MyCommand.CommandText =
            "SELECT COUNT(*) as TRows FROM pub.my_odbc_net";
          Console.WriteLine("Total Rows:" +
                            MyCommand.ExecuteScalar());

          //Fetch
          MyCommand.CommandText = "SELECT * FROM pub.my_odbc_net";
          OdbcDataReader MyDataReader;
          MyDataReader =  MyCommand.ExecuteReader();
          while (MyDataReader.Read())
            {
                Console.WriteLine("Data:" + MyDataReader.GetInt32(0) + " " +
                                  MyDataReader.GetString(1) + " " +
                                  MyDataReader.GetInt32(2));
            }

          //Close all resources
          MyDataReader.Close();
          MyConnection.Close();
        }
      catch (OdbcException MyOdbcException) //Catch any ODBC exception ..
        {
          for (int i=0; i < MyOdbcException.Errors.Count; i++)
            {
              Console.Write("ERROR #" + i + "\n" +
                            "Message: " +
                            MyOdbcException.Errors[i].Message + "\n" +
                            "Native: " +
                            MyOdbcException.Errors[i].NativeError.ToString() + "\n" +
                            "Source: " +
                            MyOdbcException.Errors[i].Source + "\n" +
                            "SQL: " +
                            MyOdbcException.Errors[i].SQLState + "\n");
            }
        }
    }
  }
}



* Replace the values (host, port, user and password) for the connection according to the installation. 
* OpenEdge database server must be configured to listening for SQL connections

Workaround
Notes
Last Modified Date1/16/2019 5:25 PM
Attachment 
Disclaimer The origins of the information on this site may be internal or external to Progress Software Corporation (“Progress”). Progress Software Corporation makes all reasonable efforts to verify this information. However, the information provided is for your information only. Progress Software Corporation makes no explicit or implied claims to the validity of this information.

Any sample code provided on this site is not supported under any Progress support program or service. The sample code is provided on an "AS IS" basis. Progress makes no warranties, express or implied, and disclaims all implied warranties including, without limitation, the implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample code is borne by the user. In no event shall Progress, its employees, or anyone else involved in the creation, production, or delivery of the code be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample code, even if Progress has been advised of the possibility of such damages.