在WPF App中根据用户名显示用户的SQL信息

时间:2019-06-23 01:21:02

标签: c# sql-server wpf xaml

我正在创建一个WPF应用程序,一个用户在创建一个帐户,并可以使用其用户名和密码登录。当用户成功登录其用户名后,他们在注册时输入的其他详细信息应显示在下一页上。到目前为止,无论使用什么用户名或密码,唯一显示的就是第一个注册用户的信息,但它应该基于登录的人。
更好的解释是,有用户A和用户B,当显示用户A登录信息时,无论何时用户B登录,无论如何,仍然显示用户A的信息,我希望用户B(以及所有后续用户)的信息在输入他的特定用户名时显示。

用于注册命令的C#

private void SubmitBtn_Click(object sender, RoutedEventArgs e)
        {
            if (tbStudentName.Text == "" || pbPassword.Password == "" || tbSchoolName.Text == "" || tbHouseName.Text == ""
                || tbProg.Text == "" || tbPhoneNumber.Text == "" || tbAddress.Text == "")
            {
                var dim = new Dim();
                dim.Show();
                this.Effect = new BlurEffect();

                var cmb = new Custom_MessageBoxes.CustomMsgBox2();
                cmb.ShowDialog();

                this.Effect = null;
                dim.Close();
            }

            else
            {
                Connect obj = new Connect();
                obj.conn.ConnectionString = obj.locate;
                obj.conn.Open();
                string InsertUser = "INSERT INTO tblSignUp values ('"+tbStudentName.Text+ "', '" + tbSchoolName.Text + "', '" + tbHouseName.Text + "', '" + tbProg.Text + "', '" + tbPhoneNumber.Text + "', '" + tbAddress.Text + "', '" + pbPassword.Password + "')";
                obj.cmd.Connection = obj.conn;
                obj.cmd.CommandText = InsertUser;
                obj.cmd.ExecuteNonQuery();
                obj.conn.Close();

                var dim = new Dim();
                dim.Show();
                this.Effect = new BlurEffect();

                var cmb = new Custom_MessageBoxes.RegistrationComplete();
                cmb.ShowDialog();

                this.Effect = null;
                dim.Close();
                Clear();
            }
        }

用于登录命令的C#

//Sign In button click event
        private void UserSignInBtn_Click(object sender, RoutedEventArgs e)
        {
            SqlConnection sqlCon = new SqlConnection(connectionString);


            try
            {
                Connect obj = new Connect();
                obj.conn.ConnectionString = obj.locate;

                obj.conn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter("SELECT COUNT (*) FROM tblSignUp WHERE StudentName = '"+tbID.Text+"' AND Password = '"+PB.Password+"'", obj.conn);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                if (dt.Rows[0][0].ToString() == "1")
                {
                    // Custom Message Box and Dim Effect
                    var jim = new Dim();

                    jim.Show();
                    this.Effect = new BlurEffect();

                    var lsmb = new Custom_MessageBoxes.LoginSuccessfulMsgBox();
                    lsmb.ShowDialog();

                    this.Effect = null;
                    jim.Close();

                    var User_Homepage = new User_Homepage();
                    NavigationService.Navigate(User_Homepage);
                }
                else
                {
                    // Custom Message Box and Dim Effect 2
                    var him = new Dim();

                    him.Show();
                    this.Effect = new BlurEffect();

                    var rmdlgb = new ReturnMessageDialogueBox();
                    rmdlgb.ShowDialog();

                    this.Effect = null;
                    him.Close();
                }


                obj.conn.Close();

            }
            catch(Exception ex)
            {
                using (EventLog eventlog = new EventLog("Application"))
                {
                    eventlog.Source = "SQL Error: From My Application";
                    eventlog.WriteEntry(ex.StackTrace, EventLogEntryType.Error, 101, 1);
                }
            }
            finally
            {
                sqlCon.Close();
            }
        }

我想要用户信息的页面

string connectionString = @"Data Source=HP;Initial Catalog=User_SignUpDB;Integrated Security=True;";


        public UHP()
        {
            InitializeComponent();


            Page1 p1 = new Page1();
            var pls = p1.tbID.Text;

            SqlConnection sqlCon = new SqlConnection(connectionString);
            sqlCon.Open();

            string query = "SELECT * FROM tblSignUP WHERE StudentName = StudentName and HouseName = HouseName";
            SqlCommand createCommand = new SqlCommand(query, sqlCon);


            SqlDataReader dr = createCommand.ExecuteReader();
            if (dr.Read())
            {
                nameTxt.Text = (dr["StudentName"].ToString());
                hseTxt.Text = (dr["HouseName"].ToString());
                progTxt.Text = (dr["Prog"].ToString());
            }


            sqlCon.Close();


        }

1 个答案:

答案 0 :(得分:0)

您的查询:

SELECT *
FROM tblSignUP
WHERE
    StudentName = StudentName
    AND HouseName = HouseName

没有参数传递给它;这只是一个硬编码的语句。

您正在比较WHERE子句中的等效字段,这使它变得多余,即您实际上只是在表中执行SELECT *。因此,您读入应用程序的内容始终只是返回的第一行。

您需要的是类似的东西

string query = "SELECT * FROM tblSignUP WHERE StudentName = @StudentName and HouseName = @HouseName";

SqlCommand createCommand = new SqlCommand(query, sqlCon);

createCommand.Parameters.Add(new SqlParameter("@StudentName", StudentName));
createCommand.Parameters.Add(new SqlParameter("@HouseName", HouseName));

我假设传递给StudentName构造函数(第二个参数)的HouseNameSqlParameter变量已经在您的代码中的某个地方定义了。