我已经完成了“更新”按钮。当我想“删除”数据时,我的意思是删除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();
}
答案 0 :(得分:0)
通过查看代码,我觉得很奇怪,对于许多参数,你要分配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();
}