Using Dapper with a Progress OpenEdge database

This is part 1 in a two-part series of blog posts about using Dapper with Progress OpenEdge.

Part 2 (coming soon) talks about using Dapper.Contrib with OpenEdge.

What is Dapper?

Dapper is a lightweight, open-source ORM for the .NET Framework that provides extension methods for the IDbConnection interface.

It allows you to easily execute SQL statements and map the results to objects, and more.

Example:

public class Dog
{
    public int Id { get; set; }
    public int? Age { get; set; }
    public string Name { get; set; }
    public float? Weight { get; set; }
}

var dogs = connection.Query<Dog>("select * from dbo.Dog");

Using the Progress OpenEdge ODBC Driver

To connect to the OpenEdge database from .NET I am using the Progress OpenEdge 11.6 ODBC Driver which is included in Progress OpenEdge 11.6. This is a 32-bit only driver which means we must open the 32-bit ODBC Data Source Administration console within Windows.

Adding an ODBC System DSN

To use the ODBC driver we need to create a System Data Source. The first step is to select the driver we want to use.

Next, I enter in my data source name, description, and database connection settings.

Ensure you use the port number for the OpenEdge SQL broker, not the 4GL broker

Once the ODBC Data Source is created you can now use it from .NET using the Data Source Name from the previous step. Here I am selecting all records from the Dogs table and printing the Ids.

using (var connection = new OdbcConnection("DSN=My OpenEdge DB;Pwd=mypassword"))
{
    connection.Open();

    var command = connection.CreateCommand();
    command.CommandText = "SELECT * FROM pub.Dogs";

    var reader = command.ExecuteReader();

    while (reader.Read())
    {
        Console.WriteLine(reader.GetInt32(0));
    }
}

Wiring it all up

Because OdbcConnection implements the IDbConnection interface we can use it directly with Dapper.

using (var connection = new OdbcConnection("DSN=My OpenEdge DB;Pwd=mypassword"))
{
    connection.Open();

    var dogs = connection.Query<Dog>("SELECT * FROM pub.Dogs");
}

I have now selected all records from the Dogs table and used them to populate a List<Dog>.

Here’s the output in LINQPad:

This code can now be extended to query the database for different records, and more complex SQL queries.

Part 2 (coming soon) talks about using Dapper.Contrib to dynamically generate SQL statements instead of having to hand-craft them.

64 Replies to “Using Dapper with a Progress OpenEdge database”

  1. Hello would you mind letting me know which webhost you’re
    utilizing? I’ve loaded your blog in 3 completely different browsers and I must say this blog loads a lot quicker
    then most. Can you recommend a good web hosting provider at a fair price?
    Kudos, I appreciate it!

  2. I loved as much as you will receive carried out right
    here. The sketch is tasteful, your authored subject matter stylish.
    nonetheless, you command get got an edginess over that you wish be delivering the following.
    unwell unquestionably come more formerly again since exactly the same nearly very often inside case you shield this hike.

  3. I would like to get across my gratitude for your kind-heartedness supporting women who really need help on in this niche. Your personal commitment to getting the solution all-around became quite beneficial and have continuously enabled women just like me to realize their aims. Your personal valuable help and advice can mean so much to me and far more to my office workers. Regards; from all of us.

Leave a Reply

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