0

我正在尝试完成一个需要程序与数据库交互的大学项目。

我的一些命名有点奇怪,但不要担心!

我正在尝试使用单个提交按钮来更新或插入数据库。

主要问题是我无法让更新工作,但当我更改代码以尝试修复它时,我让它变得更糟。这是我目前拥有的。

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网上遇到错误?

如果我删除该行,它不会抛出异常,但不会更新数据库。

谢谢

4

2 回答 2

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());
    }
}
于 2013-06-03T16:01:17.080 回答
2

不确定这是否是唯一的问题,但您在WHERE关键字之前有一个等号 (=)。

于 2013-06-03T16:01:18.210 回答