C#和Oracle,登录表单:由于对象的当前状态,操作无效

evi*_*ber 2 c# oracle state object

  private void bLogIn(object sender, EventArgs e)
    {

        string logging = "select * from CLIENT where LOGIN='" + this.t_Login.Text + "' and PASSWORD='" + this.t_Password.Text + "' ;";

        OracleConnection conn = new OracleConnection("Data Source=XXX/orcl;User Id=XXX;Password=XXX;");
        OracleCommand cmd = new OracleCommand();

        cmd.CommandText = logging;
        cmd.Connection = conn;
        try
        {
            conn.Open();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }



        int count = 0;

        OracleDataReader reader = cmd.ExecuteReader();  // At this line there is the error: Operation is not valid due to the current state of the object

        while (reader.Read())
        {
           count = count + 1;
        }
        if (count == 1)
        {
        MessageBox.Show("Welcome");
        }
        else
        {
            MessageBox.Show("Wrong Password");
        }
        conn.Dispose();
    }
Run Code Online (Sandbox Code Playgroud)

这是适用于MySQL的代码,但在转换为Oracle之后它无法正常工作.我究竟做错了什么?差异在哪里.它应该像MySQL一样容易,对吧?

为什么"OracleDataReader reader = cmd.ExecuteReader();" 导致错误:"由于对象的当前状态,操作无效"???

Sac*_*chu 7

您没有确定与cmd的连接..请尝试下面的代码

OracleCommand cmd = new OracleCommand();
cmd.connection = conn;
cmd.CommandText = logging;
int count = 0;
OracleDataReader reader = cmd.ExecuteReader();
Run Code Online (Sandbox Code Playgroud)