[英]C# SQL Server CE - Update won't work
I'm trying to finish a college project that requires a program to interact with a database. 我正在尝试完成一个大学项目,该项目需要一个程序才能与数据库进行交互。
Some of my naming is a little odd, but don't worry! 我的一些命名有些奇怪,但是请放心!
I'm trying to use a single submit button to either update or insert to the database. 我正在尝试使用一个提交按钮来更新或插入数据库。
Main issue is that I can't get an update to work though when I changed my code to try and fix it, I made it worse. 主要问题是,当我更改代码以尝试对其进行修复时,我无法获得更新,但情况变得更糟。 Here is what I currently have.
这是我目前拥有的。
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());
}
}
}
}
Has anyone got any ideas why I am getting an error on the c.ExecuteQuery
line? 有谁知道为什么我在
c.ExecuteQuery
行上c.ExecuteQuery
?
If I remove said line, it will not throw an exception, but it will not update the database. 如果删除该行,它将不会引发异常,但不会更新数据库。
Thanks 谢谢
You have a simple syntax error in your update query just before the where statement. 在更新查询中,在where语句之前有一个简单的语法错误。
There is an invalid equal sign 无效的等号
string taskUpdate = "UPDATE TaskCode SET TaskCode = @TaskCode, " +
"TaskDescription = @TaskDescription " +
"WHERE TaskCode = @TaskCode;";
Your query also could be simplified with 您的查询也可以通过以下方式简化
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());
}
}
不知道这是否是唯一的问题,但是在WHERE
关键字之前您有一个等号(=)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.