winform程序无法从sql读取数据

时间:2017-02-07 11:10:47

标签: c# sql-server winforms

我有一个像这样创建的SQL表

create table users
(username varchar(20) not null,
pass varchar(30) null,
phoneNum char(8) null,
email varchar(50) null,
name varchar(50) null,
membership varchar(3) null,
rating char(1) null,
primary key(username))

我还有一个试图从中读取数据的winform程序。我目前有这个:

public DataTable loadUserData()
    {
        using (SqlConnection conn = new SqlConnection())
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                using (SqlDataAdapter dataAdapter = new SqlDataAdapter())
                {
                    try
                    {
                        //Define connection object
                        conn.ConnectionString = CONNECTION_STRING;

                        //Define command object
                        cmd.CommandText = "Select * from users";
                        cmd.Connection = conn;

                        //Define data adapter object
                        dataAdapter.SelectCommand = cmd;

                        //Open connection
                        conn.Open();

                        //Execute command
                        userDataTable.Clear();
                        dataAdapter.Fill(userDataTable);
                    }//try
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }//using sql command
        }//using sql connection
        return userDataTable;
    }

这是能够加载数据(我认为)。然后在我需要使用数据的另一种形式,我有这样的事情:

 DataTable dt = db.loadUserData();
 foreach(DataRow dr in dt.Rows)
 {
     else if (txtPassword.Text == dr["pass"].ToString())
                {
                    //code to login
                }
 }

但我一直收到这样的错误 enter image description here

只有正确输入密码才会出现此错误。我在这里做错了什么或为什么它不起作用?

这是我完整的foreach循环:

Database db = new Database(); //Database is the class where loadUserdata is found
int count = 0;
        DataTable udt = db.loadUserData();
        DataTable rdt = db.loadRoomsData();
        foreach(DataRow udr in udt.Rows)
        {
            if (txtUsername.TextLength == 0)
            {
                lblVerify.Visible = true;
                lblVerify.Text = "Please enter a username";
                txtUsername.Focus();
                break;
            }
            else if (txtUsername.Text.ToLower() == udr["username"].ToString())
            {
                if (txtPassword.TextLength == 0)
                {
                    lblVerify.Visible = true;
                    lblVerify.Text = "Please enter the password";
                    txtPassword.Focus();
                    break;
                }
                else if (txtUsername.Text.ToLower() == "admin" && txtPassword.Text == "admin")
                {
                    Administrator admin = new Administrator();
                    admin.Show();
                    this.Hide();
                    break;
                }
                else if (txtPassword.Text == udr["pass"].ToString() && checkBooking() == true)
                {
                    Receipt rcpt = new Receipt(udr["username"].ToString(), backColor);
                    rcpt.Show();
                    this.Hide();
                    break;
                }
                else if (checkFull() == true)
                {
                    lblVerify.Visible = true;
                    lblVerify.Text = "All our hotels are currently fully booked. Sorry for the incovenience caused";
                    break;
                }
                else if (txtPassword.Text == udr["pass"].ToString())
                {
                    Main hotel = new Main(txtUsername.Text, backColor);
                    hotel.Show();
                    this.Hide();
                    break;
                }
                else
                {
                    lblVerify.Visible = true;
                    lblVerify.Text = "Password does not match Username. Please try again";
                    txtPassword.Focus();
                    txtPassword.SelectAll();
                    break;
                }
            }
            else if(udr["username"].ToString() != txtUsername.Text)
            {
                count++;
            }
        }

我调试了程序,出现错误的确切行是

`else if (txtPassword.Text == dr["pass"].ToString())'

1 个答案:

答案 0 :(得分:0)

好的抱歉,我发现了错误。我的checkBooking()函数有问题,我想我知道如何解决它。谢谢大家。