如何使用sqlcommand工作ExecuteReader()方法没有任何异常或错误?

时间:2016-03-28 12:43:56

标签: c# sql-server-2008 c#-4.0 c#-3.0 c#-2.0

我正在使用c#进行简单的登录应用程序,因为我使用sql-server-2012构建了一个数据库连接类和相关的东西我使用下面给出的方法这个类有什么问题,即使我已经做了银行程序使用这些方法完美但但不是为什么?

namespace Stock
{
    class Dbs
    {
        public Dbs()
        {
            connect();
        }

        SqlConnection connection;
        SqlDataAdapter adapter;
        SqlDataReader dreader;
        DataTable dtbl;
        SqlCommand cmd;
        public SqlConnection connect()
        {
            connection = new SqlConnection("Data Source = .; Initial Catalog = stockdb ;Integrated Security = true");
            return connection;
        }

        public bool login(string username,string password)
        {
            bool ok = false;
            cmd = new SqlCommand("select * from administrator", connection);
            connection.Open();
            // here exception occurs
            cmd.Connection = connection;
            dreader = cmd.ExecuteReader();
            while (dreader.Read())
            {
                if (dreader["username"].ToString() == username && dreader["passwords"].ToString() == password)
                {


                    MessageBox.Show("welcome " + username + " login success ....!");
                    ok = true;

                }
                else
                {
                    MessageBox.Show("Get out of here...........");
                }
            }

            return ok;

        }



        }
}

0 个答案:

没有答案