示例#1
0
        private void btngps_Click(object sender, RoutedEventArgs e)
        {
            gpsmap scgpsmap = new gpsmap();

            scgpsmap.Show();
            this.Close();
        }
        private void Btnlogin_Click(object sender, RoutedEventArgs e)
        {
            string user, password;

            user     = txtUsername.Text;
            password = txtPassword.Password;
            user     = Console.ReadLine();
            password = Console.ReadLine();

            SqlConnection sqlCon = new SqlConnection(@"Data Source=LAPTOP-K2AV9JKV\SQLEXPRESS; Initial Catalog=LoginDB; Integrated Security=True");

            try
            {
                if (sqlCon.State == System.Data.ConnectionState.Closed)
                {
                    sqlCon.Open();
                }
                String     query  = "SELECT * FROM tblUser WHERE username=@Username AND password=@Password";
                SqlCommand sqlCmd = new SqlCommand(query, sqlCon)
                {
                    CommandType = System.Data.CommandType.Text
                };
                sqlCmd.Parameters.AddWithValue("@Username", txtUsername.Text);
                sqlCmd.Parameters.AddWithValue("@Password", txtPassword.Password);
                //int count = Convert.ToInt32(sqlCmd.ExecuteScalar());

                //sqlCon.Open();

                SqlDataReader sdr = sqlCmd.ExecuteReader();

                if ((sdr.Read() == true))

                {
                    sdr.Read();
                    //user = sdr.GetString(1);  // Name string
                    //password = sdr.GetString(2); // Password string
                    gpsmap scgpsmap = new gpsmap();
                    scgpsmap.Show();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Username or password is incorrect!!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sqlCon.Close();
            }
        }