2

I'm looking to populate a datatable with the values of a refcursor parameter UserRole from a stored procedure "spValidateDBA" but it's giving me this error every time:

Column 'UserRole' does not belong to table.

C# code:-

string sConnectionString = "Data Source=XE;User ID=sys;Password=system;DBA PRIVILEGE=sysdba";
        OracleConnection myConnection = new OracleConnection(sConnectionString);
        OracleCommand myCommand = new OracleCommand("spValidateDBA", myConnection);
        myCommand.CommandType = CommandType.StoredProcedure;
        myCommand.CommandText = "spValidateDBA";
        myCommand.Parameters.Add("UserId", OracleDbType.Varchar2, 50);
        myCommand.Parameters["UserId"].Value = txtUsrId.Text.ToString().ToUpper();
        myCommand.Parameters.Add("UserRole",OracleDbType.RefCursor, 50).Direction = ParameterDirection.Output;
        myCommand.Parameters.Add("UserIdOut", OracleDbType.Varchar2, 50).Direction = ParameterDirection.Output;
        var rolechk = false;
        string checkrole = "DBA";
       myConnection.Open();
       myCommand.ExecuteReader();
        // Create the OracleDataAdapter
        OracleDataAdapter da = new OracleDataAdapter(myCommand);
        DataTable dt = new DataTable();
        da.Fill(dt);  // Trying to populate a DataTable with refcursor UserRole.
        if (myCommand.Parameters["UserIdOut"].Value.ToString().ToUpper() == txtUsrId.Text.ToString().ToUpper())
            {
                CustomMsgbox.Show("1", "DB Utilities Tool", "OK", "Cancel");
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["UserRole"].ToString().ToUpper().Equals(checkrole)==true)//getting the error "Column 'UserRole' does not belong to table." here
                   {

                        CustomMsgbox.Show("\tLogin Successful..!!\t" + Environment.NewLine + "Welcome to DB Utilities Tool", "DB Utilities Tool", "OK", "Cancel");
                        DBA dba = new DBA();
                        dba.Show();
                        this.Hide();
                        rolechk = true;
                        break;

                    }
                }

                if (!rolechk)
                {

                    CustomMsgbox.Show("Insufficient privileges", "DB Utilities Tool", "OK", "Cancel");
                    myConnection.Close();
                }


        else
            CustomMsgbox.Show("Please enter correct User ID/Password", "DB Utilities Tool", "OK", "Cancel");

    }

Stored procedure spValidateDBA

create or replace PROCEDURE spValidateDBA(
    UserId IN VARCHAR2,
UserRole OUT SYS_REFCURSOR,
 UserIdOut OUT VARCHAR2)
  AS
BEGIN
select USERNAME into UserIdOut from DBA_USERS DU where DU.USERNAME=UserId;
OPEN UserRole FOR
select GRANTED_ROLE from DBA_USERS DU,DBA_ROLE_PRIVS DRP where DU.USERNAME=UserId AND DU.USERNAME=DRP.GRANTEE;
  END spValidateDBA;
4

1 回答 1

0

您只检索GRANTED_ROLE游标查询中的一列:

OPEN UserRole FOR select GRANTED_ROLE from DBA_USERS DU,.......

但是您正试图UserRole从游标中获取一列。

if (dr[" UserRole "].ToString().ToUp.........

那里没有任何列命名UserRole,您只能 fetch GRANTED_ROLE

于 2014-04-20T08:59:50.077 回答