Implementation of C connection to db2 database

  • 2020-05-12 03:08:57
  • OfStack

Drive through OLE DB for DB2

string strSql = @"select phone_no from no_store where id<5";
            string strConn = "Provider=IBMDADB2;Data Source= The database name ;UID= The user name ;PWD= password ;";
            using (OleDbConnection conn = new OleDbConnection(strConn))
            {
                OleDbCommand cmd = new OleDbCommand(strSql, conn);
                try
                {
                    conn.Open();
                    OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    adp.Fill(ds);
                    DataTable dt = ds.Tables[0];
                    if (dt != null)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            Console.WriteLine(" The phone " + i + ":" + dt.Rows[i][0].ToString());
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            Console.Read();

Provided by IBM IBM. data. DB2. DLL

string strSql = @"select phone_no from no_store where id<5";
            string strConn = "Database= The database name ;UID= The user name ;PWD= password ;";
            using (DB2Connection conn = new DB2Connection(strConn))
            {
                DB2Command cmd = new DB2Command(strSql, conn);
                try
                {
                    conn.Open();
                    DB2DataAdapter adp = new DB2DataAdapter(cmd);
                    DataSet ds = new DataSet();
                    adp.Fill(ds);
                    DataTable dt = ds.Tables[0];
                    if (dt != null)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            Console.WriteLine(" The phone " + i + ":" + dt.Rows[i][0].ToString());
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            Console.Read();

summary
(1) the database operation objects of the two ways can refer to c# to connect the database objects of sqlserver.
(2) if the db2 database is on the remote server, the database name, user name and password in the connection string are cataloged into the local database name, user name and password for db2.
(3) to use IBM.Data.DB2, the assembly must be referenced.

Related articles: