【问题标题】:Proper use of DataReader in Oracle?在 Oracle 中正确使用 DataReader?
【发布时间】:2016-06-21 01:35:20
【问题描述】:

我的任务是将我们的一些 C# 代码隐藏转换为使用 Oracle 而不是 SQL Server。我对 oracle 的体验是......哦...... 2 天。

所以,这就是我所拥有的:

private void LoadPreferences()
{  // inital load of users function and role based on last action performed in database

    try
    {
        OracleConnection oConn = GetConnection();

        //string selectSQL = "select Function_ID,Role_ID from vw_mos_DPL_Last_Selection where Lan_ID = '" + strUserID + "'";
        //string selectSQL = "select Function_ID,Role_ID from vw_mos_DPL_Last_Selection where Lan_ID = @strUserID";
        //string selectSQL = "SELECT WORK_ID, ROLE_ID, ACTIVITY_ID, LAN_ID, CREATE_TS FROM THN_DPL_DETAIL WHERE Lan_ID = @strUserID ORDER BY CREATE_TS DESC";
        string selectSQL = "SELECT * FROM(";
        selectSQL = selectSQL + "SELECT TB.BUS_ID, TL.LOB_ID, TD.ROLE_ID, TL.LOB_ID, TD.ACTIVITY_ID, TD.LAN_ID, TD.CREATE_TS ";
        selectSQL = selectSQL + "FROM THN_DPL_DETAIL TD ";
        selectSQL = selectSQL + "LEFT JOIN THN_ACTIVITY TA ON TD.Activity_ID = TA.Activity_ID ";
        selectSQL = selectSQL + "LEFT JOIN THN_ROLE TR ON TR.ROLE_ID = TA.Role_ID ";
        selectSQL = selectSQL + "LEFT JOIN THN_LOB TL On TL.LOB_ID = TA.LOB_ID ";
        selectSQL = selectSQL + "LEFT JOIN THN_BUSINESS TB ON TB.BUS_ID = TR.BUS_ID ";
        selectSQL = selectSQL + "WHERE Lan_ID = @strUserID ";
        selectSQL = selectSQL + "ORDER BY CREATE_TS DESC";
        selectSQL = selectSQL + ") WHERE ROWNUM = 1;";


        OracleCommand cmd = new OracleCommand(@selectSQL, oConn);
        cmd.Parameters.AddWithValue("@strUserID", strUserID);

        OracleDataReader reader;


        oConn.Open();
        reader = cmd.ExecuteReader();
        //read first line
        reader.Read();
        if (reader.HasRows == true)
        {
            //First, grab the bytes from the reader: AA = Bus_ID, BB = LOB_ID and CC = Role_ID
            byte AA = reader.GetByte(0);
            byte BB = reader.GetByte(1);
            byte CC = reader.GetByte(2);

            //Now set the SelectedValue of Business and re-query the LOB dropdown for eligible values
            CallBus_DrpDwnLst.SelectedValue = AA.ToString();
            LOBLoad();

            //Now set the SelectedValue of LOB since it's filled with eligible valuse
            CallLOB_DrpDwnLst.SelectedValue = BB.ToString();
            CallRoleLoad();

            //Now set the SelectedValue of Role since it's filled with eligible valuse
            CallRole_DrpDwnLst.SelectedValue = CC.ToString();

        }
        else //no rows found, clear selection
        {
            CallBus_DrpDwnLst.SelectedIndex = 0;
            CallLOB_DrpDwnLst.SelectedIndex = 0;
            CallRole_DrpDwnLst.SelectedIndex = 0;
        }
        //close the reader
        reader.Close();
        oConn.Close();
    }
    catch (Exception ex)
    {
        // Handle the error
        Response.Write(ex.Message);
        //ErrorLogging.WriteToEventLog(ex);
    }
}

我遇到的问题是在reader = cmd.ExecuteReader() 线上我收到了一个错误:

ORA-01036: 非法变量名/编号

谁能帮我解决这个问题?我知道 strUserID 可以正确解析,因为我在单步执行代码时检查了它。

【问题讨论】:

  • 我认为那将来自@strUserID;如果将其更改为 :strUserID,并将 addWithValue 更改为 strUserID,会不会更快乐?
  • 现在它告诉我“SQL 命令没有正确结束”。
  • 您不希望命令末尾有分号,但这应该会给您 ORA-00911。除非你的司机发现它。
  • 是的。如果您想要积分,请随时将这两个信息组合成一个答案。他们一起解决了我的问题。

标签: c# oracle code-behind


【解决方案1】:

错误是因为@strUserID 占位符中的@。 Oracle 使用冒号表示绑定变量,addWithValue() 调用应该使用普通的绑定名称。

分号也是语句分隔符,因此它不构成单个语句的一部分;这有时会导致 ORA-00911,但您的驱动程序似乎在 Oracle 抛出该错误之前给您“SQL 命令未正确结束”。

所以你的代码应该是:

...
        selectSQL = selectSQL + "WHERE Lan_ID = :strUserID ";
        selectSQL = selectSQL + "ORDER BY CREATE_TS DESC";
        selectSQL = selectSQL + ") WHERE ROWNUM = 1";

        OracleCommand cmd = new OracleCommand(@selectSQL, oConn);
        cmd.Parameters.AddWithValue("strUserID", strUserID);
...

【讨论】:

  • 完美!非常感谢!
【解决方案2】:

在您的查询中而不是执行 select * 给返回值名称以匹配您的 C# 过程所期望的,如

select Function_ID,Role_ID from (  your inner select)

【讨论】:

  • 我知道“Select *”有效,因为它适用于我的 Oracle SQL Developer 应用程序。我先把它写在那里以确保我的 SQL 是准确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
相关资源
最近更新 更多