Search code examples
c#databasedb2database-connectiondb2-luw

Connecting to IBM DB2 10.1 from C#


I need to connect to IBM DB2 10.1 from a .Net application. I've started out with a console app to get the connection working as a proof of concept. I am using the IBM.Data.DB2 namespace.

I am getting the following error:

 InvalidOperationException was unhandled: SQL1159  Initialization error with DB2 .NET Data Provider, reason code 2, tokens C:\Users\myname\Desktop\dbConnect\dbConnectApp\dbConnectApp\bin\db2app.dll, , Process Owner: XXXXX\myname

At first I had processor architecture errors. I worked this out by referencing an IBM.Data.DB2 dll from a 32 bit DB2 data driver and then changing the platform execution architecture to x86 in the Configuration Manager in Visual Studio.

Some info:

Visual Studio 2012, .NET 4.5, IBM DB2 10.1 on Windows Server 2008 R2

Example Code:

  static void SelectRow(string myConnectionString)
    { 
        if (myConnectionString == "")
        {
            myConnectionString = "server=MyServerName:60001;Database=mydatabasename;UID=db2admin;PWD=Mypassword1!;";
        }
        DB2Connection myConn = new DB2Connection(myConnectionString);
        string mySelectQuery = "Select * from DB_SCH.USER";
        DB2Command myDB2Command = new DB2Command(mySelectQuery);
        myDB2Command.Connection = myConn;
        myConn.Open();
        myDB2Command.ExecuteNonQuery();
        myConn.Close();
    }

What can be done to fix this error? What are some other ways to connect to Db2 from C#?

Edit: New error after changing to 64 bit version of IBM.Data.Db2 and changing to 64 bit processor architecture in the Visual Studio project:

 SQL1159  Initialization error with DB2 .NET Data Provider, reason code 3, tokens C:\Users\myname\Desktop\dbConnect\dbConnectApp\dbConnectApp\bin\db2app64.dll, , Process Owner: xxxxx\myusername

Solution

  • You are using fixpack 0 of Db2 V10.1 on Windows-Server 2008 R2, all 64-bit.

    That Db2 build pre-dates Visual-Studio-2012 and so you will get issues, until you deploy a build of Db2 that explicitly supports Visual-Studio-2012.

    Your best bet is to upgrade your Db2-server to the final fixpack of V10.1 which is fixpack 6 (at current date), downloadable from IBM Fix Central website. I recommend you download the server fixpack image, and follow the Db2 Knowledge Center steps for pre-installation steps, and post-installation steps. The server-fixpack is installable without an existing installation (it will work for 90 days) which lets you test it out on a dev environment before committing to your server.

    During installation with db2setup GUI you will need to explicitly choose to install the "IBM Database Add-Ins for Visual Studio" component, which gives external plugins that work with Visual-Studio-2012.

    Technically the support in Db2 V10.1 for Visual-Studio-2012 arrived at V10.1 fixpack 2, but if you must go to the trouble of applying a new fixpack you may as well install the final fixpack of the V10.1 tree, simply to obtain some years of bug fixes.

    Another option is to upgrade the version of Db2 to either V10.5 or V11.1 but that will involve a new license, whereas installing a fixpack will re-use your existing license.