关于c#连接DB2

      前段时间项目中有个要去DB2连接数据的功能,顺便就把相关知识整理下。

c#连接DB2一共有3种方式

分别为

一、ODBC

OdbcConnection odbcConn = new OdbcConnection("Driver={IBM DB2 ODBC
DRIVER};Server=localhost;DSN=TESTDB;UID=username;PWD=pwd;Protocol=TCPIP");
odbcConn.Open();

 

二、OLE DB

[C#]
OleDbConnection con = new OleDbConnection("Provider=IBMDADB2;" +
"Data Source=sample;UID=userid;PWD=password;" );
con.Open()
参考地址
http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.apdv.ms.doc/doc/c0011825.html
這裡貼一個我寫的連接DB2 返回一個oledbdatareader的類

using System;
using System.Collections.Generic;
using System.Data.OleDb;
public class DB2Helper : IDisposable
{
/// <summary>
/// <DB2>执行查询语句,返回SqlDataReader
/// </summary>
/// <param name="strSQL"><DB2>查询语句</param>
/// <returns>SqlDataReader</returns>
private bool m_disposed;//is or not disposed true or flase
OleDbConnection cn=null;
OleDbCommand DB2Command = null;
private string strQuerySQL="";

public OleDbDataReader ExecuteReaderDB2(string strSQL)
{
strQuerySQL = strSQL;
DB2Command = new OleDbCommand(strQuerySQL, cn);
OleDbDataReader rdr = null;
try
{
cn.Open();
rdr = DB2Command.ExecuteReader();
}
catch (System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
return rdr;
}

public DB2Helper()
{
string DB2ConnectionString = @"Provider=IBMDADB2.1;Location=xxx.xxx.xxx.xxx:xxxx;Data Source=TestDB;" +
" Persist Security Info=True;User ID=xxxx;Password=xxxx;CurrentSchemaTestDB;";
cn = new OleDbConnection(DB2ConnectionString);
DB2Command = new OleDbCommand();
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (!m_disposed)
{
if (!disposing)
{
// Release unmanaged resources
DB2Command.Connection.Close();
DB2Command = null;
cn.Close();
cn = null;
}

// Release managed resources
m_disposed = true;
}

}

~DB2Helper()
{
Dispose(false);
}
}

 

三、使用IBM提供的IBM.data.DB2.DLL 的接口进行连接

using  IBM.data.DB2;

DB2Connection cn = new DB2Connection(
  "Database=TEST;UserID=db2admin; Password=password;Server=IS500");

DB2Command myDB2Command = new DB2Command(myInsertQuery);

myDB2Command.Connection = cn ;

myConn.Open();

myDB2Command.ExecuteNonQuery();

myConn.Close();

其中IBM.data.DB2.DLL在DB2客户端里有

(大概位置是 in X:/Program Files/IBM/SQLLIB/BIN/netf11 )

参考地址

http://publib.boulder.ibm.com/infocenter/db2luw/v8//index.jsp?topic=/com.ibm.db2.udb.dndp.doc/htm/frlrfIBMDataDB2DB2ConnectionClassTopic.htm

虽然没有完全测试完毕,,第二,三种方法,,我确认是必须安装DB2客户端才有办法使用
而且第三种,还必须版本足够高才能正常使用

资料不是很完整,,,待以后补充噶
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值