我有一个供学生使用的表,单击搜索按钮后,我已成功检索数据并将其显示在文本框中,然后将几个单词更改为某些文本框,然后单击了另一个按钮以更新查询但是,当我检查数据库时,没有进行任何更改,非常感谢您的帮助,我是C#和Visual Studio的新手。
这是我的表单代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace UA_SMS_Grade_Inquiry_System
{
public partial class UpdateStudent : Form
{
public UpdateStudent()
{
InitializeComponent();
}
private void searchStudButton_Click(object sender, EventArgs e)
{
string search = this.searchStudTbox.Text;
MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();
connBuilder.Add("Database", "grade_inquiry");
connBuilder.Add("Data Source", "localhost");
connBuilder.Add("User Id", "root");
connBuilder.Add("Password", "");
MySqlConnection connection = new MySqlConnection(connBuilder.ConnectionString);
connection.Open();
try
{
MySqlCommand cmd = new MySqlCommand("SELECT * FROM students INNER JOIN courses ON students.course = courses.course INNER JOIN departments ON departments.department = courses.department WHERE student_school_id ='" + search + "'", connection);
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
this.updateStudIdTbox.Text = (reader["student_school_id"].ToString());
this.updateStudNameTbox.Text = (reader["studname"].ToString());
this.updateCourseTbox.Text = (reader["course"].ToString());
this.updateSectionTbox.Text = (reader["section"].ToString());
this.updateYearTbox.Text = (reader["year_level"].ToString());
this.updateCollegeTbox.Text = (reader["department"].ToString());
}
}
else
{
MessageBox.Show("NO MATCHING RECORDS");
}
}
catch (Exception)
{
MessageBox.Show("COULD NOT CONNECT TO DATABASE\nPLEASE CONTACT ADMINISTRATOR");
}
connection.Close();
}
private void updateButton_Click(object sender, EventArgs e)
{
string search_id = this.searchStudButton.Text;
string stud_id = this.updateStudIdTbox.Text;
string studname = this.updateStudNameTbox.Text;
string course = this.updateCourseTbox.Text;
int year = System.Convert.ToInt32(this.updateYearTbox.Text);
string section = this.updateSectionTbox.Text;
string department = this.updateCollegeTbox.Text;
MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();
connBuilder.Add("Database", "grade_inquiry");
connBuilder.Add("Data Source", "localhost");
connBuilder.Add("User Id", "root");
connBuilder.Add("Password", "");
MySqlConnection connection = new MySqlConnection(connBuilder.ConnectionString);
connection.Open();
try
{
MySqlCommand update = new MySqlCommand("UPDATE students SET student_school_id='" + stud_id + "' WHERE student_school_id ='" + search_id + "'", connection);
MySqlDataReader reader = update.ExecuteReader();
MessageBox.Show("SUCCESS");
}
catch (Exception)
{
MessageBox.Show("ERROR");
}
connection.Close();
}
}
}
我不知道我单击更新按钮后,但我检查了数据库,但未进行任何更改,这表示成功了。我只是尝试仅使用student_school_id列进行更新,这不是我表中的主键,而是一个唯一列,以防止重复。