簡體   English   中英

刪除datagridview和數據庫中的特定數據

[英]Delete specifics data in datagridview and in database

我已經完成了“更新”按鈕。 當我想“刪除”數據時,我的意思是刪除datagridview單元中的數據,然后單擊“更新”按鈕,出現錯誤=> System.Data.SqlClient.SqlException:'String or二進制文件將被截斷”。 經過一些研究,有人告訴我是在插入數據的情況下,並且由於數據的限制長度(例如: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();


        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM