编程使用c#连接到IBM db2的两种方式

作者: 流浪*
发布时间:2015-07-15 10:35:20

一:使用c#通过odbc连接到IBM db2
使用 ConnectionString 属性连接到各种数据源。

部署:只要在客户端安装IBM DB2 ODBC driver。配置DSn即可。

1):可以单独下载DB2 Run-Time Client,大约(86.6m),安装后则odbc驱动程序安装成功。下载地址:ftp://ftp.software.ibm.com/ps/products/db2/fixes2/english-us/db2winIA32v8/fixpak/FP17a_WR21440/FP17a_WR21440_RTCL.exe
2):也可以直接安装ibm db2数据库后,该驱动程序自动安装。下载地址http://www6.software.ibm.com/sdfdl/v2/regs2/db2pmopn/Express-C/DB2ExpressC9/Xa.2/Xb.AjZr_0m973fVBNZX63eCwwrnyaoISX2bvOX3Ehc/Xc.db2exc_952_WIN_x86.zip/Xd./Xf.LPr.D1vk/Xg.4854742/Xi.swg-db2expresscviper2/XY.regsrvs/XZ.C7tZ8b_Fn0_ruc3stDVpbwY2QpI/db2exc_952_WIN_x86.zip

测试通过
1:dsn
2:driver

代码:
        private const string dsn = "DSN=OutBound";
        private const string conn = "Driver={IBM DB2 ODBC DRIVER};Database=sample;hostname=192.168.1.46;port=50000;protocol=TCPIP; uid=admin; pwd=admin";




        private void button1_Click(object sender, EventArgs e)
        {
 //         OdbcDataReader reader = ExecuteReader(dsn, "select * from admin.vi_cs_precalloutcust");
            OdbcDataReader reader = ExecuteReader(conn, "select * from admin.vi_cs_precalloutcust");
            while (reader.Read())
           {
               
                if (!reader.IsDBNull(0))
                {
                    this.textBox1.Text += reader[0].ToString() + "\r\n";
                }
           }
           reader.Close();

        }


        public static OdbcDataReader ExecuteReader(string connectionStr, string strSQL)
        {
            OdbcConnection connection = new OdbcConnection(connectionStr);

            OdbcCommand cmd = new OdbcCommand(strSQL, connection);
            try
            {
                connection.Open();
                OdbcDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return myReader;
            }
            catch (SqlException E)
            {
                 WriteLogFile.WriteMessage("DBOper.log", "error DBHelper.ExecuteReader " + E.Message.ToString());
            }
            return null;
        }

二:使用c#通过
IBM DB2 Data Access Application Blocks for .NET连接到IBM db2

参见https://www6.software.ibm.com/developerworks/cn/dmdd/library/tutorials/dm0504hoy/tutorial/index.html


部署
需要客户端安装DB2 Runtime Client Lite。大约500多M。下载地址ftp://ftp.software.ibm.com/ps/products/db2/fixes2/english-us/db2winIA32v8/fixpak/FP17_WR21416/FP17_WR21416_RTLITE.zip

测试未通过。发布到另外一台机器,总是显示“Length cannot be less than zero. Parameter name: length ”的错误。
代码
vs2005测试通过,引用IBM.Data.DB2.dll(net2.0版本),引用IBM.Data.ApplicationBlocks.DB2.dll

        private void button1_Click(object sender, EventArgs e)
        {
            DB2DataReader dr = null;
            string connString = "DATABASE=SAMPLE;SERVER=192.168.1.46:50000;User ID=admin;Password=admin;";
            try
            {
                dr = DB2Helper.ExecuteReader(
connString, CommandType.Text, "select * from vi_cs_precalloutcust");

                while (dr.Read())
                {
                    txtResults.Text = txtResults.Text + dr.GetValue(0) + " (" + dr.GetValue(1) + ")" + Environment.NewLine;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

            if (dr != null)
            {
                ((IDisposable)dr).Dispose();

            }

           
        }

标签: C# DB2 IBM 编程
来源:http://www.cnblogs.com/pengdc/p/3600799.html

推荐: