首页 > 解决方案 > System.InvalidOperationException:“ExecuteReader:连接属性尚未初始化。”

问题描述

我是一个初学者并尝试登录,但它不起作用,我尝试了“使用”命令,我搜索并发现我可能需要在执行阅读器中建立连接。这是我得到的错误:

System.InvalidOperationException: 'ExecuteReader: Connection property has not been initialized.' on the line of code  using (OleDbDataReader reader = Com.ExecuteReader()).

我写的代码:


 Con.Open();
            OleDbCommand Com = new OleDbCommand();
            Com.CommandText = "Select * from Admin where s_id='" + textBox1 + "'and password='" + textBox4 + "' ";


            using (OleDbDataReader reader = Com.ExecuteReader()) //the reader will rea the data comes from the query
            {
                {
                    int count = 0; //to count the number of values coming, to count if there is  ID and pssword present once
                    while (reader.Read())  //gives boolean variable until it gives true, it will continue in loop.
                    {
                        count = count + 1;

                    }
                    if (count == 1)
                    {
                        MessageBox.Show("ID and password are correct");
                    }
                    if (count > 1)
                    {
                        MessageBox.Show("duplicate ID and password");
                    }
                    else
                    {
                        MessageBox.Show("ID and password are correct");

                    }
                }
            }
            Con.Close();

        }
    }

很抱歉写的杂乱无章,这是我第一次。

错误信息照片

标签: connectionexecutereader

解决方案


推荐阅读