2013-06-03 6 views
0

데이터베이스와 상호 작용하는 프로그램이 필요한 대학 프로젝트를 완료하려고합니다.C# SQL Server CE - 업데이트가 작동하지 않습니다.

일부 내 이름은 다소 이상하지만 걱정하지 마십시오!

단일 제출 버튼을 사용하여 데이터베이스를 업데이트하거나 삽입하려고합니다.

주요 문제는 코드를 변경하여 수정 한 후에도 작동하도록 업데이트 할 수 없다는 것이므로 악화되었습니다. 여기 제가 현재 가지고있는 것이 있습니다.

private void btn_submit_Click(object sender, EventArgs e) 
{ 
    using (SqlCeConnection con = new SqlCeConnection(@"Data Source=G:\Dropbox\HND\Visual Studio\Visual C#\TestForms\TestForms\Database1.sdf")) 
    { 
     con.Open(); 
     string taskSel = "SELECT TaskCode FROM TaskCode;"; 
     SqlCeCommand c1 = new SqlCeCommand(taskSel, con); 
     SqlCeDataReader reader; 
     reader = c1.ExecuteReader(); 

     if (reader.Read()) 
     { 
     try 
     { 
      string taskUpdate = "UPDATE TaskCode SET TaskCode = @TaskCode, TaskDescription = @TaskDescription = WHERE TaskCode = @TaskCode;"; 
      SqlCeCommand c = new SqlCeCommand(taskUpdate, con); 
      c.Parameters.AddWithValue("@TaskCode", cbx_taskCode.Text); 
      c.Parameters.AddWithValue("@TaskDescription", txt_desc.Text); 
      c.ExecuteNonQuery(); 
      con.Close(); 
      MessageBox.Show("Record has been updated"); 
      MainMenu.Current.Show(); 
      this.Close(); 
     } 
     catch (SqlCeException exp) 
     { 
      MessageBox.Show(exp.ToString()); 
     } 
     } 
     else 
     { 
     try 
     { 
      string taskInsert = "INSERT INTO TaskCode VALUES (@TaskCode, @TaskDescription);"; 
      SqlCeCommand c = new SqlCeCommand(taskInsert, con); 
      c.Parameters.AddWithValue("@TaskCode", cbx_taskCode.Text); 
      c.Parameters.AddWithValue("@TaskDescription", txt_desc.Text); 
      c.ExecuteNonQuery(); 
      con.Close(); 
      MessageBox.Show("Record has been added"); 
      MainMenu.Current.Show(); 
      this.Close(); 
     } 
     catch (SqlCeException exp) 
     { 
      MessageBox.Show(exp.ToString()); 
     } 
     } 
    } 
} 

c.ExecuteQuery 라인에서 오류가 발생하는 이유는 무엇입니까?

해당 줄을 제거하면 예외가 발생하지 않지만 데이터베이스는 업데이트되지 않습니다.

감사합니다.

+0

'c.ExecuteQuery line'이 보이지 않습니다.'c.ExecuteNonQuery()'를 의미합니까? 'UPDATE' 문에서'WHERE' 전에'='를 제거하십시오. – GamerJosh

+0

*** *** 오류는 무엇입니까? 완전하고 정확한 오류 메시지를 게시하십시오 - 감사합니다! –

답변

3

where 문 바로 앞에있는 업데이트 쿼리에 간단한 구문 오류가 있습니다. 잘못된 등호가있다

string taskUpdate = "UPDATE TaskCode SET TaskCode = @TaskCode, " + 
        "TaskDescription = @TaskDescription " + 
        "WHERE TaskCode = @TaskCode;"; 

는 유일한 문제는,하지만 당신은 WHERE 전에 등호 (=)가있는 경우도

using (SqlCeConnection con = new SqlCeConnection(@"Data Source=G:\Dropbox\HND\Visual Studio\Visual C#\TestForms\TestForms\Database1.sdf")) 
{ 
    con.Open(); 
    string taskSel = "SELECT COUNT(*) FROM TaskCode"; 
    string cmdText; 
    SqlCeCommand c1 = new SqlCeCommand(taskSel, con); 
    int count = (int)c1.ExecuteScalar(); 
    if (count > 0) 
    { 
     // Here there is no point to update the TaskCode. You already know the value 
     // Unless you have a different value, but then you need another parameter 
     // the 'old' TaskCode..... 
     cmdText = "UPDATE TaskCode SET " + 
        "TaskDescription = @TaskDescription " + 
        "WHERE TaskCode = @TaskCode;"; 
    } 
    else 
    { 
     cmdText = "INSERT INTO TaskCode VALUES (@TaskCode, @TaskDescription);"; 
    } 
    try 
    { 
     SqlCeCommand c = new SqlCeCommand(cmdText, con); 
     c.Parameters.AddWithValue("@TaskCode", cbx_taskCode.Text); 
     c.Parameters.AddWithValue("@TaskDescription", txt_desc.Text); 
     c.ExecuteNonQuery(); 
     MessageBox.Show(count > 0 ? "Record has been updated" : "Record has been added"); 
     MainMenu.Current.Show(); 
     this.Close(); 
    } 
    catch (SqlCeException exp) 
    { 
     MessageBox.Show(exp.ToString()); 
    } 
} 
2

확실하지 단순화 할 수 귀하의 질의 예어.