重复输入' mect200'关键'小学'

时间:2014-06-17 15:22:53

标签: mysql sql primary-key

我正在创建一个学生数据库,其中包含每个课程的course idcourse namecredits,通过失败声明和grades。 我的目标是更新信息。我写了以下代码:

private void Update_bttn_Click(object sender, EventArgs e)
{
    try
    {
        string ConString = " datasource = localhost; port = 3306; username = root; password = 3306";
        string query = "update studentdata.semestre1 set CourseId ='"+ this.crsId.Text +"',CourseName='" + this.crsName.Text + "',Credits ='" + this.credits.Text + "',CourseStatement= '" + this.comboBox2.Text + "',Grade = '" + this.Grades.Text + "';";
        MySqlConnection ConDatabase = new MySqlConnection(ConString);
        MySqlCommand cmdDataBase = new MySqlCommand(query, ConDatabase);


        MySqlDataReader myReader;


        ConDatabase.Open();
        myReader = cmdDataBase.ExecuteReader();
        MessageBox.Show("Information Updated");
        while ((myReader.Read())) { }
        ConDatabase.Close();
    }
    catch (Exception ex) { MessageBox.Show(ex.Message); }
}

当我尝试输入信息时,我收到此错误

Duplicate entry 'the course id i entered' for key 'Primary'

1 个答案:

答案 0 :(得分:2)

来自错误的

课程ID似乎是您的主要关键

而且你的更新语句中没有where子句

你的查询应该是这样的

string query = "update studentdata.semestre1 
                set CourseName='" + this.crsName.Text + "',
                Credits ='" + this.credits.Text + "',
                CourseStatement= '" + this.comboBox2.Text + "',
                Grade = '" + this.Grades.Text + "'
                where CourseId ='"+ this.crsId.Text +"',;";