2016-06-09 6 views
0

Wie Sie Daten in XAMPP phpmyadmin mysql-Datenbank von Windows-Formular-Anwendung C# speichern? Variablen mit Daten sind country1, city1, temperature1. Database Wetter und TabelleSo speichern Sie Daten in XAMPP phpmyadmin mysql-Datenbank von Windows-Formularanwendung C#

prognostiziert
try 
{ 

    string MyConnection2 = "datasource=localhost;port=3307;username=db1;password=db12121"; 
    string Query = "insert into weather.forecast(country, city, temperature) values('" + country1 + "','" + city1 + "','" + temperature "');"; 


    MySqlConnection MyConn2 = new MySqlConnection(MyConnection2); 
    MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2); 
    MySqlDataReader MyReader2; 
    MyConn2.Open(); 
    MyReader2 = MyCommand2.ExecuteReader();  
    MessageBox.Show("Save Data"); 
    while (MyReader2.Read()) 
    { 
    } 
    MyConn2.Close(); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
} 

Antwort

0

Dies könnte für Sie arbeiten:

MySql.Data.MySqlClient.MySqlConnection connection; 
     string server = "localhost"; 
     string database = "weather"; 
     string uid = "db1"; 
     string password = "db12121"; 
     string connectionString; 
     connectionString = "SERVER=" + server + ";" + "DATABASE=" + 
     database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";"; 

    try 
     { 
      connection.Open(); 
      if (connection.State == ConnectionState.Open) 
      { 
       MySqlCommand cmd = new MySqlCommand("insert into forecast (country,city,temperature) values(@Country,@City,@Temperature)", connection); 
       cmd.Parameters.AddWithValue("@Country", country1); 
       cmd.Parameters.AddWithValue("@City", city1); 
       cmd.Parameters.AddWithValue("@Temperature", temperature); 
       cmd.ExecuteNonQuery(); 
      } 
      else 
      { 
       DisplayMessage.Text = "Database connection failed."; 
      } 


     } 
     catch (Exception ex) 
     { 

     } 

     connection.Close(); 
}