Example #1
0
        private void RegisterButton_Click(object sender, RoutedEventArgs e)//注册
        {
            //打开数据库并建立/打开表
            string           dbFilename = "myQQdb.db";
            SQLiteConnection dbCore     = new SQLiteConnection("data source=" + dbFilename);

            dbCore.Open();
            string        commandText = "CREATE TABLE IF NOT EXISTS users(ID CHAR(10) NOT NULL, PASSWORD VARCHAR(20) NOT NULL);";
            SQLiteCommand cmd         = new SQLiteCommand(commandText, dbCore);
            int           result      = cmd.ExecuteNonQuery();

            string tempID            = this.NameTextBox.Text;
            string tempPassword      = this.PasswordBox.Text;
            string tempPasswordCheck = this.PasswordCheckBox.Text;

            if (!string.IsNullOrEmpty(tempID) && !string.IsNullOrEmpty(tempPassword) && !string.IsNullOrEmpty(tempPasswordCheck)) //判断输入是否为空
            {
                if (tempPassword != tempPasswordCheck)                                                                            //判断两次输入密码是否相同
                {
                    messegebox warning = new messegebox("两次密码输入不相同");
                    warning.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                    warning.Show();
                }
                else
                {
                    //向数据库中添加信息
                    cmd.CommandText = "INSERT INTO users(ID,PASSWORD) VALUES(@ID, @PASSWORD)";//添加数据
                    cmd.Parameters.Add("ID", DbType.String).Value       = tempID;
                    cmd.Parameters.Add("PASSWORD", DbType.String).Value = tempPassword;
                    result = cmd.ExecuteNonQuery();
                    if (result == 1)
                    {
                        messegebox confirm = new messegebox("注册成功");
                        confirm.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                        confirm.Show();
                        dbCore.Close();
                        this.Close();
                    }
                }
            }
            else
            {
                //错误提示
                messegebox warning2 = new messegebox("用户名或密码不能为空");
                warning2.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                warning2.Show();
            }
        }
        private void LoginButton_Click(object sender, RoutedEventArgs e) //登录按钮
        {
            string tempName = this.NameTextBox.GetLineText(0);           //当前输入的用户名和密码
            string tempPasw = this.PasswordsBox.Password;

            if (!string.IsNullOrEmpty(tempName) && !string.IsNullOrEmpty(tempPasw))//判断是否为空
            {
                //数据库加载
                string           dbFilename = "myQQdb.db";
                SQLiteConnection dbCore     = new SQLiteConnection("data source=" + dbFilename);
                dbCore.Open();
                string           commandText  = "select * from users;";
                SQLiteCommand    cmd          = new SQLiteCommand(commandText, dbCore);
                int              result       = cmd.ExecuteNonQuery();
                SQLiteDataReader reader       = cmd.ExecuteReader();
                bool             signforlogin = false;
                while (reader.Read())
                {
                    //检查账号密码是否正确
                    string checkID       = (string)reader["ID"];
                    string checkPassword = (string)reader["PASSWORD"];
                    if (checkID == tempName && checkPassword == tempPasw)
                    {
                        signforlogin = true;
                    }
                }
                if (signforlogin == true)
                {
                    string        strRe  = null;
                    string        sendMe = "q" + tempName;
                    ServerConnect tempSC = new ServerConnect();
                    strRe = tempSC.ServerQuery(sendMe);
                    Console.WriteLine(strRe);
                    if (strRe == "n")
                    {
                        //登录,告知服务器上线信息
                        string        strReceive = null;
                        string        sendMess   = tempName + "_net2019";
                        ServerConnect tempsev    = new ServerConnect();
                        strReceive = tempsev.ServerQuery(sendMess);
                        if (strReceive == "lol")
                        {
                            MainWindow a = new MainWindow(tempName);
                            a.Show();
                            this.Close();
                        }
                        else
                        {
                            //错误提示
                            messegebox warning = new messegebox("登录失败:未知错误");
                            warning.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                            warning.Show();
                        }
                    }
                    else
                    {
                        //错误提示
                        messegebox warning = new messegebox("账号已在线,无法登陆");
                        warning.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                        warning.Show();
                    }
                }
                else
                {
                    //错误提示
                    messegebox warning = new messegebox("用户名或密码错误");
                    warning.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                    warning.Show();
                }
            }
            else
            {
                //错误提示
                messegebox warning = new messegebox("用户名或密码为空");
                warning.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                warning.Show();
            }
        }