Ejemplo n.º 1
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                //Establishes the connection to the database

                //connString is just the connection string
                string connString = "Server=PL09\\MTCDB;Database=Numeric;Trusted_Connection=True;";

                //New connection using connString
                SqlConnection sqlAccounts = new SqlConnection(connString);


                //Creates a SqlCommand class
                using (SqlCommand cmdAccounts = new SqlCommand("spUserAccount", sqlAccounts))
                {
                    //Command type is a stored procedure
                    cmdAccounts.CommandType = CommandType.StoredProcedure;
                    //Supplies the parameters within your stored procedure (tbUser.Text will supply the parameter @user)
                    cmdAccounts.Parameters.AddWithValue("@user", tbUser.Text);
                    cmdAccounts.Parameters.AddWithValue("@pass", tbPass.Text);

                    //Opens the connection to the database
                    sqlAccounts.Open();
                    //This is used because we aren't trying to return a value
                    cmdAccounts.ExecuteNonQuery();

                    //Adapter allows for select/insert statements (I think)
                    SqlDataAdapter daAccounts = new SqlDataAdapter();

                    //Creates virtual table for the entity returned from the stored procedure (I think that's what's going on here)
                    DataTable dtAccounts = new DataTable();

                    //Basically selects the record in the database
                    daAccounts.SelectCommand = cmdAccounts;

                    //Puts that record in your data table
                    daAccounts.Fill(dtAccounts);

                    //Closes the connection.
                    sqlAccounts.Close();


                    //Variables
                    int count;

                    //Counts the rows in data table.
                    count = dtAccounts.Rows.Count;


                    //If the row return = 1, close the login form and open the converter.
                    if (count == 1)
                    {
                        Default frmDefault = new Default();
                        this.Hide();
                        frmDefault.Show();
                    }
                    // Else, display error.
                    else
                    {
                        lblError.Visible = true;
                    }
                }
            }

            // Displays exception
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }