C#DB2 development issues in mind

Source: Internet
Author: User
Tags db2 ole



Recently the company has a small tool need to use the DB2 database, has not played before DB2, think it should be easy to achieve.



This gadget was first used with NHibernate to connect db2,nhibernate. It's a shame ...



For the first time with DB2, a half-day data said that DB2 control Center has a UI can input SQL command, the result: The control center is not connected. Always error.



Of course, I didn't know what was going on at first. Thought I had a problem with the DB2 database. Actually, No. Yes, I didn't. Configure the hub to configure its IP, resulting in the inability to connect.



First use of the students can search the internet when the control center is not connected to the Times wrong information (at the beginning of the heart is a crash), there will tell you how to configure the article.



The Control Center connection database interface is as follows. The place to play is where the SQL commands are executed.






After online query data, nhibernate connection DB2 usage, what mapping file to compile into embedded resources, configuration file to add what


Finally found the connection DB2, to use IBM.Data.DB2.dll. At first I installed the DB2 are 64 bit, with NHibernate connection DB2, run the program, unexpectedly reported format mismatch (anyway is the wrong number of digits.) Should use a 32-bit DLL). Remember that the company program is compiled to x86, so I re-installed DB2 X86 version, and then reference is X86 IBM.Data.DB2.dll.



Then continue to run the program, the program also error: System.InvalidOperationException:SQL1159 initialization error with DB2. NET Data Provider, Reason code 2, Tokens C:\Users\Administrator\Desktop\Test\Test\bin\db2app.dll



The online search solution is to find the Db2app.dll and then copy it to the bin directory. Because it cannot be added to the GAC, nor can it be referenced.



After copying Db2app.dll to the bin directory, a run, hey, the program is OK.



Then prepare for the release process to QA. Then the problem arises again.



Db2app.dll must be in the Bin directory, but also can not change the bin folder name, a change and error. Example C:\bin\ I wrote the program folder (specific EXE in this area) Db2ap.dll.



As a coder this makes me feel that this directory structure is too ugly, where EXE must also be in the bin directory.



And then try in Baidu, Google search, a day passed,,, or did not find countermeasures.



During this period I found that if I used OLE DB to connect DB2, I didn't need db2app.dll at all. Because the manager said to use NHibernate to connect, I can not easily change the way to achieve.



At the end of the hour to the manager said, this if you want to keep the bin directory, the program is finished, if not retained, now only to OLE DB mode to process, the middle of the operation of SQL I write myself.



Because the tools are small, SQL is not much. With the manager's approval, I started using OLE DB.



I used to use OLE DB, and found a connection string on the Internet and wrote it in the configuration file. Then start writing code:



connectionstring= "Provider=ibmdadb2;database=192.168.0.5:50000;data source=mydb; Pwd=guosange; Uid=guosange



Soon, the program is finished, and then the test begins. Want to test me to change the IP, read the newspaper what is wrong.



Results at random to get rid of IP, or even delete the IP, the program is not error, normal operation. After another period of testing, analysis. found that I had to connect the DB2 in the DB2 Control center, and the connection string datasource must be the database nickname ... Otherwise, not even.



Base difference Ah ... has been used to connect the database with ADO, thought this OLE DB is also ADO, the result,, (so all the students, to climb high, the foundation to fight AH)



It is worth mentioning that this OLE DB connection database, IBM. Data.DB2.dll can also not.






This, it makes me embarrassed again ... Let the customer use this tool, you have to let him first use the control center to connect to the database, do a nickname, and then go to the configuration file to change the configuration? That's not going to work.



Then I search the ADO connection again, found that the original IBM.Data.DB2.dll this DLL is used for ADO connection ... And then reference IBM.Data.DB2.dll, modify the code, compile, run, error.



The cause of the error is still the bin directory without Db2app.dll (previously changed to OLE DB has been deleted).






I am again melancholy ... Do you have to use OLE DB, and try again for a long time, still can't solve ... The heart has been scolded, with what DB2 ....



After a strong ideological struggle, feel, feel the solution hopeless, think or use OLE DB forget. After all, it looks a lot better than a bin directory.



Compile, publish, complete.






By the way open a 2008, see online a lot with DB2 program are VS2005 and. NET 2.0 combination write, my machine minimum version is VS2008, will use it.



Build a solution, select the. NET version of 2.0, reference IBM.Data.DB2.dll, write a segment of ADO connection DB2 code


 string connectionString = "Server=192.168.0.5:50000;DataBase=mydb;UID=guosange;PWD=guosange"; string commandtext = "select count(*) from testTable"; using (IBM.Data.DB2.DB2Connection connection = new IBM.Data.DB2.DB2Connection(connectionString))
            {
                connection.Open();
                IBM.Data.DB2.DB2Command command = new IBM.Data.DB2.DB2Command(commandtext, connection);
                IBM.Data.DB2.DB2DataAdapter adapter = new IBM.Data.DB2.DB2DataAdapter(command);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                connection.Close();
            }



Perform...  Still prompt need db2app.dll. Feel hopeless.






Before you close, think about quoting a 64-bit IBM.Data.DB2.dll try and see what's wrong with the newspaper. Compile, execute: Results.. The TND actually succeeded.



My program has been compiled to x86 .... My operating system for Win7 x64 ... But experience tells me it should be an error. But in fact there is no error ... I was confused.






Cause I really don't know ... But the result is this ...



Then hurriedly modify the code, instead ADO connect DB2, compile, build, test. Release..



In order to verify, I specially installed a virtual machine, installed on 32-bit XP, installed. net2.0 operation error.



System.TypeLoadException:Could not load type ' system.datetimeoffset ' from assembly ' mscorlib, version=2.0.0.0, culture= Neutral, publickeytoken=b77a5c561934e089 '.






WORKAROUND: You will need to hit the Microsoft. NET Framework 2.0 Service Pack 1 (x86) Patch:






Http://www.microsoft.com/downloads/zh-cn/details.aspx?familyid=79bc3b77-e02c-4ad3-aacf-a7633f706ba5








After patching, it runs normally.



Quickly get the code out, modify, compile, build, test. Release.






Summary: ADO connection DB2, need IBM.Data.DB2.dll. OLE DB does not require: However, you configure the database instance connection for the control center.



If error System.InvalidOperationException:SQL1159 initialization error with DB2. NET Data Provider, Reason code 2



There may be a lack of db2app.dll. Copy to the bin directory.



For procedural reasons, please choose the IBM.Data.DB2.dll version. The problem must be more information, multi-test.



C#DB2 development issues in mind


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.