C#連接db2數(shù)據(jù)庫的實(shí)現(xiàn)方法
更新時(shí)間:2013年05月31日 15:31:32 作者:
本篇文章是對(duì)C#連接db2數(shù)據(jù)庫的方法進(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ù)庫名;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ù)庫名;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ù)庫操作對(duì)象可以參考c#連接sqlserver的數(shù)據(jù)庫對(duì)象。
(2)如果db2數(shù)據(jù)庫在遠(yuǎn)程服務(wù)器,連接字符串中的數(shù)據(jù)庫名、用戶名、密碼為db2編目到本地的數(shù)據(jù)庫名、用戶名、密碼。
(3)使用IBM.Data.DB2,必須引用該程序集。
復(fù)制代碼 代碼如下:
string strSql = @"select phone_no from no_store where id<5";
string strConn = "Provider=IBMDADB2;Data Source=數(shù)據(jù)庫名;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ù)庫名;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ù)庫操作對(duì)象可以參考c#連接sqlserver的數(shù)據(jù)庫對(duì)象。
(2)如果db2數(shù)據(jù)庫在遠(yuǎn)程服務(wù)器,連接字符串中的數(shù)據(jù)庫名、用戶名、密碼為db2編目到本地的數(shù)據(jù)庫名、用戶名、密碼。
(3)使用IBM.Data.DB2,必須引用該程序集。
您可能感興趣的文章:
- C#實(shí)現(xiàn)異步連接Sql Server數(shù)據(jù)庫的方法
- C#使用開源驅(qū)動(dòng)連接操作MySQL數(shù)據(jù)庫
- C#實(shí)現(xiàn)遠(yuǎn)程連接ORACLE數(shù)據(jù)庫的方法
- c#連接sqlserver數(shù)據(jù)庫、插入數(shù)據(jù)、從數(shù)據(jù)庫獲取時(shí)間示例
- c#連接mysql數(shù)據(jù)庫的方法
- c#連接數(shù)據(jù)庫及sql2005遠(yuǎn)程連接的方法
- C# 連接SQL數(shù)據(jù)庫的方法及常用連接字符串
- c#連接access數(shù)據(jù)庫操作類分享
- C#連接MySql數(shù)據(jù)庫的方法
- C#使用Socket快速判斷數(shù)據(jù)庫連接是否正常的方法
相關(guān)文章
C#將布爾類型轉(zhuǎn)換成字節(jié)數(shù)組的方法
這篇文章主要介紹了C#將布爾類型轉(zhuǎn)換成字節(jié)數(shù)組的方法,涉及C#中字符串函數(shù)的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
詳解C#借助.NET框架中的XmlTextReader類讀取XML的方法
這篇文章主要介紹了詳解借助.NET框架中的XmlTextReader類讀取XML的方法,這種方式的執(zhí)行效率還是比較令人滿意的,需要的朋友可以參考下2016-04-04
C#使用正則表達(dá)式實(shí)現(xiàn)漢字轉(zhuǎn)拼音
這篇文章主要為大家詳細(xì)介紹了C#如何使用正則表達(dá)式實(shí)現(xiàn)漢字轉(zhuǎn)拼音的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
C#中dictionary如何根據(jù)索引值獲取Key值
這篇文章主要介紹了C#中dictionary如何根據(jù)索引值獲取Key值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
c#實(shí)現(xiàn)數(shù)據(jù)庫事務(wù)示例分享
這篇文章主要介紹了c#執(zhí)行多條sql更新語句實(shí)現(xiàn)數(shù)據(jù)庫事務(wù)的示例,大家參考使用吧2014-01-01

