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