2013-06-03 91 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;"; 

你的查询还可以与

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

不知道被简化,如果它是唯一的问题,但你有WHERE之前等号(=)符号关键词。