简体   繁体   English

更新/添加表中的行

[英]Updating/adding Rows in a table

I have a table that has three columns; 我有一个包含三列的表。 ID, surname and first_name 身份证,姓氏和名字

What im trying to do is make a button that can add a new row, and another to update an existing contact to this table. 我正在尝试做的是创建一个可以添加新行的按钮,另一个按钮来更新此表的现有联系人。 I think im pretty close but there's something that just isnt working. 我认为我非常接近,但有些东西行不通。 I have a remove method which works fine, but the way im trying to do the update method is basically remove the contact then add a new one. 我有一个可以正常工作的删除方法,但是我尝试执行更新方法的方法基本上是删除联系人,然后添加一个新联系人。 What it's doing is removing the contact then not adding anything else. 它正在做的是删除联系人,然后不添加其他任何内容。

Here's my addContact method 这是我的addContact方法

        public int addContact(Contacts newContact)
    {

        MySqlConnection conn = null;
        if (newContact != null)
        {
            try
            {
                conn = new MySqlConnection();
                conn.Open();

                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "insert into contacts (id, surname, last_name) values (@id, @surname, @firstName)";

                cmd.Prepare();

                cmd.Parameters.AddWithValue("@id", newContact.contactID);
                cmd.Parameters.AddWithValue("@surname", newContact.surname);
                cmd.Parameters.AddWithValue("@firstName", newContact.first_name);
                cmd.ExecuteNonQuery();

                return (int)cmd.LastInsertedId;
            }
            catch (MySqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());

            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }

            }

        }
        return -1;
    }

And I think the main problem is that im the method incorrectly. 而且我认为主要问题是该方法不正确。 Here's the button that should update that current contact selected in the listbox. 这是应该更新在列表框中选择的当前联系人的按钮。

        private void button2_Click(object sender, EventArgs e)
    {
        Contacts currentContact = (Contacts)listBox1.SelectedItem;
        contactMan.removeContact(currentContact.contactID);
        Contacts testInsertContact = new Contacts();

        testInsertContact.surname = textBox2.Text;
        testInsertContact.first_name = textBox3.Text;
        testInsertContact.contactID = contactMan.addContact(testInsertContact);
        reloadContacts();
    }

Any help is appreciated thanks 任何帮助表示赞赏,谢谢


Well I fixed it by making a new project and basically rewriting the same code. 好吧,我通过制作一个新项目并基本上重写相同的代码来修复它。 No idea why but the problem was that it wasnt connecting to the database correctly and it works now for whatever reason. 不知道为什么,但是问题是它没有正确连接到数据库,并且无论什么原因它现在都可以工作。

cmd.CommandText = "insert into contacts (id, surname, last_name) values (@id, @surname, @firstName)";

请在

cmd.ExecuteNonQuery();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM