I have a DB2 expresss in my machine and I am able to query from the database using command window (after following two commands):
set DB2INSTANCE=db2inst1
db2 connect to tims user
Now, when I try to connect to the database from a C# console application, I am getting following errors with different connection strings.
Attempt 1
string connectionString = @"Provider = IBMDADB2; Database = TIMS; Hostname = localhost; CurrentSchema=db2inst1; ";
SQL1032N No start database manager command was issued. SQLSTATE=57019
Attempt 2
string connectionString = @"Provider = IBMDADB2; Database = TIMS; CurrentSchema=db2inst1; ";
SQL1031N The database directory cannot be found on the indicated file system. SQLSTATE=58031
What should be the correct connection string for this scenario?
CODE
string connectionString = @"Provider = IBMDADB2; Database = TIMS; Hostname = localhost; CurrentSchema=db2inst1; ";
OleDbConnection myConnection = new OleDbConnection();
myConnection.ConnectionString = connectionString;
myConnection.Open();
Do you have multiple DB2 instances running on your machine? You can get a list of instances that exist by executing the
db2ilist
command.If you have to execute the
set DB2INSTANCE=db2inst1
statement when you open a DB2 Command Window in order to connect to theTIMS
database with thedb2 connect to TIMS
command, then you need to ensure that the environment for your C# application is configured the same way.You can do this in a number of ways:
by setting the DB2INSTANCE environment variable before starting your application
Change the default DB2 instance on your machine by using the command
db2set -g DB2INSTDEF=db2inst1
(** see note below)Use a TCPIP connection string (as described by @Bhaarat) so that your application does not depend on the database catalog for the default instance
Note: Before changing
DB2INSTDEF
you may want to see what the current value is, by executing the commanddb2set -all
and looking forDB2INSTDEF
in the output. Also note that changing the default instance may affect other applications that run on your machine.My DB2 insatnce name is "db2inst1" and it was working fine when I used DB2 command window.
Now I made following settings and it is working fine now. :-)
ConnectionString
CODE
Note: Try to create DSN for ODBC and use ODBC connection in a sample SSIS package. This will help in resolving OLEDB connection issues also (that are common to both)
refer this url http://www.c-sharpcorner.com/uploadfile/nipuntomar/connection-strings-for-ibm-db2/
your connection string should be something like this
in more you can refer this too http://www.codeproject.com/Articles/4870/Connect-to-DB2-from-Microsoft-NET