由于某种原因,UPDATE正在删除SQL Server数据库中的记录,而不是更新它。我试过只传递一个变量(并将其更改为不同的单个字段),并删除了100%。我也确定有数据被传递,所以我没有处理DBNull问题。我的SO和谷歌搜索不断提出删除,然后添加行更新记录的过程。这里可能会发生什么?SQL UPDATE删除记录
protected void grdvMyEntries_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string Task = (grdvMyEntries.Rows[e.RowIndex].FindControl("ddlTasks") as DropDownList).SelectedItem.Value;
string Code = (grdvMyEntries.Rows[e.RowIndex].FindControl("ddlPayrollCodes") as DropDownList).SelectedItem.Value;
DateTime strDate = Convert.ToDateTime((grdvMyEntries.Rows[e.RowIndex].FindControl("txtDateEdit") as TextBox).Text.Trim());
string TimeIn = (grdvMyEntries.Rows[e.RowIndex].FindControl("txtTimeInEdit") as TextBox).Text.Trim();
string TimeOut = (grdvMyEntries.Rows[e.RowIndex].FindControl("txtTimeOutEdit") as TextBox).Text.Trim();
string ItemNo = (grdvMyEntries.Rows[e.RowIndex].FindControl("txtItemNo") as TextBox).Text.Trim();
string WO = (grdvMyEntries.Rows[e.RowIndex].FindControl("txtWO") as TextBox).Text.Trim();
string WIP = (grdvMyEntries.Rows[e.RowIndex].FindControl("txtWIP") as TextBox).Text.Trim();
string Note = (grdvMyEntries.Rows[e.RowIndex].FindControl("txtNote") as TextBox).Text.Trim();
string TimeID = grdvMyEntries.DataKeys[e.RowIndex].Value.ToString();
string strConnString = ConfigurationManager.ConnectionStrings["MAFapp"].ConnectionString;
string strTmp = "Date:" + strDate + "|TimeIn:" + TimeIn + "|TimeOut:" + TimeOut + "|WIP:" + WIP + "|WO:" + WO + "|ItemNo:" + ItemNo + "|Task:" + Task + "|Code:" + Code + "|Note:" + Note + "|WHERE TimeID:" + TimeID;
Debug.Print(strTmp);
using (SqlConnection con = new SqlConnection(strConnString))
{
string query = "UPDATE Time SET [Date][email protected], [TimeIn][email protected], [TimeOut][email protected], [WIP][email protected], [WO][email protected], [ItemNo][email protected], [Task][email protected], [Code][email protected], [Note][email protected] WHERE [TimeID][email protected]";
Debug.WriteLine(query);
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@TimeID", TimeID);
cmd.Parameters.AddWithValue("@Date", strDate);
cmd.Parameters.AddWithValue("@TimeIn", TimeIn);
cmd.Parameters.AddWithValue("@TimeOut", TimeOut);
if (String.IsNullOrWhiteSpace(WIP.Trim()))
{
cmd.Parameters.AddWithValue("@WIP", WIP).Value = Convert.DBNull; ;
}
else
{
cmd.Parameters.AddWithValue("@WIP", WIP);
}
if (String.IsNullOrWhiteSpace(WO.Trim()))
{
cmd.Parameters.AddWithValue("@WO", WO).Value = Convert.DBNull; ;
}
else
{
cmd.Parameters.AddWithValue("@WO", WO);
}
if (String.IsNullOrWhiteSpace(ItemNo.Trim()))
{
cmd.Parameters.AddWithValue("@ItemNo", ItemNo).Value = Convert.DBNull; ;
}
else
{
cmd.Parameters.AddWithValue("@ItemNo", ItemNo);
}
if (String.IsNullOrWhiteSpace(Task.Trim()))
{
cmd.Parameters.AddWithValue("@Task", Task).Value = Convert.DBNull; ;
}
else
{
cmd.Parameters.AddWithValue("@Task", Task);
}
cmd.Parameters.AddWithValue("@Code", Code);
if (String.IsNullOrWhiteSpace(Note.Trim()))
{
cmd.Parameters.AddWithValue("@Note", Note).Value = Convert.DBNull; ;
}
else
{
cmd.Parameters.AddWithValue("@Note", Note);
}
con.Open();
cmd.ExecuteNonQuery();
con.Close();
tblSummary.DataBind();
grdvMyEntries.DataBind();
}
}
}
你在桌子上触发吗?如果不是,则不可能使用更新删除记录。也许你有一些字段为NULL,并且你的检查查询不能识别它们 –
你检查过数据库表是否缺少行(因为joe说它不能被该更新本身删除)?可能是因为在其他地方的“错误”选择,您的更新会使行不能成行吗? – Thorarins
除非您在@Joe Taras提及的表上触发表,否则不可能使用Update语句进行删除。 –