【发布时间】:2017-07-26 20:22:14
【问题描述】:
我创建了一个应用程序,其中有一个 datagridview。当我单击一个按钮时,写入 datagridview 的数据会以表格的形式发送到我的数据库。但我有一个例外,我迷路了。
错误:System.Data.SqlClient.SqlException: 'Fail to convert nvarchar value 'B003382A3' to int value。'
我什至不知道他为什么尝试转换为 int,因为我的数据在我的数据库中是 nvarchar。我唯一的建议是他正在反转单元格。
我的代码:
private void metroButton1_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[20].Value != null) && (bool)row.Cells[20].Value)
{
SqlCommand command = maConnexion.CreateCommand();
command = new SqlCommand("update FailAndPass set Machine=@machine, ProgCode=@pc, BoardName=@BName, BoardNumber=@BNumber, Tester=@T, DateTest=@DT, TimeTest=@TT, TimeStart=@TS, FComponent=@FC, Message=@Mess, TotalTestProg=@TTP, ReadValue=@RV, ValueReference=@VR, PTolerance=@PT, FaultCodeByOP=@FCBO, FaultDetail=@FD, RepairingDate=@RD, RepairingTime = @RT, ReportingOperator=@RO WHERE SerialNum=@Serial", maConnexion);
command.Parameters.AddWithValue("@Serial", textBox1.Text);
command.Parameters.AddWithValue("@Machine", row.Cells[0].Value != null ? row.Cells[0].Value : DBNull.Value);
command.Parameters.AddWithValue("@pc", row.Cells[2].Value != null ? row.Cells[2].Value : DBNull.Value);
command.Parameters.AddWithValue("@BName", row.Cells[3].Value != null ? row.Cells[3].Value : DBNull.Value);
command.Parameters.AddWithValue("@BNumber", row.Cells[4].Value != null ? row.Cells[4].Value : DBNull.Value);
command.Parameters.AddWithValue("@T", row.Cells[5].Value != null ? row.Cells[5].Value : DBNull.Value);
command.Parameters.AddWithValue("@DT", 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("@Mess", 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("@FCBO", row.Cells[15].Value != null ? row.Cells[15].Value : DBNull.Value);
command.Parameters.AddWithValue("@FD", row.Cells[16].Value != null ? row.Cells[16].Value : DBNull.Value);
command.Parameters.AddWithValue("@RD", row.Cells[17].Value != null ? row.Cells[17].Value : DBNull.Value);
command.Parameters.AddWithValue("@RT", row.Cells[18].Value != null ? row.Cells[18].Value : DBNull.Value);
command.Parameters.AddWithValue("@RO", row.Cells[19].Value != null ? row.Cells[19].Value : DBNull.Value);
command.ExecuteNonQuery();
}
}
maConnexion.Close();
this.Hide();
Repair rep = new Repair();
rep.Show();
}
这是我的数据库的图片:
我使用了调试器,但似乎我的单元格不正确。我的datagridview的第一列是Machine,代码中对应的单元格是row.cells1。
似乎我所有的单元格的顺序都不正确。这到底是什么?我的意思是,通常 BoardName 是第三个单元格,但我把 row.cell[2] 我没有错误了。而且它不是唯一一个出现此错误的单元格。
【问题讨论】:
-
表
FailAndPass中的所有列的类型都是NVARCHAR? -
我假设除
SerialNum之外的所有列都是可为空的。尝试使用DbType指定字符串参数的数据类型,例如command.Parameters.AddWithValue("@Machine", row.Cells[0].Value != null ? row.Cells[0].Value : DBNull.Value).DbType = DbType.String;. -
旁注:
private void AddParameter(SqlCommand command, string ParameterName, object value) { command.Parameters.AddWithValue(ParameterName, value != null ? value : DBNull.Value); } -
blogs.msmvps.com/jcoehoorn/blog/2014/05/12/… - 引自博客文章“AddWithValue() 函数存在问题:它必须为您的查询参数推断数据库类型。事情是这样的:有时它会出错。 "
-
不是一个整数!!!
标签: c# sql sql-server visual-studio datagridview