1 OracleClient資料庫操作(淘汰)

2022-02-18 21:13:31 字數 3402 閱讀 2061

一、資料庫連線

oracle 資料提供程式,位於system.data.oracleclient 命名空間.( .net 4 以後的版本,會將不在維護和更新了)

第一步:引入命名空間

在程式的開頭寫上下面的**

using system.data.oracleclient;
第二步:引入對應元件

點選專案-右鍵-新增引用-找到system.data.oracleclient-確定.

第三步:寫**

private

void button1_click(object

sender, eventargs e)

conn.close();

}

database和data source的區別?database是資料庫的意思,用sqlserver資料庫是需要用這個。 

data source是資料資源的意思,用oracle資料庫時用這個。

if (conn.state == connectionstate.open)的意思?用conn.state判斷當前資料庫是關閉還是開啟的,而connectionstate.open得意思是開啟。所以如果資料庫是開啟狀態就可以為「真」。 這句話是用來判斷資料庫是不是開啟狀態的。(前提是要引用using system.data;命名空間)

安裝oracle後 tnsnames.ora檔案目錄

orcl =

(description =

(address_list =

(address = (protocol = tcp)(host =localhost)(port = 1521))

)(connect_data =

(server = dedicated)

(service_name = orcl)))

二、執行sql語句(command

oraclecommand 命令物件的名稱 =new oraclecommand(sql語句,連線物件名稱);

共分為兩種操作:增刪改和查詢操作

當需要使用增刪改操作時

string str1="

insert into student values(1,'tom')";

oraclecommand cmd = new

oraclecommand(str1,conn);

cmd.executereader();

當需要查詢時

string str1="

select * from student";

oraclecommand cmd = new

oraclecommand(str1,conn);

oracledatareader dr=cmd.executereader();//

把查詢的結果傳給oracledatareader的物件

//下一步讀取資料

讀取資料oracledatareader物件獲取到值以後就需要讀取資料了

首先

dr.read();//

把箭頭指向**的第一行,第一列

然後dr["表的欄位名"]或者dr[下標數字(相當於陣列)] 就可以讀取指定的資料了

假設**的第乙個欄位是id,第二個欄位是name

例如dr["id"] 或者dr[0] 就能讀出第一行第一列的資料

dr["name"]或者di[1]就能讀出第一行第二列的資料

如果想要第二行的資料就需要用到迴圈

while (dr.read())//

假設這是乙個控制台程式

command 有三個常用方法1、

cmd.executenonquery();//執行sql語句並

返回受影響的行數。通常用來執行增刪改。

2、

oracledatareader dr=cmd.executereader();//

執行sql語句並把查詢的內容放到oracledatareader物件裡。用來做查詢

3、

cmd.executescalar();//

執行sql語句,並返回結果集中的第一行第一列。通常和函式配合使用,為了計算某個值。

三、 datareaderoracledatareader的屬性:hasrows 用來判斷結果是否有資料,有的話返回true

if

(dr.hasrows)

oracledatareader的方法:

dr.read();//

dr.close();//

關閉oracledatareader物件。如果乙個oracleconnection建立了多個oracledatareader,則在建立oracledatareader之前需要先管壁上乙個。

四、資料介面卡 dataadapter物件dataadapter物件是乙個資料介面卡物件,在dataset與資料來源之間起到橋梁的作用。它有四個屬性

1、 selectcommand ,用來向資料庫傳送查詢sql語句

2、deletecommand,向資料庫傳送刪除語句

3、insertcommand,向資料庫傳送插入語句

4、updatecommand,向資料庫傳送更新語句

dataadapter的方法

1 、fill方法:用於填充dataset資料集

da.fill(ds,"

student

");//

括號裡的引數(data物件,表名);

datagridview1.datasource = ds.tables["表名"

];//

或者datagridview1.datasource = ds.tables[0];

2、updata方法:用來更新資料庫

五、資料集dataset

Oracle Client 配置連線資料庫

從2.本地儲存oracle client package 3.在當前目錄下,新建立兩個檔案,sqlnet.ora和tnsnames.ora然後再手動配置sqlnet.ora和tnsnames.ora 比如這裡是 sqlnet.ora檔案內容 sqlnet.authentication service...

資料探勘1

資料探勘過程的方 其中比較經典的是crisp dm cross industrystandard process for data mining,跨行業資料探勘標準流程 其中一共分為6個步驟 商業理解,資料理解,資料準備,建模,評估,發布。統計學習劃分為兩種型別 有 監督學習,無監督學習 有監督學習...

複習資料1

1.實體類為什麼都用包裝型別?舉個例子,乙個student類,其成績屬性,我們設定為 private integer socre 而不設定為 private int socre 這是因為有的學生會出現考0分,而有的學生缺考,這樣,就沒有辦法去區分了。包裝型別 integer 和 基本型別 int 的...