首页 新闻 会员 周边

c# mvc 连接db2问题

0
悬赏园豆:10 [待解决问题]

请问,c# mvc 怎么连接db2?

连接字符串怎么写?

需要添加什么dll文件么?

求大神说明详细一些~~~

大叔、的主页 大叔、 | 初学一级 | 园豆:84
提问于:2016-03-14 18:20
< >
分享
所有回答(2)
0

神站 www.connectionstrings.com

吴瑞祥 | 园豆:29449 (高人七级) | 2016-03-14 21:33

这个我有看,点就去之后,找不到下载,我想下载db2的dll,找不到

支持(1) 反对(0) 大叔、 | 园豆:84 (初学一级) | 2016-03-14 23:16

@chuu: (⊙﹏⊙)b,谢谢

支持(0) 反对(0) 大叔、 | 园豆:84 (初学一级) | 2016-03-15 11:50
0

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客户端才有办法使用
而且第三种,还必须版本足够高才能正常使用

 

陀螺- | 园豆:74 (初学一级) | 2016-03-15 00:22

好吧,完全复制粘贴的,不过还是谢谢

支持(0) 反对(0) 大叔、 | 园豆:84 (初学一级) | 2016-03-15 11:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册