Exemplo n.º 1
0
        private void btnCreateAcc_Click(object sender, EventArgs e)
        {
            if (txtDefUser.Text.Trim().Length == 0)
            {
                MessageBox.Show("Please enter your username ");
                txtDefUser.Focus();

            }
            else if (txtDefPass.Text.Trim().Length == 0)
            {
                MessageBox.Show("Please enter your password");
                txtDefPass.Focus();
            }
            string UserDef;
            UserDef = txtDefUser.Text;

            int UserStatus;

            string filename = "HauntedBuilding.mdf";
            string connectionInfo = String.Format(@"Data Source=(LocalDB)\v11.0;
                AttachDbFilename=|DataDirectory|\{0};Integrated Security=True;", filename);

            SqlConnection Cnn = new SqlConnection(connectionInfo);

            //SqlConnection Cnn = new SqlConnection();
            //Cnn.ConnectionString = "Data Source=WIN-PC;Initial Catalog=HauntedBuilding;Integrated Security=True";

            SqlCommand oCmd = new SqlCommand();

            oCmd.Connection = Cnn;
            oCmd.CommandTimeout = 0;
            oCmd.CommandType = CommandType.StoredProcedure;
            oCmd.CommandText = "spDefineAcc";
            oCmd.Parameters.Add("@UserName", SqlDbType.NVarChar);
            oCmd.Parameters.Add("@Password", SqlDbType.NVarChar);
            oCmd.Parameters["@UserName"].Value = txtDefUser.Text;
            oCmd.Parameters["@Password"].Value = txtDefPass.Text;

            try
            {
                Cnn.Open();
                oCmd.ExecuteNonQuery();
                MessageBox.Show("welcome " + txtDefUser.Text);
                UserStatus = 0;
                fmSelectGame ff = new fmSelectGame(UserDef, UserStatus);
                this.Hide();
                ff.ShowDialog();
                this.Close();

            }
            catch (SqlException ex)
            {

                MessageBox.Show(ex.Message);
                txtDefUser.Clear();
                txtDefPass.Clear();
                txtDefUser.Focus();
            }
            finally
            {
                Cnn.Close();
            }
        }
Exemplo n.º 2
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUser.Text.Trim().Length == 0)
            {
                MessageBox.Show("Please enter your username ");
                txtUser.Focus();

            }
            else if (txtPass.Text.Trim().Length == 0)
            {
                MessageBox.Show("Please enter your password");
                txtPass.Focus();
            }

            string Player;
            Player = txtUser.Text;
            int UStatus;

            string filename = "HauntedBuilding.mdf";
            string connectionInfo = String.Format(@"Data Source=(LocalDB)\v11.0;
                AttachDbFilename=|DataDirectory|\{0};Integrated Security=True;", filename);

            SqlConnection Cnn = new SqlConnection(connectionInfo);

            //Cnn.ConnectionString = "Data Source=WIN-PC;Initial Catalog=HauntedBuilding;Integrated Security=True";

            SqlCommand oCmd = new SqlCommand();
            oCmd.Connection = Cnn;
            oCmd.CommandTimeout = 0;
            oCmd.CommandType = CommandType.StoredProcedure;
            oCmd.CommandText = "spLogin";
            oCmd.Parameters.Add("@UserName", SqlDbType.NVarChar);
            oCmd.Parameters.Add("@Password", SqlDbType.NVarChar);
            oCmd.Parameters["@UserName"].Value = txtUser.Text;
            oCmd.Parameters["@Password"].Value = txtPass.Text;
            SqlDataReader DR;

            try
            {
                Cnn.Open();
                DR = oCmd.ExecuteReader();
                if (DR.Read())
                {
                    DR.Close();
                    this.Close();
                    UStatus = 1;
                    fmSelectGame fmSelGame = new fmSelectGame(Player, UStatus);
                    fmSelGame.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Username or password not valid");
                    DR.Close();
                    txtUser.Clear();
                    txtPass.Clear();
                    txtUser.Focus();
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
                txtUser.Clear();
                txtPass.Clear();
                txtUser.Focus();
            }
            finally
            {
                Cnn.Close();
            }
        }