How to Connect to IBM Db2 in .NET With C#

Db2 is a cross-platform database management system (DBMS) developed by IBM. It provides a robust relational database engine and integrated support for certain NoSQL features, making Db2 a powerful and flexible solution.

Db2 remains a popular choice in modern software development thanks to its strong analytical and transactional capabilities. However, flawless connectivity between the database and the application is critical, and this is where modern data providers from the dotConnect product line come into play.

In this article, we will explore how to connect to this platform using dotConnect for Db2, covering different types of connections.

Why choose dotConnect for Db2?

dotConnect for Db2 is a powerful data provider designed for all Db2-related operations. With its on-the-fly connector creation, flexible configuration, seamless integration with Visual Studio, and advanced ORM support, dotConnect for Db2 improves functionality, boosts performance, and simplifies the daily work of .NET developers working with Db2.

Download and activate dotConnect for Db2

30-day free trial version

Download and install dotConnect for Db2 directly on your machine, or install the Devart.Data.Db2 NuGet package.

To activate a 30-day trial, use the license key assigned to you after completing the registration on the Devart website.

Full version

After purchasing the full version, go to your profile's Licenses page. Choose your product and click Details. Here, you'll find the license details and the Activation Key.

License details and the activation key

To activate a connection in your application, add the License Key to your connection string.

Connect using SSL/TLS

You can establish an SSL connection to a Db2 database using the Devart.Data.Db2 library with the specified connection string parameters. Modify your code as follows:

static void Main(string[] args) {
  // Define your connection string with SSL parameters
  string connectionString = "" +
    "Server=127.0.0.1;" +
    "User Id=TestUser;" +
    "Password=TestPassword;" +
    "Database=TestDB;" +
    "Security=SSL;" +
    "SSLClientKeystash=path/to/your/keystash.sth;" +
    "SSLClientKeystoredb=path/to/your/keystore.kdb;" +
    "License Key=**********;";

  // Create a new Db2Connection object
  using(Db2Connection connection = new Db2Connection(connectionString)) {
    try {
      // Open the connection
      connection.Open();
      Console.WriteLine("Connection successful!");
    } catch (Db2Exception ex) {
      // Handle any errors that may have occurred
      Console.WriteLine($"Error: {ex.Message}");
    }
  }
}

SSL parameters:

  • Security=SSL; - defines using SSL for security.
  • SSLClientKeystash - defines the path to the SSL stash (.sth) file used for the SSL connection. This file contains the SSL certificate and key information.
  • SSLClientKeystoredb - defines the path to the SSL key database file used for the SSL connection. This file stores SSL certificates and keys.

Connect using Server Explorer

In the Server Explorer pane, right-click Data Connections and select Add Connection from the menu.

Add a new connection

The Choose Data Source dialog opens. Select dotConnect for Db2 from the list of installed data providers. Click Continue.

Choose Data Source

In the Add Connection dialog, fill in the necessary connection properties. Click Test Connection to verify that all the settings are correct, and you can connect to the Db2 database.

Provide connection details for the Db2 database

After that, expand the newly created data connection to view the database structure. You can view the list of tables in the database and columns in tables.

To retrieve data from some particular table, right-click its name and select Retrieve Data from Table. This action opens a new query window with the SELECT statement for fetching data from the table.

Retrieve data from the Db2 table

Connect with EF Core using Entity Developer

To create an EF Core model from the Db2 database, use the visual ORM builder - Entity Developer that allows you to visually design and generate EF Core models, making database application development faster, easier, and more efficient.

If you don't have it already installed, close your Visual Studio instance, download Entity Developer, and install it following the on-screen instructions.

Follow the detailed illustrated guide to create your database model using Entity Developer. When this process is complete, the model you created opens. Check this model and context classes.

View the created model

Connect with EF Core using Scaffold-DbContext

Follow the instructions below to connect to a Db2 database using EF Core and scaffold the database context. This process will generate the EF Core model classes and DbContext based on your existing Db2 database schema.

Install the required NuGet packages:

Go to Tools > NuGet Package Manager > Package Manager Console. Run the below command to scaffold the DbContext and entity classes from your Db2 database. Make sure to replace the connection string with your actual connection details.

Scaffold-DbContext "Server=127.0.0.1;User Id=TestUser;Password=TestPassword;Database=TestDB;License Key=your_license_key;"
Devart.Data.Db2.Entity.EFCore -OutputDir Models

Notice that the -OutputDir Models parameter specifies the output directory to which the generated model classes are placed (in this case, it is Models). After running the scaffold command, EF Core will generate the DbContext class and entity classes in the specified output directory.

Review the generated code to ensure it matches your database schema.

Conclusion

Given all the benefits, it is no surprise that Db2 is a popular choice among .NET developers. However, one of the key challenges is maintaining connectivity between the database and the application. This challenge is effectively addressed by dotConnect for Db2 by Devart. This user-friendly and reliable solution ensures easy connection to the data source from a .NET application on any operating system.

You can try dotConnect for Db2 in your projects with a fully functional 30-day free trial. Explore its capabilities in your daily workflows and see how it enhances your application development.

Try the 30-day trial of the full product. No limits. No card required Start free trial