c# - 如何从 C# 中的 ExecuteOracleNonQuery 获取 rowid

标签 c# .net oracle

我正在 C# 中使用 ExecuteOracleNonQuery 使用存储过程将记录插入到我的 Oracle 数据库中,但似乎无法返回 ROWID。

在 C# 中...

using (OracleConnection oc= 
    new OracleConnection(AppConfiguration.ConnectionString))
{
    OracleCommand myCommand = new OracleCommand("PKG_TEST.INSERT", oc);
    myCommand.CommandType = CommandType.StoredProcedure;
    myCommand.Parameters.AddWithValue("p_Id", allAssets.Id);
    myCommand.Parameters.AddWithValue("p_Description", allAssets.Description);
    OracleString rowId;
    try
    {
        myConnection.Open();
        result = myCommand.ExecuteOracleNonQuery(out rowId);
        allAssets.RowId = rowId.ToString();
    }
    catch(System.Exception ex)
    {
        Console.WriteLine(ex.StackTrace);
    }
    finally
    {
        myConnection.Close();
    }
}
return result;

Oracle包中的过程...

PROCEDURE insert (
  p_id               IN   ALL_ASSETS.id%TYPE,
  p_description      IN   ALL_ASSETS.description%TYPE
)
IS
BEGIN
  INSERT INTO ALL_ASSETS
              (id, description)
       VALUES (p_id, p_description);
END insert;

谁能解释一下 ROWID 是如何返回的吗?

编辑 - 我现在已将代码更改为上面的代码,但仍然没有 ROWID 返回。此外,仅插入一条记录。

谢谢。

最佳答案

为什么你期望它返回 rowid ?您在数据库端没有要求它,并且过程不会返回 ROWID 并且无论如何也不知道要返回哪一个。

自动返回受影响行的 ROWID 可以用于 INSERT/UPDATE/MERGE,也许还可以用于 DELETE(尽管我不确定这种情况下的意义)。

您可以使用 INSERT INTO ALL_ASSETS (id, description) VALUES (p_id, p_description) RETURNING ROWID INTO v_rowid;

v_rowid 将是 PL/SQL 中的变量,您可以将其作为附加输出参数从数据库返回,或将过程转换为函数。

关于c# - 如何从 C# 中的 ExecuteOracleNonQuery 获取 rowid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1110289/

相关文章:

java - Java 中的准备语句连接

c# - String.Format ("Your query {0} with {1} Placeholders", theQuery, results.Count) 在 XSLT 中等效

c# - 带有 "any cpu"编译选项的 SQLite [未选中 "prefer 32 bit"选项时在 64 位机器上崩溃]

c# - 给定边界矩形、起始角和扫角,如何确定圆弧端点

c# - 导航到一个页面 n 次

c# - await Task.WhenAll 不等待所有任务完成

c# - winform应用程序中按钮不可见

c# - 捕获线程未处理的异常

c# - 错误 System.Data.OracleClient 在安装设置时需要 Oracle 客户端软件版本 8.1.7 或更高版本

sql - 计算 pl/sql 中游标的行数