从 DbCommand 对象转换为 OracleCommand 对象

发布于 2024-08-05 18:45:19 字数 1750 浏览 12 评论 0原文

我继承了 ASP.NET (C#) 应用程序中的一个函数,其中作者使用了 Microsoft.Practices.EnterpriseLibrary.Data 库,但有人要求我更改它,以便它使用 System.Data.OracleClient。该函数使用数据库中的存储过程。 itemNameopenDate 是函数接受的字符串参数。PKG_AUCTION_ITEMS.IsAuctionItem 是存储过程函数名称。

这是我收到的代码:

    string result = String.Empty;

    Database db = DatabaseFactory.CreateDatabase("OraData");
    using (DbCommand cmdDB = db.GetStoredProcCommand("PKG_AUCTION_ITEMS.IsAuctionItem"))
    {
        db.AddInParameter(cmdDB, "vItemName", DbType.String, itemName);
        db.AddInParameter(cmdDB, "vOpenDate", DbType.String, openDate);
        db.AddParameter(cmdDB, "ret", DbType.String, 2, ParameterDirection.ReturnValue, false, 0, 0, null, DataRowVersion.Current, null);


        db.ExecuteNonQuery(cmdDB);
        result = cmdDB.Parameters["ret"].Value.ToString();
    }

这是我的代码:(connstr 是连接字符串)

    string result = String.Empty;
    OracleConnection conn = new OracleConnection(connstr);
    OracleCommand cmd = new OracleCommand("PKG_AUCTION_ITEMS.IsAuctionItem",conn);
    myCmd.CommandType = CommandType.StoredProcedure;

    using (myCmd)
    {
        myCmd.Parameters.AddWithValue("vItemName", itemName);
        myCmd.Parameters.AddWithValue("vOpenDate", openDate);
        myCmd.Parameters.AddWithValue("ret", ???);
        myCmd.ExecuteNonQuery();
        result = myCmd.Parameters["ret"].Value.ToString();
    }

我不明白 AddInParameter 和 AddParameter 之间的区别是什么,以及这一行的作用:

db.AddParameter(cmdDB, "ret", DbType.String, 2, ParameterDirection.ReturnValue, false, 0, 0, null, DataRowVersion.Current, null);

我在正确的轨道吗?有人可以帮忙吗? 谢谢

I have inherited a function in an ASP.NET (C#) application where the author used the Microsoft.Practices.EnterpriseLibrary.Data library, but I have been asked to change it so that it uses System.Data.OracleClient. This function uses a stored procedure form a database. itemName, and openDate are string parameters that the function takes in. PKG_AUCTION_ITEMS.IsAuctionItem is the stored procedure function name.

Here is the code that I received:

    string result = String.Empty;

    Database db = DatabaseFactory.CreateDatabase("OraData");
    using (DbCommand cmdDB = db.GetStoredProcCommand("PKG_AUCTION_ITEMS.IsAuctionItem"))
    {
        db.AddInParameter(cmdDB, "vItemName", DbType.String, itemName);
        db.AddInParameter(cmdDB, "vOpenDate", DbType.String, openDate);
        db.AddParameter(cmdDB, "ret", DbType.String, 2, ParameterDirection.ReturnValue, false, 0, 0, null, DataRowVersion.Current, null);


        db.ExecuteNonQuery(cmdDB);
        result = cmdDB.Parameters["ret"].Value.ToString();
    }

Here is my code:(connstr is the connection string)

    string result = String.Empty;
    OracleConnection conn = new OracleConnection(connstr);
    OracleCommand cmd = new OracleCommand("PKG_AUCTION_ITEMS.IsAuctionItem",conn);
    myCmd.CommandType = CommandType.StoredProcedure;

    using (myCmd)
    {
        myCmd.Parameters.AddWithValue("vItemName", itemName);
        myCmd.Parameters.AddWithValue("vOpenDate", openDate);
        myCmd.Parameters.AddWithValue("ret", ???);
        myCmd.ExecuteNonQuery();
        result = myCmd.Parameters["ret"].Value.ToString();
    }

I do not understand what the difference between AddInParameter and AddParameter is, and what this line does:

db.AddParameter(cmdDB, "ret", DbType.String, 2, ParameterDirection.ReturnValue, false, 0, 0, null, DataRowVersion.Current, null);

Am I on the right track? Can anyone please help?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂ガ小子 2024-08-12 18:45:19

在这种情况下,db.AddParameter 添加一个输出参数。您需要让数据库客户端库知道您正在寻找从存储过程调用返回的返回值。因此调用 AddParameter。 db.AddInParameter 添加一个参数,该参数只是一个内参数。这是使用 ParameterDirection.Input 的 db.AddParameter 的快捷方式。请参阅http://blogs.x2line.com/al/archive /2006/03/25/1579.aspx 有关 AddInParameter 与 AddParameter 的讨论。

类似地,使用 OracleClient,AddWithValue 就像 AddInParameter——当您已经知道值时用于输入参数的快捷方式。由于根据定义,返回值是输出参数,因此您不能使用 AddWithValue。您需要改用Parameters.Add()。

现在,回到您的主要问题:使用 OracleClient 的等效代码是什么。是这样的:

string result = String.Empty;
OracleConnection conn = new OracleConnection(connstr);
OracleCommand cmd = new OracleCommand("PKG_AUCTION_ITEMS.IsAuctionItem",conn);
myCmd.CommandType = CommandType.StoredProcedure;

using (myCmd)
{
    myCmd.Parameters.AddWithValue("vItemName", itemName);
    myCmd.Parameters.AddWithValue("vOpenDate", openDate);

    // depending on whether you're using Microsoft's or Oracle's ODP, you 
    // may need to use OracleType.Varchar instead of OracleDbType.Varchar2.
    // See http://forums.asp.net/t/1002097.aspx for more details.
    OracleParameter retval = new OracleParameter("ret",OracleDbType.Varchar2,2);
    retval.Direction = ParameterDirection.ReturnValue;
    myCmd.Parameters.Add(retval);

    myCmd.ExecuteNonQuery();
    result = myCmd.Parameters["ret"].Value.ToString();
}

db.AddParameter adds an output parameter in this case. You need to let the db client library know that you're looking to get the return value back from the sproc call. Hence the call to AddParameter. db.AddInParameter adds a parameter which is only an in-parameter. In the It's a shortcut for db.AddParameter using ParameterDirection.Input. See http://blogs.x2line.com/al/archive/2006/03/25/1579.aspx for a discussion of AddInParameter vs. AddParameter.

Similarly, using OracleClient, AddWithValue is like AddInParameter-- a shortcut to use for input params when you already know the value. Since the return value is, by definition, an output parameter, you can't use AddWithValue. You need to use Parameters.Add() instead.

Now, back to your main question: what's the equivalent code using OracleClient. It's something like this:

string result = String.Empty;
OracleConnection conn = new OracleConnection(connstr);
OracleCommand cmd = new OracleCommand("PKG_AUCTION_ITEMS.IsAuctionItem",conn);
myCmd.CommandType = CommandType.StoredProcedure;

using (myCmd)
{
    myCmd.Parameters.AddWithValue("vItemName", itemName);
    myCmd.Parameters.AddWithValue("vOpenDate", openDate);

    // depending on whether you're using Microsoft's or Oracle's ODP, you 
    // may need to use OracleType.Varchar instead of OracleDbType.Varchar2.
    // See http://forums.asp.net/t/1002097.aspx for more details.
    OracleParameter retval = new OracleParameter("ret",OracleDbType.Varchar2,2);
    retval.Direction = ParameterDirection.ReturnValue;
    myCmd.Parameters.Add(retval);

    myCmd.ExecuteNonQuery();
    result = myCmd.Parameters["ret"].Value.ToString();
}
旧人 2024-08-12 18:45:19

实际上,我们更明确地进行参数配置,如下所示。

System.Data.OracleClient.OracleCommand command = new System.Data.OracleClient.OracleCommand("PACKAGE_NAME.STORED_NAME");
command.CommandType = System.Data.CommandType.StoredProcedure;

System.Data.OracleClient.OracleParameter param;
param = new System.Data.OracleClient.OracleParameter("PARAM_NAME_ID", System.Data.OracleClient.OracleType.Number);
param.Value = id;
command.Parameters.Add(param);

param = new System.Data.OracleClient.OracleParameter("PARAM_NAME_RETURN_COUNT", System.Data.OracleClient.OracleType.Number);
param.Direction = System.Data.ParameterDirection.Output;
command.Parameters.Add(param);
...

您会看到,我们为返回的参数显式分配了一个属性方向。第一个获取变量“id”的值,并且是传递给存储过程的参数。

第二个参数由存储过程返回,因此没有为该参数值分配任何值,并且方向设置为 "System.Data.ParameterDirection.Output"

We actually do the configuration of the parameters more explicitly, something like this

System.Data.OracleClient.OracleCommand command = new System.Data.OracleClient.OracleCommand("PACKAGE_NAME.STORED_NAME");
command.CommandType = System.Data.CommandType.StoredProcedure;

System.Data.OracleClient.OracleParameter param;
param = new System.Data.OracleClient.OracleParameter("PARAM_NAME_ID", System.Data.OracleClient.OracleType.Number);
param.Value = id;
command.Parameters.Add(param);

param = new System.Data.OracleClient.OracleParameter("PARAM_NAME_RETURN_COUNT", System.Data.OracleClient.OracleType.Number);
param.Direction = System.Data.ParameterDirection.Output;
command.Parameters.Add(param);
...

You see, there is a property direction which we explicitly assign for the parameter that is being returned. The first gets the value of a variable "id" and is a parameter that gets passed TO the stored procedure.

The 2nd one is being returned by the stored procedure, therefore no value is assigned to that parameter value and the direction is set to "System.Data.ParameterDirection.Output"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文