所以,我试图在名为price的表中插入数据,但抛出了不匹配数据的异常。这是我插入的代码
foreach (DataGridViewRow row in dgvMultipleItems.SelectedRows)
{
#region Insert Data Into The Database
cmd.Parameters.AddWithValue("@ID", OleDbType.Numeric);
cmd.Parameters["@ID"].Value = row.Cells[0].Value.ToString();
cmd.Parameters.AddWithValue("@Company", OleDbType.VarChar);
cmd.Parameters["@Company"].Value = row.Cells[1].Value.ToString();
cmd.Parameters.AddWithValue("@PriceStructure", OleDbType.VarChar);
cmd.Parameters["@PriceStructure"].Value = row.Cells[2].Value.ToString();
cmd.Parameters.AddWithValue("@ItemNumber", OleDbType.VarChar);
cmd.Parameters["@ItemNumber"].Value = row.Cells[3].Value.ToString();
cmd.Parameters.AddWithValue("@Description", OleDbType.VarWChar);
cmd.Parameters["@Description"].Value = row.Cells[4].Value.ToString();
cmd.Parameters.AddWithValue("@Unit", OleDbType.VarChar);
cmd.Parameters["@Unit"].Value = row.Cells[5].Value.ToString();
cmd.Parameters.AddWithValue("@Price", OleDbType.Numeric);
cmd.Parameters["@Price"].Value = row.Cells[6].Value.ToString();
cmd.Parameters.AddWithValue("@Discount", OleDbType.Double);
cmd.Parameters["@Discount"].Value = row.Cells[7].Value.ToString();
cmd.Parameters.AddWithValue("@FluidType", OleDbType.VarChar);
cmd.Parameters["@FluidType"].Value = row.Cells[8].Value.ToString();
cmd.Parameters.AddWithValue("@SubContractor", OleDbType.VarChar);
cmd.Parameters["@SubContractor"].Value = row.Cells[9].Value.ToString();
cmd.Parameters.AddWithValue("@Discount_SubContract", OleDbType.Double);
cmd.Parameters["@Discount_SubContract"].Value = row.Cells[10].Value.ToString();
cmd.Parameters.AddWithValue("@Price_SubContract", OleDbType.Numeric);
cmd.Parameters["@Price_SubContract"].Value = row.Cells[11].Value.ToString();
try
{
conn.Open();
i = cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
数据库是访问权限,类型如下:ID = Number,Company = Short Text,PriceStructure = Short Text,ItemNumber = Short Text,Description = Long Text,Unit = Short Text,Price = Number,Discount = Number ,FluidType =短文本,SubContractor =短文本,Discount_SubContract =数字,Price_SubContract =数字。有人可以帮我跟踪错误吗?
答案 0 :(得分:1)
您正在以错误的方式添加参数。不要使用AddWithValue
。改为使用添加:
cmd.Parameters.Add("@ID", OleDbType.Numeric);
cmd.Parameters["@ID"].Value = Convert.ToInt32(row.Cells[0].Value);
cmd.Parameters.Add("@Company", OleDbType.VarChar);
cmd.Parameters["@Company"].Value = row.Cells[1].Value.ToString();
//... and so on...
AddWithValue
用于在不指定数据类型的情况下添加参数,并且由于您已经指定,因此最准确的方法将是Add
,您必须在此处指定。正确使用AddWithValue
将是:
cmd.Parameters.AddWithValue("@ID", row.Cells[0].Value.ToString());
无论如何,不应该使用恕我直言AddWithValue
,因为它可能会导致数据类型转换出现问题:
http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/
编辑:您似乎在将数值转换为数据库预期值时遇到了很多问题。您应该转换为预期的类似数据类型。例如:
//THIS IS WRONG!
cmd.Parameters.AddWithValue("@Price", OleDbType.Numeric);
cmd.Parameters["@Price"].Value = row.Cells[6].Value.ToString();
//THIS IS OK!
cmd.Parameters.AddWithValue("@Price", OleDbType.Numeric);
cmd.Parameters["@Price"].Value = Convert.ToInt32(row.Cells[6].Value);