简体   繁体   English

使用来自Oracle存储过程的refcursor值填充C#数据表

[英]Populating C# datatable with refcursor values from an Oracle Stored Procedure

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: 我想用存储过程“ spValidateDBA”中的refcursor参数UserRole的值填充数据表,但每次都会出现此错误:

Column 'UserRole' does not belong to table. 列“ UserRole”不属于表。

C# code:- C#代码:-

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 存储过程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;

You are retrieving only one column GRANTED_ROLE in the cursor query: 您仅在游标查询中检索GRANTED_ROLE列:

OPEN UserRole FOR select GRANTED_ROLE from DBA_USERS DU, ....... OPEN UserRole FOR从DBA_USERS DU中选择GRANTED_ROLE ,.......

however you are trying to fetch an UserRole column from the cursor. 但是,您正在尝试从游标中获取UserRole列。

if (dr[" UserRole "].ToString().ToUp......... 如果(dr [“ UserRole ”] .ToString()。ToUp .........

There is not any column named UserRole there, you can only fetch GRANTED_ROLE . 那里没有任何名为UserRole列,您只能获取GRANTED_ROLE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM