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

亚历克西斯·林

我已经完成了更新按钮。当我想“删除”数据时,我的意思是删除 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();


        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从datagridview和数据库中删除选定的行?

来自分类Dev

从DataGridView以及数据库中删除任何行

来自分类Dev

Datagridview和数据库

来自分类Dev

如何从选定行datagridview中删除记录并在删除后更新数据库?

来自分类Dev

在datagridview中删除多行

来自分类Dev

从DataGridView和MS-Access数据库中删除选定的行

来自分类Dev

通过nativescript Vue删除Couchbase数据库中的特定数据集?

来自分类Dev

从DataGridView中删除选定的行

来自分类Dev

在datagridview中删除行的列表

来自分类Dev

从DataGridView中删除选定的行

来自分类Dev

从列表视图和数据库中删除项目

来自分类Dev

SQLException从JTable和数据库中删除行

来自分类Dev

使用 OnItemClickListener 从 ListView 和数据库中删除项目

来自分类Dev

从数据库中删除特定字段

来自分类Dev

C# DataGridView 删除数据行

来自分类Dev

刷新以从Datagridview C#中删除所有数据

来自分类Dev

刷新以从Datagridview C#中删除所有数据

来自分类Dev

在DatagridView中链接数据库

来自分类Dev

从数据数组中删除特定数据

来自分类Dev

从WPF中的DataGridView中删除行

来自分类Dev

用户删除该行后,如何从C#中的DataGridview中的特定行保存数据

来自分类Dev

datagridview从字符串中删除“&”字符

来自分类Dev

如何删除SQL datagridview中的整个行?

来自分类Dev

从datagridview和列表中删除行

来自分类Dev

如何从datagridview中删除选定的行

来自分类Dev

如何删除我的 DataGridView 列中的时间

来自分类Dev

从 DataGridView 中删除重复项但保留计数?

来自分类Dev

从数据网格视图和数据库中删除选定的行

来自分类Dev

使用datagridView时,INSERT命令会发生异常,而删除和更新在使用Access数据库的C#中可以正常工作

Related 相关文章

  1. 1

    如何从datagridview和数据库中删除选定的行?

  2. 2

    从DataGridView以及数据库中删除任何行

  3. 3

    Datagridview和数据库

  4. 4

    如何从选定行datagridview中删除记录并在删除后更新数据库?

  5. 5

    在datagridview中删除多行

  6. 6

    从DataGridView和MS-Access数据库中删除选定的行

  7. 7

    通过nativescript Vue删除Couchbase数据库中的特定数据集?

  8. 8

    从DataGridView中删除选定的行

  9. 9

    在datagridview中删除行的列表

  10. 10

    从DataGridView中删除选定的行

  11. 11

    从列表视图和数据库中删除项目

  12. 12

    SQLException从JTable和数据库中删除行

  13. 13

    使用 OnItemClickListener 从 ListView 和数据库中删除项目

  14. 14

    从数据库中删除特定字段

  15. 15

    C# DataGridView 删除数据行

  16. 16

    刷新以从Datagridview C#中删除所有数据

  17. 17

    刷新以从Datagridview C#中删除所有数据

  18. 18

    在DatagridView中链接数据库

  19. 19

    从数据数组中删除特定数据

  20. 20

    从WPF中的DataGridView中删除行

  21. 21

    用户删除该行后,如何从C#中的DataGridview中的特定行保存数据

  22. 22

    datagridview从字符串中删除“&”字符

  23. 23

    如何删除SQL datagridview中的整个行?

  24. 24

    从datagridview和列表中删除行

  25. 25

    如何从datagridview中删除选定的行

  26. 26

    如何删除我的 DataGridView 列中的时间

  27. 27

    从 DataGridView 中删除重复项但保留计数?

  28. 28

    从数据网格视图和数据库中删除选定的行

  29. 29

    使用datagridView时,INSERT命令会发生异常,而删除和更新在使用Access数据库的C#中可以正常工作

热门标签

归档