从链接到ListView中的选定项的文本框中更改值后,表未在数据库中更新

时间:2015-01-27 01:33:28

标签: c# android mysql listview

这是它的工作原理。我从列表视图中选择一行,然后点击“编辑”按钮,其中所选项目的值也将显示在注册表单中。然后“注册”按钮将更改为“更新”。我在更改我的注册表单上的文本框输入后尝试更新我的customers表,但我的数据库没有任何更改。

我没有收到任何错误,但我可能在这里错过了一些内容。

这是我的代码:

    private void btnRfrsh_Click(object sender, EventArgs e)
    {
        try
        {
            con = "datasource=localhost; port=3306; database=cam_air_db; uid=root;";
            connect = new MySqlConnection(con);
            connect.Open();

            string query = "SELECT Cust_Lname, Cust_Fname, Cust_MI, Birthdate, Age, Sex, Passport_ID, Address, Contact_Num, Nationality from customers where removed = 0";
            MySqlCommand select = new MySqlCommand(query, connect);
            MySqlDataReader refresh = select.ExecuteReader();

            while (refresh.Read())
            {
                ListViewItem item;
                item = new ListViewItem(refresh.GetString(0));
                item.SubItems.Add(refresh.GetString(1));
                item.SubItems.Add(refresh.GetString(2));
                item.SubItems.Add(refresh.GetString(3));
                item.SubItems.Add(refresh.GetString(4));
                item.SubItems.Add(refresh.GetString(5));
                item.SubItems.Add(refresh.GetString(6));
                item.SubItems.Add(refresh.GetString(7));
                item.SubItems.Add(refresh.GetString(8));
                item.SubItems.Add(refresh.GetString(9));
                lviewCust.Items.Add(item);
            }

            if (refresh.Read())
            {
                connect.Close();

            }
            else
            {
                connect.Close();

            }
        }

        catch (Exception error)
        {
            MessageBox.Show(error.Message);
        }
    }

    private void btnEdit_Click(object sender, EventArgs e)
    {
        if (lviewCust.SelectedItems.Count > 0)
        {
            ListViewItem item = lviewCust.SelectedItems[0];
            cust_fname.Text = item.SubItems[0].Text;
            cust_lname.Text = item.SubItems[1].Text;
            cust_mi.Text = item.SubItems[2].Text;
            //DateTime bdate = Convert.ToDateTime(item.SubItems[3].Text);
            String bdate_string = item.SubItems[3].Text;
            DateTime bdate = DateTime.ParseExact(bdate_string, "dd-MM-yyyy", null);
            cust_bdate.Value = bdate;
            cust_age.Text = item.SubItems[4].Text;
            cust_sex.Text = item.SubItems[5].Text;
            cust_passid.Text = item.SubItems[6].Text;
            cust_nation.Text = item.SubItems[9].Text;
            cust_add.Text = item.SubItems[7].Text;
            cust_contact.Text = item.SubItems[8].Text;
        }
        cust_fname.ReadOnly = true;
        cust_lname.ReadOnly = true;
        cust_mi.ReadOnly = true;
        cust_passid.ReadOnly = true;
        btnReg.Text = "Update";
        btnReg.Name = "btnUpdate";
        btnReg.Click -= this.btnReg_Click;
        btnReg.Click += this.btnUpdate_Click;
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            con = "datasource=localhost; port=3306; database=cam_air_db; uid=root;";
            connect = new MySqlConnection(con);
            connect.Open();

            string query = "UPDATE customers SET Age = '" + this.cust_age.Text + "', Nationality = '" + this.cust_nation.Text + "', Address = '" + this.cust_add.Text + "', Contact_Num = '" + this.cust_contact.Text + "' WHERE  Cust_Fname = '" + this.cust_fname.Text + "' and Cust_Lname = '" + this.cust_lname.Text + "'";
            MySqlCommand update = new MySqlCommand(query, connect);
            MySqlDataReader updte = update.ExecuteReader();
            MessageBox.Show("Customer Info Updated Successfully");


            if (updte.Read())
            {
                connect.Close();

            }
            else
            {
                connect.Close();

            }
        }

        catch (Exception error)
        {
            MessageBox.Show(error.Message);
        }
        cust_fname.Clear();
        cust_lname.Clear();
        cust_mi.Clear();
        cust_bdate.Value = DateTime.Now;
        cust_age.Clear();
        cust_passid.Clear();
        cust_add.Clear();
        cust_contact.Clear();
        cust_nation.Clear();
        cust_fname.ReadOnly = false;
        cust_lname.ReadOnly = false;
        cust_mi.ReadOnly = false;
        cust_passid.ReadOnly = false;

        btnReg.Text = "Register";
        btnReg.Name = "btnReg";
        btnReg.Click -= this.btnUpdate_Click;
        btnReg.Click += this.btnReg_Click;
    }




}

}

1 个答案:

答案 0 :(得分:0)

  • 首先,我认为你应该使用ExecuteNonQuery()而不是ExecuteReader() 在执行返回某些内容的sql命令(例如SELECT)时调用ExecuteReader() 当您调用一个不返回任何内容的命令(例如INSERT,UPDATE,DELETE等)时,您应该调用ExecuteNonQuery()。
    查看详细信息here
  • 其次,我认为你应该在警报“成功”之前检查结果。 ExecuteNonQuery()返回受影响的行数,您可以检查此行以确定是否成功。