Oracle存储过程不返回任何值

时间:2014-04-14 12:28:45

标签: c# oracle validation login

我从c#登录表单调用oracle存储过程,但它没有返回任何值。因此,下面的dt.rows.count始终为0,if-else语句的if部分永远不会被执行,它总是到达else部分。

C#代码:

String sConnectionString = "Data Source=XE;User ID=system;Password=system;";

OracleConnection myConnection = new OracleConnection(sConnectionString);
myConnection.Open();

OracleCommand myCommand = new OracleCommand("spValidateDBA", myConnection);

myCommand.CommandType = CommandType.StoredProcedure;
myCommand.CommandText = "spValidateDBA";
myCommand.Parameters.Add("UserName", OracleDbType.Varchar2, 50);
myCommand.Parameters["UserName"].Value = txtUsrId.Text.ToString();
myCommand.Parameters.Add("UserPassword", OracleDbType.Varchar2, 50);
myCommand.Parameters["UserPassword"].Value = txtPassword.Text.ToString();
myCommand.Parameters.Add("DataCount", OracleDbType.Int32, 20).Direction =
ParameterDirection.Output;
OracleDataReader myReader = myCommand.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(myReader);


if (dt.Rows.Count>0)
    {
        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();
    }

else
    CustomMsgbox.Show("Please enter correct Username/password",
    "DB Utilities Tool", "OK", "Cancel");
        myConnection.Close();
}    

Oracle存储过程:

create or replace PROCEDURE spValidateDBA(
    UserName IN VARCHAR2,
    UserPassword IN VARCHAR2,
  DataCount OUT INTEGER)
  AS
BEGIN
select count(*) into DataCount from DBA_USER where USER_NAME=UserName and
    USER_PWD=UserPassword and USER_ROLE = 'DBA';
    END spValidateDBA;

请注意,表DBA_USER当前包含1行,其值为:

USER_NAME   USER_PWD    USER_ROLE
   dask       password     DBA

因此,当登录表单的输入为dask(用户名)&时,上述存储过程中的DataCount应该返回1行,其值为1。密码(userpassword),但它没有返回任何内容。

有人可以告诉我这里哪里出错吗?我真的需要很快解决这个问题。

1 个答案:

答案 0 :(得分:0)

您没有从SP返回任何结果集,因此您的数据表将始终为空。您将在输出参数中返回值。使用ExecuteNonQuery然后从参数中读取值。

试试这个

myCommand.ExecuteNonQuery();
int dataCount = 0;
if(myCommand.Parameters["DataCount"] ! =null)
{
  dataCount = Convert.ToInt32(myCommand.Parameters["DataCount"].Value);
}

if(dataCount > 0)
{
   CustomMsgbox.Show("...");
}