C#連接db2數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
更新時(shí)間:2013年05月31日 15:31:32 作者:
本篇文章是對(duì)C#連接db2數(shù)據(jù)庫(kù)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
通過OLE DB for DB2驅(qū)動(dòng)
string strSql = @"select phone_no from no_store where id<5";
string strConn = "Provider=IBMDADB2;Data Source=數(shù)據(jù)庫(kù)名;UID=用戶名;PWD=密碼;";
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("電話" + i + ":" + dt.Rows[i][0].ToString());
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Console.Read();
通過IBM提供的IBM.data.DB2.DLL
string strSql = @"select phone_no from no_store where id<5";
string strConn = "Database=數(shù)據(jù)庫(kù)名;UID=用戶名;PWD=密碼;";
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("電話" + i + ":" + dt.Rows[i][0].ToString());
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Console.Read();
小結(jié)
(1)兩種方式的數(shù)據(jù)庫(kù)操作對(duì)象可以參考c#連接sqlserver的數(shù)據(jù)庫(kù)對(duì)象。
(2)如果db2數(shù)據(jù)庫(kù)在遠(yuǎn)程服務(wù)器,連接字符串中的數(shù)據(jù)庫(kù)名、用戶名、密碼為db2編目到本地的數(shù)據(jù)庫(kù)名、用戶名、密碼。
(3)使用IBM.Data.DB2,必須引用該程序集。
復(fù)制代碼 代碼如下:
string strSql = @"select phone_no from no_store where id<5";
string strConn = "Provider=IBMDADB2;Data Source=數(shù)據(jù)庫(kù)名;UID=用戶名;PWD=密碼;";
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("電話" + i + ":" + dt.Rows[i][0].ToString());
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Console.Read();
通過IBM提供的IBM.data.DB2.DLL
復(fù)制代碼 代碼如下:
string strSql = @"select phone_no from no_store where id<5";
string strConn = "Database=數(shù)據(jù)庫(kù)名;UID=用戶名;PWD=密碼;";
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("電話" + i + ":" + dt.Rows[i][0].ToString());
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Console.Read();
小結(jié)
(1)兩種方式的數(shù)據(jù)庫(kù)操作對(duì)象可以參考c#連接sqlserver的數(shù)據(jù)庫(kù)對(duì)象。
(2)如果db2數(shù)據(jù)庫(kù)在遠(yuǎn)程服務(wù)器,連接字符串中的數(shù)據(jù)庫(kù)名、用戶名、密碼為db2編目到本地的數(shù)據(jù)庫(kù)名、用戶名、密碼。
(3)使用IBM.Data.DB2,必須引用該程序集。
您可能感興趣的文章:
- C#實(shí)現(xiàn)異步連接Sql Server數(shù)據(jù)庫(kù)的方法
- C#使用開源驅(qū)動(dòng)連接操作MySQL數(shù)據(jù)庫(kù)
- C#實(shí)現(xiàn)遠(yuǎn)程連接ORACLE數(shù)據(jù)庫(kù)的方法
- c#連接sqlserver數(shù)據(jù)庫(kù)、插入數(shù)據(jù)、從數(shù)據(jù)庫(kù)獲取時(shí)間示例
- c#連接mysql數(shù)據(jù)庫(kù)的方法
- c#連接數(shù)據(jù)庫(kù)及sql2005遠(yuǎn)程連接的方法
- C# 連接SQL數(shù)據(jù)庫(kù)的方法及常用連接字符串
- c#連接access數(shù)據(jù)庫(kù)操作類分享
- C#連接MySql數(shù)據(jù)庫(kù)的方法
- C#使用Socket快速判斷數(shù)據(jù)庫(kù)連接是否正常的方法
相關(guān)文章
C#中調(diào)用DLL時(shí)未能加載文件或程序集錯(cuò)誤的處理方法(詳解)
下面小編就為大家?guī)硪黄狢#中調(diào)用DLL時(shí)未能加載文件或程序集錯(cuò)誤的處理方法(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Unity UGUI教程之實(shí)現(xiàn)滑頁(yè)效果
使用UGUI提供的ScrollRect和ScrollBar組件實(shí)現(xiàn)基本滑動(dòng)以及自己控制每次移動(dòng)一頁(yè)來達(dá)到滑頁(yè)的效果。具體實(shí)現(xiàn)思路請(qǐng)參考下本教程2016-04-04Winform下實(shí)現(xiàn)圖片切換特效的方法
這篇文章主要介紹了Winform下實(shí)現(xiàn)圖片切換特效的方法,包括百葉窗、淡入、旋轉(zhuǎn)等多種效果,需要的朋友可以參考下2014-08-08C#實(shí)現(xiàn)簡(jiǎn)單的天氣預(yù)報(bào)示例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單的天氣預(yù)報(bào)示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06