|
|
| .NET访问Oracle数据库相关 |
| 作者:无从考证 来源:转载 发布时间:2005-11-18 13:55:35 发布人:chinazhan |
减小字体
增大字体
.NET 访问 Oracle 过程/函数的方法很类似于 SQL Server,例如: OracleParameter[] parameters = { new OracleParameter("ReturnValue", OracleType.Int32, 0, ParameterDirection.ReturnValue, true, 0, 0, "", DataRowVersion.Default, Convert.DBNull ) new OracleParameter("参数1", OracleType.NVarChar, 10), new OracleParameter("参数2", OracleType.DateTime), new OracleParameter("参数3", OracleType.Number, 1) }; parameters[1].Value = "test"; parameters[2].Value = DateTime.Now; parameters[3].Value = 1; // 也可以是 new OracleNumber(1); OracleConnection connection = new OracleConnection( ConnectionString ); OracleCommand command = new OracleCommand("函数/程名", connection); command.CommandType = CommandType.StoredProcedure; foreach(OracleParameter parameter in parameters) command.Parameters.Add( parameter ); connection.Open(); command.ExecuteNonQuery(); int returnValue = parameters[0].Value; //接收函数返回值 connection.Close(); Parameter 的 DbType 设定请参见 System.Data.OracleClient.OracleType 枚举的文档,比如:Oracle 数据库中 Number 类型的参数的值可以用 .NET decimal 或 System.Data.OracleClient.OracleNumber 类型指定; Integer 类型的参数的值可以用 .NET int 或 OracleNumber 类型指定。等等。 上面例子中已经看到函数返回值是用名为“ReturnValue”的参数指定的,该参数为 ParameterDirection.ReturnValue 的参数。 5。访问 Oracle 过程/函数 (2) 不返回记录集(没有 SELECT 输出)的过程/函数,调用起来和 SQL Server 较为类似。但如果想通过过程/函数返回记录集,在 Oracle 中就比较麻烦一些了。 在 SQL Server 中,如下的存储过程: CREATE PROCEDURE GetCategoryBooks ( @CategoryID int ) AS SELECT * FROM Books WHERE CategoryID = @CategoryID GO 在 Oracle 中,请按以下步骤操作: (1)创建一个包,含有一个游标类型:(一个数据库中只需作一次) CREATE OR REPLACE PACKAGE Test AS TYPE Test_CURSOR IS REF CURSOR; END Test; (2)过程: CREATE OR REPLACE PROCEDURE GetCategoryBooks ( p_CURSOR out Test.Test_CURSOR, -- 这里是上面包中的类型,输出参数 p_CatogoryID INTEGER ) AS BEGIN OPEN p_CURSOR FOR SELECT * FROM Books WHERE CategoryID=p_CatogoryID; END GetCategoryBooks; (3).NET 程序中: OracleParameters parameters = { new OracleParameter("p_CURSOR", OracleType.CURSOR, 2000, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Default, Convert.DBNull), new OracleParameter("p_CatogoryID", OracleType.Int32) }; parameters[1].Value = 22; OracleConnection connection = new OracleConnection( ConnectionString ); OracleCommand command = new OracleCommand("GetCategoryBooks", connection); command.CommandType = CommandType.StoredProcedure; foreach(OracleParameter parameter in parameters) command.Parameters.Add( parameter ); connection.Open(); OracleDataReader dr = command.ExecuteReader(); while(dr.Read()) { // 你的具体操作。这个就不需要我教吧? } connection.Close(); 另外有一点需要指出的是,如果使用 DataReader 取得了一个记录集,那么在 DataReader 关闭之前,程序无法访问输出参数和返回值的数据。 上一页 [1] [2]
|
| |
|
[]
[返回上一页]
[打 印]
[收 藏] |
|
| ∷相关文章评论∷ (评论内容只代表网友观点,与本站立场无关!) [更多评论...] |
|
|