對於 HR 架構的默認安裝
上述代碼顯示
代碼還說明了如何定義結果集的 REF CURSOR 參數
還請注意
如果包返回多個游標
返回單個值的存儲過程
OracleCommand 類的 ExecuteOracleScalar() 方法用於執行將單個值作為 OracleType 數據類型返回的 SQL 語句或存儲過程
盡管如此
您只能使用 RETURN 參數檢索 Oracle 函數的返回值(如上一節所述)
序列
Oracle 使用序列 來生成唯一編號
Oracle 序列是原子對象
可以使用 CREATE SEQUENCE 命令創建 Oracle 序列
CREATE SEQUENCE LOCATIONS_SEQ
INCREMENT BY
START WITH
MAXVALUE
MINVALUE
NOCYCLE
NOCACHE
NOORDER
大多數序列代碼是不言自明的
下面的代碼顯示了一個存儲過程
CREATE OR new PROCEDURE ADD_LOCATION (
p_location_id OUT NUMBER
p_street_address IN VARCHAR
p_postal_code IN VARCHAR
p_city IN VARCHAR
p_state_province IN VARCHAR
p_country_id IN CHAR
)
AS
BEGIN
INSERT INTO LOCATIONS (
LOCATION_ID
STREET_ADDRESS
POSTAL_CODE
CITY
STATE_PROVINCE
COUNTRY_ID)
VALUES (
LOCATIONS_SEQ
p_street_address
p_postal_code
p_city
p_state_province
p_country_id
);
SELECT LOCATIONS_SEQ
END ADD_LOCATION;
下面的代碼調用該存儲過程
// create the connection
OracleConnection conn = new OracleConnection(
User Id=UserID;Password=Password;
// create the command for the stored procedure
OracleCommand cmd = new OracleCommand();
cmd
cmd
cmd
// add the parameters for the stored procedure including the LOCATION_ID
// sequence value that is returned in the output parameter p_location_id
cmd
ParameterDirection
cmd
cmd
cmd
cmd
cmd
// execute the command to add the records
OracleString rowId;
conn
int rowsAffected = cmd
conn
// output the results
Console
Console
cmd
控制台顯示一個記錄被插入到該表中
Rows affected:
Location ID:
使用 DataAdapter 填充數據集
可使用 REF CURSOR 通過 DataAdapter 來填充 DataSet
以下是使用 DataAdapter 填充 DataSet 的代碼
// create the connection
OracleConnection conn = new OracleConnection(
User Id=UserID;Password=Password;
// create the command for the stored procedure
OracleCommand cmd = new OracleCommand();
cmd
cmd
cmd
// add the parameters for the stored procedure including the REF CURSOR
// to retrieve the result set
cmd
cmd
ParameterDirection
// createt the DataAdapter from the command and use it to fill the
// DataSet
OracleDataAdapter da = new OracleDataAdapter(cmd);
DataSet ds = new DataSet();
da
// output the results
Console
對於 HR 架構的默認安裝
使用 DataAdapter 更新 Oracle
當您使用 REF CURSOR 參數填充 DataSet 時
本節說明如何生成一個可以處理所需的創建
CREATE OR new PACKAGE CRUD_LOCATIONS AS
TYPE T_CURSOR IS REF CURSOR;
PROCEDURE GetLocations (cur_Locations OUT T_CURSOR);
PROCEDURE UpdateLocations (
p_location_id IN NUMBER
p_street_address IN VARCHAR
p_postal_code IN VARCHAR
p_city IN VARCHAR
p_state_province IN VARCHAR
p_country_id IN CHAR);
PROCEDURE DeleteLocations (p_location_id IN NUMBER);
PROCEDURE InsertLocations (
p_location_id OUT NUMBER
p_street_address IN VARCHAR
p_postal_code IN VARCHAR
p_city IN VARCHAR
p_state_province IN VARCHAR
p_country_id IN CHAR);
END CRUD_LOCATIONS;
包正文如下所示
CREATE OR new PACKAGE BODY CRUD_LOCATIONS AS
PROCEDURE GetLocations (cur_Locations OUT T_CURSOR)
IS
BEGIN
OPEN cur_Locations FOR
SELECT * FROM LOCATIONS;
END GetLocations;
PROCEDURE UpdateLocations (
p_location_id IN NUMBER
p_street_address IN VARCHAR
p_postal_code IN VARCHAR
p_city IN VARCHAR
p_state_province IN VARCHAR
p_country_id IN CHAR)
IS
BEGIN
UPDATE LOCATIONS
SET
STREET_ADDRESS = p_street_address
POSTAL_CODE = p_postal_code
CITY = p_city
STATE_PROVINCE = p_state_province
COUNTRY_ID = p_country_id
WHERE
LOCATION_ID = p_location_id;
END UpdateLocations;
PROCEDURE DeleteLocations (p_location_id IN NUMBER)
IS
BEGIN
DELETE FROM LOCATIONS
WHERE LOCATION_ID = p_location_id;
END DeleteLocations;
PROCEDURE InsertLocations
(
p_location_id OUT NUMBER
p_street_address IN VARCHAR
p_postal_code IN VARCHAR
p_city IN VARCHAR
p_state_province IN VARCHAR
p_country_id IN CHAR
)
AS
BEGIN
INSERT INTO LOCATIONS (
LOCATION_ID
STREET_ADDRESS
POSTAL_CODE
CITY
STATE_PROVINCE
COUNTRY_ID)
VALUES (
LOCATIONS_SEQ
p_street_address
p_postal_code
p_city
p_state_province
p_country_id
);
SELECT LOCATIONS_SEQ
END InsertLocations;
END CRUD_LOCATIONS;
下面的代碼定義了一個 DataAdapter
// define the connection string
String connString =
// create the data adapter
OracleDataAdapter da = new OracleDataAdapter();
// define the select command for the data adapter
OracleCommand selectCommand =
new OracleCommand(
new OracleConnection(connString));
selectCommand
selectCommand
OracleType
da
// define the udpate command for the data adapter
OracleCommand updateCommand =
new OracleCommand(
new OracleConnection(connString));
updateCommand
updateCommand
updateCommand
updateCommand
updateCommand
updateCommand
updateCommand
da
// define the delete command for the data adapter
OracleCommand deleteCommand =
new OracleCommand(
new OracleConnection(connString));
deleteCommand
deleteCommand
da
OracleCommand insertCommand =
new OracleCommand(
new OracleConnection(connString));
insertCommand
insertCommand
insertCommand
insertCommand
insertCommand
insertCommand
insertCommand
da
// define a DataTable and fill it using the data adapter
DataTable dt = new DataTable();
da
//
// call the Update() method of the data adapter to update the Oracle
// database with changes made to the data
da
使用多個結果集
Oracle 不支持批量查詢
以下是返回兩個結果集(全部 EMPLOYEES 和 JOBS 記錄)的包規范
CREATE OR new PACKAGE SELECT_EMPLOYEES_JOBS AS
TYPE T_CURSOR IS REF CURSOR;
PROCEDURE GetEmployeesAndJobs (
cur_Employees OUT T_CURSOR
cur_Jobs OUT T_CURSOR
);
END SELECT_EMPLOYEES_JOBS;
包正文如下所示
CREATE OR new PACKAGE BODY SELECT_EMPLOYEES_JOBS AS
PROCEDURE GetEmployeesAndJobs
(
cur_Employees OUT T_CURSOR
cur_Jobs OUT T_CURSOR
)
IS
BEGIN
OPEN cur_Employees FOR
SELECT * FROM Employees;
OPEN cur_Jobs FOR
SELECT * FROM Jobs;
END GetEmployeesAndJobs;
END SELECT_EMPLOYEES_JOBS;
以下代碼顯示了如何使用從上述包中返回的兩個結果集來填充 DataSet 中的兩個相關表
// create the connection
OracleConnection conn = new OracleConnection(
User Id=UserID;Password=Password;
// define the command for the stored procedure
OracleCommand cmd = new OracleCommand();
cmd
cmd
// add the parameters including the two REF CURSOR types to retrieve
// the two result sets
cmd
ParameterDirection
cmd
ParameterDirection
cmd
// create the DataAdapter and map tables
OracleDataAdapter da = new OracleDataAdapter(cmd);
da
da
// create and fill the DataSet
DataSet ds = new DataSet();
da
// create a relation
ds
ds
ds
// output the second employee (zero
// based on the relation
Console
ds
ds
控制台輸出顯示了第二個員工的職務
Employee ID:
小結
通過 Oracle
Oracle 過程與 MicrosoftSQL Server 存儲過程之間的主要區別是
From:http://tw.wingwit.com/Article/program/net/201311/11377.html