如何在buttonclick上向mysql添加文本 [英] How do I add a text to mysql on buttonclick

查看:96
本文介绍了如何在buttonclick上向mysql添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void pictureBox1_Click(object sender, EventArgs e)
        {
            System.IO.Stream str = Properties.Resources.commander_5;
            System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str);
            snd.Play();

            label1.Text = "S1";



            
   string constring = "datasource=123.456.678;port=3306;username=1234;password=1234";
            string query = "update mysqlcshap.Fahrzeug set (Status) TEXT('1');";
            MySqlConnection Dateneingabe = new MySqlConnection(constring);
            MySqlCommand cmd = new MySqlCommand(query, Dateneingabe);
            MySqlDataReader myReader;


            try
            {
                Dateneingabe.Open();
                myReader = cmd.ExecuteReader();
           
                while (myReader.Read()) { }





            }




            catch (Exception ex)
            {  }





我尝试了什么:



如何在Buttonclick上更新MYSQL上的文本?



What I have tried:

How can I update the Text on MYSQL on Buttonclick?

推荐答案

查询看起来应该更像这样

The query should look more like this
"update mysqlcshap.Fahrzeug set Status='1';"

但是,这将更新< b> all 数据库中的记录可能不是你想要的。你可能想要更新一个特定的记录,为此你需要一个WHERE子句

However, this will update all the records in the database which is probably not what you want. You probably want to update a specific record and for that you need a WHERE clause

"update mysqlcshap.Fahrzeug set Status='1' WHERE Id=1234;"

这假设你的表中有一个主键值,它的名字是'Id'。当然,你需要知道Id是什么才能做到这一点。

有时你可以创建保留字的字段。我不记得'Status'是否是其中之一,但只是在[]中包装字段名称。

你正在使用ExecuteReader。此查询不返回任何内容,因此无需阅读。使用ExecuteNonQuery运行无法读取的命令。

您的连接字符串需要修复(参见这个。我没有看到'数据库'参数,也许它是mysqlcshap?

我通常将我的连接包装在using语句中。所有这些在一起,这就是我想出的结果

This assumes that you have a primary key value in your table and it's name is 'Id'. Of course you need to know what the Id is in order to do this.
Sometimes you can create fields that are reserved words. I don't remember if 'Status' is one of them, but just case wrap the field names in [].
You're using ExecuteReader. This query doesn't return anything so there's nothing to read. Use ExecuteNonQuery to run a command that doesn't read.
Your connection string needs fixing (see this. I don't see a 'Database' argument, perhaps it's the mysqlcshap?
I usually wrap my connection in a using statement. Put all that together and this is what I came up with

private void pictureBox1_Click(object sender, EventArgs e)
{
    System.IO.Stream str = Properties.Resources.commander_5;
    System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str);
    snd.Play();
    label1.Text = "S1";
    
    string constring = "datasource=123.456.678;Database=mysqlcshap;port=3306;User Id=1234;Password=1234";
    string query = "update Fahrzeug set [Status]='1' WHERE Id=1234;";
    using (MySqlConnection Dateneingabe = new MySqlConnection(constring))
    {
        MySqlCommand cmd = new MySqlCommand(query, Dateneingabe);
        try
        {
            Dateneingabe.Open();
            cmd.ExecuteNonQuery();
            Dateneingabe.Close();
        }
        catch (Exception ex)
        { 
            // DO SOMETHING WITH THE EX HERE!! 
        }
    }
}

HTH,Mike


这篇关于如何在buttonclick上向mysql添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆