我有以下代码成功执行但不更新数据库中的数据。请考虑 mode="edit" 为真的部分,因为 INSERT 工作正常。
我有不同的模式来指示是更新还是插入。插入适用于所有数据库字段,但更新不起作用:它没有给出任何错误(执行成功)但数据库没有更新。
double id; // please consider only edit part
if (mode != "edit")
id = id_increment();
else
id = Convert.ToInt64(view_gv.CurrentRow.Cells["id"].Value.ToString()); //find currrent row successfully
OleDbCommand com = new OleDbCommand();
con.ConnectionString = con_string;
con.Open();
com.Connection = con;
if (mode != "edit")
{
com.CommandText = "INSERT INTO [TBLPERSONMST] ([ID],[name],[address],[city],[o_phone],[r_phone],[mo_number],[email],[dob],[ann_date],[bg],[sex],[group]) VALUES (@id,@name,@add,@city,@ophone,@rphone,@mophone,@email,@dob,@ann_date,@bg,@sex,@group)";
}
else
{
com.CommandText = "UPDATE [TBLPERSONMST] SET [name]=@name,[address]=@add,[city]=@city,[o_phone]=@ophone,[r_phone]=@rphone,[mo_number]=@mophone,[email]=@email,[dob]=@dob,[ann_date]=@ann_date,[bg]=@bg,[sex]=@sex,[group]=@group WHERE [ID]=@id";
}
com.Parameters.Add(new OleDbParameter("@id", id));
com.Parameters.Add(new OleDbParameter("@name", name_txt.Text));
com.Parameters.Add(new OleDbParameter("@add", add_txt.Text));
com.Parameters.Add(new OleDbParameter("@city", city_txt.Text));
if (ophone_txt.Text != "")
com.Parameters.Add(new OleDbParameter("@ophone", Convert.ToDouble(ophone_txt.Text)));
else
com.Parameters.Add(new OleDbParameter("@ophone", "0"));
if (rphone_txt.Text != "")
com.Parameters.Add(new OleDbParameter("@rphone", Convert.ToDouble(rphone_txt.Text)));
else
com.Parameters.Add(new OleDbParameter("@rphone", "0"));
if (mo_txt.Text != "")
com.Parameters.Add(new OleDbParameter("@mophone", Convert.ToDouble(mo_txt.Text)));
else
com.Parameters.Add(new OleDbParameter("@mophone", "0"));
com.Parameters.Add(new OleDbParameter("@email", email_txt.Text));
com.Parameters.Add(new OleDbParameter("@dob", dob_date.Value.ToShortDateString()));
com.Parameters.Add(new OleDbParameter("@ann_date", anni_date.Value.ToShortDateString()));
com.Parameters.Add(new OleDbParameter("@bg", bg_cmbox.SelectedItem));
com.Parameters.Add(new OleDbParameter("@sex", gen_cmbox.SelectedItem));
com.Parameters.Add(new OleDbParameter("@group", grp_cmbox.SelectedItem));
int j=com.ExecuteNonQuery();