public void Delete() { MySqlConnection conn = DB.Connection(); conn.Open(); var cmd = conn.CreateCommand() as MySqlCommand; cmd.CommandText = @"DELETE FROM message WHERE id = @thisId;"; MySqlParameter idParameter = new MySqlParameter(); idParameter.ParameterName = "@thisId"; idParameter.Value = _id; cmd.Parameters.Add(idParameter); cmd.ExecuteNonQuery(); conn.Close(); if (conn != null) { conn.Dispose(); } }
public static List <User> Search(string userName) { List <User> allUsersFound = new List <User> { }; MySqlConnection conn = DB.Connection(); conn.Open(); var cmd = conn.CreateCommand() as MySqlCommand; cmd.CommandText = @"SELECT * FROM users WHERE name LIKE CONCAT('%',@userName,'%');"; MySqlParameter userNameParameter = new MySqlParameter(); userNameParameter.ParameterName = "@userName"; userNameParameter.Value = userName; cmd.Parameters.Add(userNameParameter); var rdr = cmd.ExecuteReader() as MySqlDataReader; while (rdr.Read()) { int id = rdr.GetInt32(0); string name = rdr.GetString(1); string password = rdr.GetString(2); User newUser = new User(name, password, id); allUsersFound.Add(newUser); } conn.Close(); if (conn != null) { conn.Dispose(); } return(allUsersFound); }