简体   繁体   English

SQL UPDATE删除记录

[英]SQL UPDATE deletes record

For some reason UPDATE is deleting the record in my SQL Server database instead of updating it. 由于某种原因,UPDATE将删除我的SQL Server数据库中的记录,而不是对其进行更新。 I have tried only passing one variable (and changed it different individual fields) and it deletes 100%. 我尝试仅传递一个变量(并将其更改为不同的单个字段),并且删除了100%。 I also have made sure there was data being passed so I'm not dealing with a DBNull issue. 我还确保有数据正在传递,因此我不处理DBNull问题。 My SO and google searches keep coming up with the delete then add row process for updating records. 我的SO和Google搜索会不断提出删除操作,然后添加行处理来更新记录。 What might be going on here? 这可能是怎么回事?

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]=@Date, [TimeIn]=@TimeIn, [TimeOut]=@TimeOut, [WIP]=@WIP, [WO]=@WO, [ItemNo]=@ItemNo, [Task]=@Task, [Code]=@Code, [Note]=@Note WHERE [TimeID]=@TimeID";
        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();
        }
    }
}

Update won't delete rows from a table. 更新不会删除表中的行。 The only possibility would be trigger on your tables. 唯一的可能性是在您的表上触发。 Please run this query to find list of triggers and its text. 请运行此查询以查找触发器及其文本的列表。

USE ChangeThisToDatabaseName
GO

SELECT so.NAME
    ,TEXT
FROM sysobjects so
    ,syscomments sc
WHERE type = 'TR'
    AND so.id = sc.id
    AND TEXT LIKE '%Time%'

Note:If there is no triggers on your table then please ensure Is there any records exist based on your filter before running the update. 注意:如果表上没有触发器,则在运行更新之前,请确保是否存在基于您的过滤器的记录。

A while ago, when I try to instert data to a table, data is being recorded but after two seconds from insterting the record was deleted automatically. 不久前,当我尝试将数据插入到表中时,正在记录数据,但是从插入记录开始两秒钟后,该数据被自动删除。 There was no trigger or something like that. 没有触发器或类似的东西。 Then I have observed the table and I found an indexing problem. 然后,我观察了表并发现了索引问题。 I have rebuilt that index and problem has been solved. 我已经重建了索引,问题已经解决。 If you do not have huge data in your table I suggest you to try to rebuild your indexes or recreate them. 如果表中没有大量数据,建议您尝试重建索引或重新创建索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM