C#WPF登录SQL bool方法获取用户 [英] C# WPF login SQL bool method get the user

查看:112
本文介绍了C#WPF登录SQL bool方法获取用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个登录方法来检查用户是否是我的数据库中的成员。我的登录方法只是一个bool,我想知道如何获取当前用户数据,以便我可以将它传递到下一个窗口。我正在检查文本框以查看文本是否有效。

我知道我可以解决这个问题的一种方法是更改​​我的登录方法,以便它返回一个用户然后我会拥有该特定用户但我希望通过检查数据是否在数据库中来查看是否可以获取用户数据。

这就是我所拥有的:

Hello all,
I have a login method to check if a user is a member in my DB. My login method is only a bool and I am wondering how can I grab that current users data so I can pass it to the next window. I am checking textboxes to see if the text is valid.
One way I know I could solve this is to change my login method so it returns a user then I would have that specific user but I would like to see if I could grab the user data by checking if the data is in the DB.
Here is what I have:

//Login XAML window.
private void BtnLoginUser_Click(object sender, RoutedEventArgs e){
            if (string.IsNullOrEmpty(txtUsername.Text)) {
                //verify and enter username.
                MessageBox.Show("Enter your username.", "Empty", MessageBoxButton.OK, MessageBoxImage.Information);                txtUsername.Focus();
                return;
            }
            else if (string.IsNullOrEmpty(txtPassword.Password)) {
                MessageBox.Show("Enter your password.", "Empty", MessageBoxButton.OK, MessageBoxImage.Information);                txtPassword.Focus();
                return;
            }
            else {
                try {
                    if(SQLuserAccess.UserLogin(txtUsername.Text, txtPassword.Password)){

                    }
                }
            }
        }

//SQL login method
        public static bool UserLogin(string username, string password) {
            bool valid = false;
            //SQL Login Query.
            string SQLloginQuery = "SELECT * FROM Users WHERE Username=@username AND Password=@password";
            SqlCommand cmdLogin = new SqlCommand(SQLloginQuery, connection);
            cmdLogin.Parameters.AddWithValue("@username", username);
            cmdLogin.Parameters.AddWithValue("@password", password);
            try {
                connection.Open();
                int result = (int)cmdLogin.ExecuteScalar();
                if (result > 0) {
                    valid = true;
                    MessageBox.Show("Login success");
                }
                else   MessageBox.Show("Login Failed");
            }
            catch (Exception ex) {
                ex.Message.ToString();
                throw ex;
            }
            finally{
                connection.Close();
            }
            return valid;
        }
//This is a method I use to get the user
        public static User GetUserById(int userId{            
            string SQLreadQuery = "SELECT Username, Password, IsAdmin, UserCreatedDate " +
                                  "FROM Users WHERE UserId = " + userId; //or SELECT ea column or *.
            SqlCommand cmdRead = new SqlCommand(SQLreadQuery, connection);
            try{
                connection.Open();
                SqlDataReader reader = cmdRead.ExecuteReader(CommandBehavior.SingleRow);
                if(reader.Read()){
                    User user = new User();
                    user.UserID = Convert.ToInt32(reader["UserId"]);
                    user.Username = reader["Username"].ToString();
                    user.Password = reader["Password"].ToString();
                    user.IsAdmin = Convert.ToBoolean(reader["IsAdmin"]);
                    user.UserCreatedDate = Convert.ToDateTime(reader["UserCreatedDate"]);
                    return user;
                }
                else{
                    return null;
                }
            }
            catch(Exception ex){
                ex.Message.ToString();
                return null;
            }
            finally{
                connection.Close();
            }
        }





我的尝试:



我知道我可以通过返回一个用户而不是Bool来解决这个问题,但我想知道我是否可以这样做而不是

也许如果我回来使用GetUserById()的用户对象然后我可以检索数据。

我想将数据存储在数据表中吗?或者只是在客户端对象中



What I have tried:

I know i can solve this by returning a User instead of Bool but i would like to see if i can do it like this instead
Maybe if i return a user object with GetUserById() then i could retrieve the data.
do i want to store the data in a datatable? or just in a client object

推荐答案

创建一个包含所需用户数据的静态类。



当用户登录时,将所需的用户数据捕获到静态类中,您仍然可以从登录方法返回 bool
Create a static class that contains the desired user data.

When the user logs in, capture the desired user data into your static class, and you can still return a bool from your login method.


这篇关于C#WPF登录SQL bool方法获取用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆