Skip to main content
fixed minor C/P problems not mentioned in reviews yet.
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141

User.Cscs

 public bool AuthenticateUser()
{
    string query = "SELECT * FROM users WHERE user_name = @userName LIMIT 1";
    bool password = false;
    try
    {
        Console.Write("connection");
        using (MySqlConnection connection = Connection.GetConnection())
        {
            Console.Write("Cmd");
            using (var cmd = new MySqlCommand(query, connection))
            {
                cmd.Parameters.AddWithValue("@userName", this.UserName);
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        password = BCrypt.Net.BCrypt.Verify(this._password + reader["salt"].ToString(), reader["password"].ToString());
                        this._user_id = int.Parse(reader["id"].ToString());
                    }
                }
            }
        }
    }
    catch (MySqlException ex)
    {
        throw new ArgumentException(ex.Message);
    }
    catch (Exception ex) {
        throw new ArgumentException(ex.Message);
    }

    return password;

}

I didn't created a method to dispose/close the connection anymore, since as far as I know, the using statement automatically disposes the type of IdisposableIDisposable. I was looking for a suggestion before I continue coding for more functionalities.

User.Cs

 public bool AuthenticateUser()
{
string query = "SELECT * FROM users WHERE user_name = @userName LIMIT 1";
bool password = false;
    try
    {
        Console.Write("connection");
        using (MySqlConnection connection = Connection.GetConnection())
        {
            Console.Write("Cmd");
            using (var cmd = new MySqlCommand(query, connection))
            {
                cmd.Parameters.AddWithValue("@userName", this.UserName);
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        password = BCrypt.Net.BCrypt.Verify(this._password + reader["salt"].ToString(), reader["password"].ToString());
                        this._user_id = int.Parse(reader["id"].ToString());
                    }
                }
            }
        }
    }
    catch (MySqlException ex)
    {
        throw new ArgumentException(ex.Message);
    }
    catch (Exception ex) {
        throw new ArgumentException(ex.Message);
    }

return password;

}

I didn't created a method to dispose/close the connection anymore, since as far as I know, the using statement automatically disposes the type of Idisposable. I was looking for a suggestion before I continue coding for more functionalities.

User.cs

public bool AuthenticateUser()
{
    string query = "SELECT * FROM users WHERE user_name = @userName LIMIT 1";
    bool password = false;
    try
    {
        Console.Write("connection");
        using (MySqlConnection connection = Connection.GetConnection())
        {
            Console.Write("Cmd");
            using (var cmd = new MySqlCommand(query, connection))
            {
                cmd.Parameters.AddWithValue("@userName", this.UserName);
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        password = BCrypt.Net.BCrypt.Verify(this._password + reader["salt"].ToString(), reader["password"].ToString());
                        this._user_id = int.Parse(reader["id"].ToString());
                    }
                }
            }
        }
    }
    catch (MySqlException ex)
    {
        throw new ArgumentException(ex.Message);
    }
    catch (Exception ex) {
        throw new ArgumentException(ex.Message);
    }

    return password;

}

I didn't created a method to dispose/close the connection anymore, since as far as I know, the using statement automatically disposes the type of IDisposable. I was looking for a suggestion before I continue coding for more functionalities.

added 24 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

C# Connection to mysqlMySQL database

I'd created a C# class to connect to a mysqlMySQL database  (Connection.cs) and a separate class for the user validation  (User.cs),.

    public static MySqlConnection GetConnection()
    {
        try
        {
            var connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            _connection = new MySqlConnection(connectionString);
            _connection.Open();
        }
        catch (MySqlException ex) {
            
            switch (ex.Number)
            {
                //0: Cannot connect to server.
                case 0:
                    MessageBox.Show("Cannot connect to server.  Contact administrator");
                    break;
                //1045: Invalid user name and/or password.
                case 1045:
                    MessageBox.Show("Invalid username/password, please try again");
                    break;
            }
          
        }
      
        return _connection;
    }
     public bool AuthenticateUser()
    {
    string query = "SELECT * FROM users WHERE user_name = @userName LIMIT 1";
    bool password = false;
        try
        {
            Console.Write("connection");
            using (MySqlConnection connection = Connection.GetConnection())
            {
                Console.Write("Cmd");
                using (var cmd = new MySqlCommand(query, connection))
                {
                    cmd.Parameters.AddWithValue("@userName", this.UserName);
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            password = BCrypt.Net.BCrypt.Verify(this._password + reader["salt"].ToString(), reader["password"].ToString());
                            this._user_id = int.Parse(reader["id"].ToString());
                        }
                    }
                }
            }
        }
        catch (MySqlException ex)
        {
            throw new ArgumentException(ex.Message);
        }
        catch (Exception ex) {
            throw new ArgumentException(ex.Message);
        }

    return password;

    }

I didn't created a method to dispose/close the connection anymore, since AFAIK usingas far as I know, the using statement automatically disposedisposes the type of IdisposableIdisposable. I was looking for a suggestion before I continue coding for more functionalities.

C# Connection to mysql database

I'd created a C# class to connect to a mysql database(Connection.cs) and a separate class for the user validation(User.cs),

    public static MySqlConnection GetConnection()
    {
        try
        {
            var connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            _connection = new MySqlConnection(connectionString);
            _connection.Open();
        }
        catch (MySqlException ex) {
            
            switch (ex.Number)
            {
                //0: Cannot connect to server.
                case 0:
                    MessageBox.Show("Cannot connect to server.  Contact administrator");
                    break;
                //1045: Invalid user name and/or password.
                case 1045:
                    MessageBox.Show("Invalid username/password, please try again");
                    break;
            }
          
        }
      
        return _connection;
    }
     public bool AuthenticateUser()
    {
    string query = "SELECT * FROM users WHERE user_name = @userName LIMIT 1";
    bool password = false;
        try
        {
            Console.Write("connection");
            using (MySqlConnection connection = Connection.GetConnection())
            {
                Console.Write("Cmd");
                using (var cmd = new MySqlCommand(query, connection))
                {
                    cmd.Parameters.AddWithValue("@userName", this.UserName);
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            password = BCrypt.Net.BCrypt.Verify(this._password + reader["salt"].ToString(), reader["password"].ToString());
                            this._user_id = int.Parse(reader["id"].ToString());
                        }
                    }
                }
            }
        }
        catch (MySqlException ex)
        {
            throw new ArgumentException(ex.Message);
        }
        catch (Exception ex) {
            throw new ArgumentException(ex.Message);
        }

    return password;

    }

I didn't created a method to dispose/close the connection anymore, since AFAIK using statement automatically dispose the type of Idisposable. I was looking for suggestion before I continue coding for more functionalities.

Connection to MySQL database

I'd created a C# class to connect to a MySQL database  (Connection.cs) and a separate class for the user validation  (User.cs).

public static MySqlConnection GetConnection()
{
    try
    {
        var connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        _connection = new MySqlConnection(connectionString);
        _connection.Open();
    }
    catch (MySqlException ex) {
        
        switch (ex.Number)
        {
            //0: Cannot connect to server.
            case 0:
                MessageBox.Show("Cannot connect to server.  Contact administrator");
                break;
            //1045: Invalid user name and/or password.
            case 1045:
                MessageBox.Show("Invalid username/password, please try again");
                break;
        }
      
    }
  
    return _connection;
}
 public bool AuthenticateUser()
{
string query = "SELECT * FROM users WHERE user_name = @userName LIMIT 1";
bool password = false;
    try
    {
        Console.Write("connection");
        using (MySqlConnection connection = Connection.GetConnection())
        {
            Console.Write("Cmd");
            using (var cmd = new MySqlCommand(query, connection))
            {
                cmd.Parameters.AddWithValue("@userName", this.UserName);
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        password = BCrypt.Net.BCrypt.Verify(this._password + reader["salt"].ToString(), reader["password"].ToString());
                        this._user_id = int.Parse(reader["id"].ToString());
                    }
                }
            }
        }
    }
    catch (MySqlException ex)
    {
        throw new ArgumentException(ex.Message);
    }
    catch (Exception ex) {
        throw new ArgumentException(ex.Message);
    }

return password;

}

I didn't created a method to dispose/close the connection anymore, since as far as I know, the using statement automatically disposes the type of Idisposable. I was looking for a suggestion before I continue coding for more functionalities.

deleted 14 characters in body; edited tags
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

I didn't created a method to dispose/close the connection anymore, since AFAIK using statement automatically dispose the type of Idisposable. I was looking for suggestion before I continue coding for more functionalities. Thank YOu

I didn't created a method to dispose/close the connection anymore, since AFAIK using statement automatically dispose the type of Idisposable. I was looking for suggestion before I continue coding for more functionalities. Thank YOu

I didn't created a method to dispose/close the connection anymore, since AFAIK using statement automatically dispose the type of Idisposable. I was looking for suggestion before I continue coding for more functionalities.

Source Link
Loading