gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:36:29 24 4
gpt4 key购买 nike

我尝试使用以下按钮更新 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();
}
}

最佳答案

您将更新数据而不获取任何内容,因此请尝试使用 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();

注意:如果 retval == 1 则您的数据库更新成功,否则不成功。

关于c# - 更新按钮查询无法正常工作 mysql c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26353696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com