Daten aus MySql-Tabelle lesen

public static void Main() {

  MySqlConnection MySql_Connection;
  string MySql_ConnectionString;

  MySql_ConnentionString = "server=localhost;";
  MySql_ConnectionString += "user=root;";
  MySql_ConnectionString += "database=world;";
  MySql_ConnectionString += "password=******;";

  MySql_Connection = new MySqlConnection(MySql_ConnentionString);
  
  try { 
    MySql_Connection.Open(); // Verbindung öffnen

    string sql = "SELECT Name, HeadOfState FROM Country WHERE Continent='Oceania'";
    MySqlCommand MySql_Command = new MySqlCommand(sql, MySql_Connection);
    MySqlDataReader MySql_DataReader = MySql_Command.ExecuteReader();

    while (MySql_DataReader.Read()){
      Console.WriteLine(MySql_DataReader[0] + " -- " + MySql_DataReader[1]);
    }
  } 

  catch (Exception ex) { 
    Console.WriteLine(ex.ToString()); 
  } 

  MySql_Connection.Close(); // Verbindung schließen
}

 

Verbindung zu MySql – Server herstellen

public static void Main() {

  MySqlConnection MySql_Connection;
  string MySql_ConnectionString;

  MySql_ConnentionString = "server=localhost;";
  MySql_ConnectionString += "user=root;";
  MySql_ConnectionString += "database=world;";
  MySql_ConnectionString += "password=******;";

  MySql_Connection = new MySqlConnection(MySql_ConnentionString);
  
  try { 
    MySql_Connection.Open(); // Verbindung öffnen
  } 

  catch (Exception ex) { 
    Console.WriteLine(ex.ToString()); 
  } 

  MySql_Connection.Close(); // Verbindung schließen
}