示例#1
0
        private void gdf(object sender, RoutedEventArgs e)
        {
            Mainpage mx = new Mainpage();

            mx.Show();
            this.Close();
        }
        private void login_btn(object sender, RoutedEventArgs e)
        {
            Mainpage bw = new Mainpage();

            /*if (username.Text == "" && password.Password == "")
             * {
             *  bw.Show();
             *  this.Close();
             *
             * }
             * else
             * {
             *  MessageBox.Show("Wrong username or password", "Login");
             *  username.Text = "";
             *  password.Password = "";
             * }
             */
            SqlConnection sqlcon = new SqlConnection(@"Data Source=DESKTOP-M4UMV3O;Initial Catalog=fall16;Integrated Security=True");

            try
            {
                if (sqlcon.State == ConnectionState.Closed)
                {
                    sqlcon.Open();
                }
                string     commandstring = "select COUNT(1) from phms WHERE username=@username AND password=@password";
                SqlCommand sqlcmd        = new SqlCommand(commandstring, sqlcon);
                sqlcmd.CommandType = CommandType.Text;
                sqlcmd.Parameters.AddWithValue("@username", username.Text);
                sqlcmd.Parameters.AddWithValue("@password", password.Password);
                int count = Convert.ToInt32(sqlcmd.ExecuteScalar());
                if (count == 1)
                {
                    Mainpage ad = new Mainpage();
                    ad.Show();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Username or Password Not Matched Try Again");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sqlcon.Close();
            }
        }