Article
· 11 hr ago 4m read

Connecting C# to InterSystems IRIS via ODBC

For developers building external applications, especially those using familiar technologies like C#ODBC (Open Database Connectivity) is a crucial, standardized bridge to any relational database, including InterSystems IRIS. While InterSystems offers its own native ADO.NET provider, the ODBC driver is often the most straightforward path for integration with generic database tools and frameworks.

Here is a step-by-step guide to getting your C# application connected to an IRIS instance using the ODBC driver, focusing on DSN-less connection string.

Step 1: Install the InterSystems IRIS ODBC Driver

The InterSystems ODBC driver is installed by default when you install InterSystems IRIS on a Windows machine.

  • If IRIS is on the same machine: The driver is already present.
  • If IRIS is on a remote server: You must download and install the standalone ODBC client driver package for your client operating system (Windows, Linux, or macOS) and bitness (32-bit or 64-bit) from WRC website if you're a client or by installing Client components and copying ODBC driver.

Once installed, you can verify its presence in the ODBC Data Source Administrator tool on Windows (look for the InterSystems IRIS ODBC35 driver).

Step 2: Define the DSN-less Connection String

Instead of creating a pre-configured Data Source Name (DSN) in the Windows administrator tool, we’ll use a DSN-less connection string. This is cleaner for deployment because your application carries all necessary connection details.

The format specifies the driver name and the server parameters:

Driver={InterSystems IRIS ODBC35};
server=127.0.0.1;
port=1972;
database=USER;
uid=_System

Note:

  • The Driver Name (InterSystems IRIS ODBC35 or sometimes InterSystems ODBC) must exactly match the name registered in your local ODBC Data Source Administrator.
  • Port is the IRIS Superserver port (often 1972).
  • Database is the target Namespace in InterSystems IRIS (e.g., USER or your custom application namespace).
  • The default UID is _System with the password SYS. Always change these defaults in a production environment.

Step 3: Implement the Connection in C#

In your C# project, you will need to reference the System.Data.Odbc namespace to work with the generic .NET ODBC provider.

Here is a minimal C# example that establishes a connection, executes a simple query against a default table, and displays the result.

using System.Data;
using System.Data.Odbc;

public class IrisOdbcExample
{
    public static void Main()
    {
        // 1. Define the DSN-less connection string
        string connectionString =
            "DRIVER={InterSystems IRIS ODBC35};" +
            "Server=127.0.0.1;Port=1972;Database=USER;" +
            "UID=_System;PWD=SYS;";

        // 2. Define the SQL Query (Example: querying the default Sample.Person table)
        string sql = "SELECT ID, Name FROM Sample.Person WHERE ID < 5";

        using (OdbcConnection connection = new OdbcConnection(connectionString))
        {
            try
            {
                connection.Open();
                Console.WriteLine("Connection successful!");

                using (OdbcCommand command = new OdbcCommand(sql, connection))
                using (OdbcDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Console.WriteLine($"ID: {reader["ID"]}, Name: {reader["Name"]}");
                    }
                }
            }
            catch (OdbcException ex)
            {
                // 3. Handle specific ODBC errors (e.g., wrong password, port blocked)
                Console.WriteLine($"ODBC Error: {ex.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"General Error: {ex.Message}");
            }
            // The connection is automatically closed at
            // the end of the Using block.
        }
    }
}
 
Setting up DSN

Next Steps

This DSN-less approach provides flexibility and avoids client-side configuration bloat. For high-performance C# applications, you may consider the native InterSystems ADO.NET Provider. But for quick integrations and tool compatibility, an ODBC connection is a reliable choice.

❗Always remember to use parameterized queries in production code to prevent SQL injection vulnerabilities.

Discussion (0)1
Log in or sign up to continue