更新按钮查询无法正常工作mysql c#

时间:2014-10-14 06:05:02

标签: c# mysql windows-store-apps

我尝试使用以下按钮更新mysql数据库表,但它没有工作,它将传递catch并在catch中显示错误消息。 但是使用断点我看到值传递给文本框值

代码

private void Button_Edit_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            #region Variables
            string gender = null;
            if (isMale || isFemale)
            {
                gender = isMale ? "Male" : "Female";
            }
            string status = null;
            if (isSingle || isMarried)
            {
                status = isSingle ? "Single" : "Married";
            }
            string ol = null;
            if (isolYes || isolNo)
            {
                ol = isolYes ? "Yes" : "No";
            }
            string al = null;
            if (isalYes || isalNo)
            {
                al = isalYes ? "Yes" : "No";
            }
            string birth = null;
            if (isbirthYes || isbirthNo)
            {
                birth = isbirthYes ? "Yes" : "No";
            }
            string Nic = null;
            if (isNicYes || isNicNo)
            {
                Nic = isNicYes ? "Yes" : "No";
            }
            string police = null;
            if (ispoliceYes || ispoliceNo)
            {
                police = ispoliceYes ? "Yes" : "No";
            }

            string DoB = dobDateInput.Date.ToString("yyyy-MM-dd HH:mm");
            string country = this.countryComboBox.SelectedItem.ToString(); 
            #endregion
            string Query = @"UPDATE `bcasdb`.`tbl_student`
                    SET
                   `reg_id` = '" + this.regIDInput.Text +
                "',`std_fname` = '" + this.fnameInput.Text +
                "',`std_lname` = '" + this.lnameInput.Text +
                "',`tbl_batch_batch_id` = '" + this.batchIDInput.Text +
                "',`gender` = '" + gender +
                "',`dob` = '" + DoB +
                "',`email` = '" + this.emailInput.Text +
                "',`mobile` = '" + this.mobileNoInput.Text +
                "',`contact_address` = '" + this.conAddressInput.Text +
                "',`home_address` = '" + this.homeAddressInput.Text +
                "',`status` = '" + status +
                "',`courceIDInput` = '" + this.courceIDInput.Text +
                "',`depart_id` = '" + this.depIDInput.Text +
                "',`parent_name` = '" + this.parentNameInput.Text +
                "',`nationality` = '" + country +
                "',`telephone` = '" + this.teleNoInput.Text +
                "',`nic` = '" + this.NICNoInput.Text +
                "',`passport_no` = '" + this.passportNoInput.Text +
                "',`acadamic_qulification` = '" + this.acdqlyInput.Text +
                "',`current_employement` = '" + this.currntEmpInput.Text +
                "',`gce_ol` = '" + ol +
                "',`gce_al` = '" + al +
                "',`birth_certifiacte` = '" + birth +
                "',`copy_of_nic` = '" + Nic +
                "',`police_clearance` = '" + police +
                "'  WHERE `reg_id` = '" + this.regIDInput.Text + "';";
            //This is command class which will handle the query and connection object.
            MySqlConnection conn = new MySqlConnection(BCASApp.DataModel.DB_CON.connection);
            MySqlCommand cmd = new MySqlCommand(Query, conn);
            MySqlDataReader MyReader;
            conn.Open();
            MyReader = cmd.ExecuteReader();// this query will be executed and data saved into the database.           
            conn.Close();
            successmsgBox();
        }
        catch (Exception)
        {
            errormsgBox();
        }
    }

1 个答案:

答案 0 :(得分:0)

您要更新数据而不提取任何内容,因此请尝试使用ExecuteNonQuery代替ExecuteReader,并始终尝试使用parametrized query

您的密码:

MySqlConnection conn = new MySqlConnection(BCASApp.DataModel.DB_CON.connection);
MySqlCommand cmd = new MySqlCommand(Query, conn);
int retval;
conn.Open();
retval = cmd.ExecuteNonQuery();// this query will be executed and data saved into the database.           
conn.Close();
successmsgBox();

Note: If retval == 1 then your database updated successfully else unsuccessful.