【问题标题】:How to save data to xampp phpmyadmin mysql database from windows form application c#如何从 Windows 窗体应用程序 c# 将数据保存到 xampp phpmyadmin mysql 数据库
【发布时间】:2016-06-09 20:36:16
【问题描述】:

如何将数据从 windows 窗体应用程序 c# 保存到 xampp phpmyadmin mysql 数据库?带有数据的变量是 country1,city1,temperature1。 数据库是天气,表是预报

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);
}

【问题讨论】:

    标签: c# mysql phpmyadmin connection


    【解决方案1】:

    这可能对你有用:

        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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多