简体   繁体   English

从C#连接到IBM DB2 10.1

[英]Connecting to IBM DB2 10.1 from C#

I need to connect to IBM DB2 10.1 from a .Net application. 我需要从.Net应用程序连接到IBM DB2 10.1。 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. 我正在使用IBM.Data.DB2命名空间。

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. 我通过从32位DB2数据驱动程序引用IBM.Data.DB2 dll,然后在Visual Studio的Configuration Manager中将平台执行体系结构更改为x86来解决此问题。

Some info: 一些信息:

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

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#? 从C#连接到Db2还有哪些其他方法?

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: 编辑:更改为64位版本的IBM.Data.Db2并更改为Visual Studio项目中的64位处理器体系结构后,出现新错误:

 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

You are using fixpack 0 of Db2 V10.1 on Windows-Server 2008 R2, all 64-bit. 您正在所有64位Windows Server 2008 R2上使用Db2 V10.1的修订包0。

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. 该Db2版本早于Visual-Studio-2012,因此您将遇到问题,直到部署了明确支持Visual-Studio-2012的Db2版本为止。

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. 最好的选择是将Db2服务器升级到V10.1的最终修订包,即修订包6(当前日期),可从IBM Fix Central网站下载。 I recommend you download the server fixpack image, and follow the Db2 Knowledge Center steps for pre-installation steps, and post-installation steps. 我建议您下载服务器修订包映像,并按照Db2知识中心步骤进行安装前和安装后的步骤。 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. server-fixpack无需安装即可安装(有效期为90天),可在提交给服务器之前在开发环境中对其进行测试。

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. 在使用db2setup GUI进行安装期间,您将需要明确选择安装“用于Visual Studio的IBM数据库插件”组件,该组件提供了可与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. 从技术上讲,Visual Studio-2012的Db2 V10.1支持是V10.1修订包2,但是如果您必须麻烦应用新的修订包,则最好安装V10.1树的最终修订包,只是为了获得几年的错误修复。

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. 另一种选择是将Db2的版本升级到V10.5或V11.1,但这将涉及新的许可证,而安装修订包将重新使用您的现有许可证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM