odp.net 可以将参数传递给布尔型 pl/sql 参数吗?

发布于 2024-09-06 13:17:35 字数 53 浏览 4 评论 0原文

是否可以将 OracleParameter 正确传递给 pl/sql 存储过程中的布尔参数?

Is it possible to correctly pass an OracleParameter to a boolean parameter in a pl/sql stored procedure?

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

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

发布评论

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

评论(2

何以笙箫默 2024-09-13 13:17:35

我使用以下解决方法来绕过此限制:

  1. 使用匿名块包装函数调用。
  2. 返回包含 1 或 0 的输出变量。
  3. 读取输出变量并将其转换为布尔值。

以下是一些示例代码:

using (var connection = new OracleConnection("<connection string>"))
{
    var command = new OracleCommand();
    command.Connection = connection;
    command.CommandText = 
        "declare v_bool boolean;" + 
        "begin " +
        "v_bool := auth_com.is_valid_username (:username); "+
        "if (v_bool = TRUE) then select 1 into :v_result from dual; end if; " +
        "if (v_bool = FALSE) then select 0 into :v_result from dual; end if; " +
        "end;";

    command.Parameters.Add(new OracleParameter { ParameterName = "username", OracleDbType = OracleDbType.NVarchar2, Size=512, Direction = ParameterDirection.Input });
    command.Parameters.Add(new OracleParameter { ParameterName = "v_result", OracleDbType = OracleDbType.Decimal, Direction = ParameterDirection.Output });     

    try
    {
        connection.Open();
        command.ExecuteNonQuery();
    }
    finally
    {
        connection.Close();
    }

    bool success = Convert.ToBoolean(((OracleDecimal)command.Parameters["v_result"].Value).ToInt32());
}

编辑:

来自 Oracle 的 Alex Keh,2013 年 10 月:

我们计划在托管提供程序中支持 ODP.NET 布尔值
短期内,可能是明年年中。

I used the following workaround to bypass this limitation:

  1. Wrap the function call using an anonymous block.
  2. Return an output variable containing 1 or 0.
  3. Read the output variable and cast it to boolean.

Here is some sample code:

using (var connection = new OracleConnection("<connection string>"))
{
    var command = new OracleCommand();
    command.Connection = connection;
    command.CommandText = 
        "declare v_bool boolean;" + 
        "begin " +
        "v_bool := auth_com.is_valid_username (:username); "+
        "if (v_bool = TRUE) then select 1 into :v_result from dual; end if; " +
        "if (v_bool = FALSE) then select 0 into :v_result from dual; end if; " +
        "end;";

    command.Parameters.Add(new OracleParameter { ParameterName = "username", OracleDbType = OracleDbType.NVarchar2, Size=512, Direction = ParameterDirection.Input });
    command.Parameters.Add(new OracleParameter { ParameterName = "v_result", OracleDbType = OracleDbType.Decimal, Direction = ParameterDirection.Output });     

    try
    {
        connection.Open();
        command.ExecuteNonQuery();
    }
    finally
    {
        connection.Close();
    }

    bool success = Convert.ToBoolean(((OracleDecimal)command.Parameters["v_result"].Value).ToInt32());
}

EDIT:

Alex Keh from Oracle, october 2013:

We're planning on supporting ODP.NET Boolean in the managed provider
in the near term, possibly in the middle of next year.

独留℉清风醉 2024-09-13 13:17:35

您不能在 SQL 中使用布尔参数。因此,调用接受或返回布尔值的存储过程在 SQL 中不起作用。在 pl/sql 块中使用这样的过程没有问题。

JCallico 答案添加:

我使用以下解决方法来绕过此限制:

  1. 使用匿名块包装函数调用。
  2. 返回包含 1 或 0 的输出变量。
  3. 读取输出变量并将其转换为布尔值。

以下是一些示例代码:

using (var connection = new OracleConnection("<connection string>"))
{
    var command = new OracleCommand();
    command.Connection = connection;
    command.CommandText = 
        "declare v_bool boolean;" + 
        "begin " +
        "v_bool := auth_com.is_valid_username (:username); "+
        "if (v_bool = TRUE) then select 1 into :v_result from dual; end if; " +
        "if (v_bool = FALSE) then select 0 into :v_result from dual; end if; " +
        "end;";

    command.Parameters.Add(new OracleParameter { ParameterName = "username", OracleDbType = OracleDbType.NVarchar2, Size=512, Direction = ParameterDirection.Input });
    command.Parameters.Add(new OracleParameter { ParameterName = "v_result", OracleDbType = OracleDbType.Decimal, Direction = ParameterDirection.Output });     

    try
    {
        connection.Open();
        command.ExecuteNonQuery();
    }
    finally
    {
        connection.Close();
    }

    bool success = Convert.ToBoolean(((OracleDecimal)command.Parameters["v_result"].Value).ToInt32());
}

编辑:

来自 Oracle 的 Alex Keh,2013 年 10 月:

我们计划在托管提供程序中支持 ODP.NET 布尔值
短期内,可能是明年年中。

You can not use boolean parameters in SQL. So calling an stored procedure that takes or returns a boolean value won't work in SQL. There is no problem using such a procedure from within a pl/sql block.

ADDED from JCallico answer:

I used the following workaround to bypass this limitation:

  1. Wrap the function call using an anonymous block.
  2. Return an output variable containing 1 or 0.
  3. Read the output variable and cast it to boolean.

Here is some sample code:

using (var connection = new OracleConnection("<connection string>"))
{
    var command = new OracleCommand();
    command.Connection = connection;
    command.CommandText = 
        "declare v_bool boolean;" + 
        "begin " +
        "v_bool := auth_com.is_valid_username (:username); "+
        "if (v_bool = TRUE) then select 1 into :v_result from dual; end if; " +
        "if (v_bool = FALSE) then select 0 into :v_result from dual; end if; " +
        "end;";

    command.Parameters.Add(new OracleParameter { ParameterName = "username", OracleDbType = OracleDbType.NVarchar2, Size=512, Direction = ParameterDirection.Input });
    command.Parameters.Add(new OracleParameter { ParameterName = "v_result", OracleDbType = OracleDbType.Decimal, Direction = ParameterDirection.Output });     

    try
    {
        connection.Open();
        command.ExecuteNonQuery();
    }
    finally
    {
        connection.Close();
    }

    bool success = Convert.ToBoolean(((OracleDecimal)command.Parameters["v_result"].Value).ToInt32());
}

EDIT:

Alex Keh from Oracle, october 2013:

We're planning on supporting ODP.NET Boolean in the managed provider
in the near term, possibly in the middle of next year.

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