C# 删除datagridview和数据库中的特定数据

C# 删除datagridview和数据库中的特定数据,c#,visual-studio,datagridview,C#,Visual Studio,Datagridview,我已经完成了更新按钮。当我想“删除”数据时,我的意思是删除datagridview单元格中的数据,并单击更新按钮时,出现一个错误=>System.data.SqlClient.SqlException:“字符串或二进制将被截断”。 经过一些研究,我被告知这是在插入数据的情况下,因为数据的长度有限(例如:nvarchar=>25,这是因为我们插入了30个字符)。但在这里,我要删除 谢谢你的帮助 代码: private void button2_Click(object sender, EventA

我已经完成了更新按钮。当我想“删除”数据时,我的意思是删除datagridview单元格中的数据,并单击更新按钮时,出现一个错误=>System.data.SqlClient.SqlException:“字符串或二进制将被截断”。 经过一些研究,我被告知这是在插入数据的情况下,因为数据的长度有限(例如:nvarchar=>25,这是因为我们插入了30个字符)。但在这里,我要删除

谢谢你的帮助

代码:

private void button2_Click(object sender, EventArgs e)
{
    SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");
    maConnexion.Open();

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if ((row.Cells[19].Value != null) && (bool)row.Cells[19].Value)
        {
            SqlCommand command = maConnexion.CreateCommand();
            command = new SqlCommand("update FailOnly set Machine=@Machine, ProgCode=@ProgCode, BoardName=@BoardName, BoardNumber=@BoardNumber, Tester=@Tester,DateTest=@DateT,TimeTest=@TT,TimeStart=@TS,FComponent=@FC, MMessage=@Message, TotalTestProg=@TTP, ReadValue=@RV, ValueReference=@VR,PTolerance=@PT , FaultCodeByOp=@Fault, RepairingDate=@RD, RepairingTime = @RT, ReportingOperator=@RO WHERE SerialNum=@Serial", maConnexion);
            command.Parameters.AddWithValue("@Machine", row.Cells[0].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@Serial", row.Cells[1].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@ProgCode", row.Cells[2].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@BoardName", row.Cells[3].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@BoardNumber", row.Cells[4].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@Tester", row.Cells[5].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@DateT", row.Cells[6].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@TT", row.Cells[7].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@TS", row.Cells[8].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@FC", row.Cells[9].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@Message", row.Cells[10].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@TTP", row.Cells[11].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@RV", row.Cells[12].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@VR", row.Cells[13].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@PT", row.Cells[14].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@Fault", row.Cells[15].Value != null ? row.Cells[15].Value : DBNull.Value);
            command.Parameters.AddWithValue("@RD", row.Cells[16].Value != null ? row.Cells[16].Value : DBNull.Value);
            command.Parameters.AddWithValue("@RT", row.Cells[17].Value != null ? row.Cells[17].Value : DBNull.Value);
            command.Parameters.AddWithValue("@RO", row.Cells[18].Value != null ? row.Cells[18].Value : DBNull.Value);
            command.ExecuteNonQuery();
        }
    }
    maConnexion.Close();
    this.Hide();
    Repair rep = new Repair();
    rep.Show();
}

通过查看代码,我觉得很奇怪,对于许多参数,您都在分配row.Cells[1].Value。这将导致以下异常“字符串或二进制将被截断”。这可能是一个未命中的情况,但下面是您的更新代码

   if ((row.Cells[19].Value != null) && (bool)row.Cells[19].Value)
        {

            SqlCommand command = maConnexion.CreateCommand();
            command = new SqlCommand("update FailOnly set Machine=@Machine, ProgCode=@ProgCode, BoardName=@BoardName, BoardNumber=@BoardNumber, Tester=@Tester,DateTest=@DateT,TimeTest=@TT,TimeStart=@TS,FComponent=@FC, MMessage=@Message, TotalTestProg=@TTP, ReadValue=@RV, ValueReference=@VR,PTolerance=@PT , FaultCodeByOp=@Fault, RepairingDate=@RD, RepairingTime = @RT, ReportingOperator=@RO WHERE SerialNum=@Serial", maConnexion);
            command.Parameters.AddWithValue("@Machine", row.Cells[0].Value != null ? row.Cells[0].Value : DBNull.Value);
            command.Parameters.AddWithValue("@Serial", row.Cells[1].Value != null ? row.Cells[1].Value : DBNull.Value);
            command.Parameters.AddWithValue("@ProgCode", row.Cells[2].Value != null ? row.Cells[2].Value : DBNull.Value);
            command.Parameters.AddWithValue("@BoardName", row.Cells[3].Value != null ? row.Cells[3].Value : DBNull.Value);
            command.Parameters.AddWithValue("@BoardNumber", row.Cells[4].Value != null ? row.Cells[4].Value : DBNull.Value);
            command.Parameters.AddWithValue("@Tester", row.Cells[5].Value != null ? row.Cells[5].Value : DBNull.Value);
            command.Parameters.AddWithValue("@DateT", row.Cells[6].Value != null ? row.Cells[6].Value : DBNull.Value);
            command.Parameters.AddWithValue("@TT", row.Cells[7].Value != null ? row.Cells[7].Value : DBNull.Value);
            command.Parameters.AddWithValue("@TS", row.Cells[8].Value != null ? row.Cells[8].Value : DBNull.Value);
            command.Parameters.AddWithValue("@FC", row.Cells[9].Value != null ? row.Cells[9].Value : DBNull.Value);
            command.Parameters.AddWithValue("@Message", row.Cells[10].Value != null ? row.Cells[10].Value : DBNull.Value);
            command.Parameters.AddWithValue("@TTP", row.Cells[11].Value != null ? row.Cells[11].Value : DBNull.Value);
            command.Parameters.AddWithValue("@RV", row.Cells[12].Value != null ? row.Cells[12].Value : DBNull.Value);
            command.Parameters.AddWithValue("@VR", row.Cells[13].Value != null ? row.Cells[13].Value : DBNull.Value);
            command.Parameters.AddWithValue("@PT", row.Cells[14].Value != null ? row.Cells[14].Value : DBNull.Value);
            command.Parameters.AddWithValue("@Fault", row.Cells[15].Value != null ? row.Cells[15].Value : DBNull.Value);
            command.Parameters.AddWithValue("@RD", row.Cells[16].Value != null ? row.Cells[16].Value : DBNull.Value);
            command.Parameters.AddWithValue("@RT", row.Cells[17].Value != null ? row.Cells[17].Value : DBNull.Value);
            command.Parameters.AddWithValue("@RO", row.Cells[18].Value != null ? row.Cells[18].Value : DBNull.Value);
            command.ExecuteNonQuery();


        }

哦,上帝,我感到很惭愧。我也完全忘了换电池了。它现在起作用了,谢谢你。不必感到羞耻,它发生在每一个编码的凡人身上。