C# 在Oracle中正确使用DataReader?

C# 在Oracle中正确使用DataReader?,c#,oracle,code-behind,C#,Oracle,Code Behind,我的任务是将我们的一些C代码转换为使用Oracle而不是SQL Server。我在甲骨文的经验是。。。哦。。。2天 所以,这里是我所拥有的: private void LoadPreferences() { // inital load of users function and role based on last action performed in database try { OracleConnection oConn = GetConnectio

我的任务是将我们的一些C代码转换为使用Oracle而不是SQL Server。我在甲骨文的经验是。。。哦。。。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解析正确,因为我在单步执行代码时检查了它。

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

select Function_ID,Role_ID from (  your inner select)

该错误是由于@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);
...

我想那将来自@strUserID;如果您将其更改为:strUserID,并将addWithValue更改为just strUserID,会更高兴吗?现在它告诉我SQL命令没有正确结束。您不希望在命令末尾使用分号,但这应该会给您ORA-00911。除非你的司机抓到了。如果你想知道要点,可以将这两个信息组合成一个答案。他们一起解决了我的问题。我知道Select*有效,因为它在我的Oracle SQL Developer应用程序中有效。我首先在那里写了它,以确保我的SQL是准确的。太好了!非常感谢!