No start database manager command was issued error

2019-08-04 08:27发布

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();

标签: c# .net db2
3条回答
你好瞎i
2楼-- · 2019-08-04 08:29

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 the TIMS database with the db2 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 command db2set -all and looking for DB2INSTDEF in the output. Also note that changing the default instance may affect other applications that run on your machine.

查看更多
Melony?
3楼-- · 2019-08-04 08:40

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. :-)

  1. Created a port in the C:\Windows\System32\drivers\etc\services file (db2c_db2inst1 50010/tcp)
  2. Set the “TCP/IP Service Name” ( = db2c_db2inst1”) for the instance. Verified using “db2 get dbm cfg” command
  3. Updated the environment variable DB2INSTANCE with the value “db2inst1”
  4. Restarted the machine
  5. Started the instance

ConnectionString

"Provider = IBMDADB2; Database = TIMS; Hostname = localhost; Protocol = TCPIP; Port = 50010; Uid = myUserID; Pwd = myPassword";

CODE

        string queryString = "SELECT * FROM DBATABC";
        try
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                OleDbCommand command = new OleDbCommand(queryString, connection);
                connection.Open();
                OleDbDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {

                        if (!reader.IsDBNull(0))
                        {
                            string companyCode = reader.GetString(0).ToString();
                        }
                    }
                }
                reader.Close();
            }
        }

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)

查看更多
Anthone
4楼-- · 2019-08-04 08:41

refer this url http://www.c-sharpcorner.com/uploadfile/nipuntomar/connection-strings-for-ibm-db2/

your connection string should be something like this

 Provider=IBMDADB2;Database=urDataBase;Hostname=urServerAddress;Protocol=TCPIP;Port=50000;
Uid=urUsername;Pwd=urPassword;

in more you can refer this too http://www.codeproject.com/Articles/4870/Connect-to-DB2-from-Microsoft-NET

查看更多
登录 后发表回答