【问题标题】:.net: Storing a .net datatable with dataAdapter from a stored procedure.net:使用存储过程中的 dataAdapter 存储 .net 数据表
【发布时间】:2017-09-14 20:05:35
【问题描述】:

我有一个名为

的存储过程
GET_CLIENT(IN VARCHAR2, IN VARCHAR2, OUT SYS REF_CURSOR)

我正在尝试将他们的结果存储在ConnectAndQuery 数据表中。

导致这个错误:

System.Data.Odbc.OdbcException: 错误 [42000] [Microsoft][Oracle ODBC 驱动程序][Oracle]
ORA-00900: 无效的 SQL 语句

在 ConexionBD.ConnectAndQuery(String layerName, Decimal idElemento, String idElementoString, String conexion)。

我的代码:

public DataTable ConnectAndQuery(string layerName, decimal idElemento, string idElementoString, string conexion)
    {
        Logger.Debug("App_Code/ConexionBD.cs: using (OdbcConnection connection = new OdbcConnection(Driver={Microsoft ODBC for Oracle}; + conexion ");
        using (OdbcConnection conn = new OdbcConnection(conexion))
        {
            try
            {
                using (OdbcCommand Command = new OdbcCommand("{ call PKG_GEONET_REPORTS.GET_ORDINARY_CLIENT(?, ?, ?) }", conn))
                {
                    Command.CommandType = CommandType.StoredProcedure;
                    Command.Parameters.Add("client_in", OracleType.VarChar).Value = idElemento.ToString();
                    Command.Parameters.Add("layer_in", OracleType.VarChar).Value = layerName;
                    Command.Parameters.Add("client_data", OracleType.Cursor).Direction = ParameterDirection.Output;
                    using (OdbcDataAdapter da = new OdbcDataAdapter(Command))
                    {
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        return dt;
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
    }

【问题讨论】:

  • 您是否调试并检查了哪一行代码引发了异常以及异常的确切消息是什么?
  • 你真的需要 OdbcCommand 吗?不能使用 OracleCommand 吗?
  • 抛出发生在那里:da.Fill(ds, "result_name"); (结果名称??????)。 OracleCommand 没问题
  • 那里需要哪些参数?

标签: c# .net oracle datatable


【解决方案1】:

在命令中更改 SQL 文本。

da.SelectCommand = new OdbcCommand(
    "{ call PKG_NAME.GET_CLIENT(?, ?, ?) }",
    conn);
  • 您的第一个参数“layer_in”应该是OracleType.VarChar 以匹配您存储的 过程第一个参数DataType,不应该吗?
  • 如果您不知道结果集的名称,则应使用 da.Fill(ds); 而不是 da.Fill(ds,"RESULT_NAME?");

【讨论】:

  • 忘记了 ODBC.NET 按序号位置而不是名称处理参数。已修复。
  • 问题出在这里: 在那里发生抛出:da.Fill(ds, "result_name"); (result_name?????),我想
  • 在您调用 da.Fill(ds,"result_name) 之前,SQL 命令不会实际执行。因此,如果您的命令语法错误,您会在此处看到命令执行错误。您看到的 Oracle 错误与语法错误有关,这可能是因为您的第一个参数上的数据类型不正确,或者调用存储过程的格式不正确,我在回答中指出了修复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-24
  • 2014-03-13
  • 2011-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多