Mysql
 sql >> Database >  >> RDS >> Mysql

C# si connette a mysql tramite il controllo utente

Quindi, in pratica, se hai un database attivo dovresti prima estrarre i dati da esso.

private static string connString = "server=127.0.0.1; userid=yourUserHere; password=youPasswordHere; database=yourDatabaseNameHere";
public static DataTable SelectData(MySqlCommand command)
        {
            try
            {
                DataTable dataTable = new DataTable();

                using (MySqlConnection connection = new MySqlConnection())
                {
                    connection.ConnectionString = connString;
                    connection.Open();

                    command.Connection = connection;
                    MySqlDataReader reader = command.ExecuteReader();
                    dataTable.Load(reader);

                    return dataTable;
                }
            }
            catch (MySqlException e)
            {
                Console.Write(e.Message);
                return null;
            }
        }

Quindi nel contesto è necessario chiamare questo metodo con una riga SQL. È necessario utilizzare sempre query parametrizzate per ridurre al minimo il rischio di iniezioni SQL e simili. Inoltre devi convertire le informazioni che hai da un datatable a un elenco (se è quello che vuoi). In questo modo:

public List<string> dataTableToString(DataTable table)
        {
            List<string> Labels = new List<string>();
            foreach (DataRow row in table.Rows)
            {
                //index of row you want returned in the list
                Labels.Add(row[2].tostring())
            }
         return labels
         }
public List<string> whateverInformationYouWantHere(string labelID,)
        {
            MySqlCommand command = new MySqlCommand();
            command.CommandText = "SELECT * FROM LABELS WHERE LabelID = @labelID";
            command.Parameters.AddWithValue("labelID", labelID);
            return dataTableToString(Databasehandler.SelectData(command));
        }

Quindi tutto ciò che devi fare è creare un ciclo foreach e inserire tutti gli elementi dell'etichetta nel tuo UL. (Se hai domande, non esitare a chiedere).