C# 访问数据库程序

发布于 2024-12-12 05:22:48 字数 1268 浏览 3 评论 0原文

String strSql = "insert into BaseData (Item," + dataGridView1.Columns[3].Name + "," + dataGridView1.Columns[4].Name + ") values ('" + row.Cells[0].Value + "','" + row.Cells[3].Value + "','" + row.Cells[4].Value + "')";                        

objCmd = new OleDbCommand(strSql, lConn);  
objCmd.ExecuteNonQuery();

strSql = "select id from BaseData where Item = '" + row.Cells[0].Value + "' and " + dataGridView1.Columns[1].Name + " = '" + row.Cells[3].Value + "' And " + dataGridView1.Columns[2].Name + " = '" + row.Cells[4].Value + "'";

OleDbCommand command = new OleDbCommand(strSql, lConn);                           
OleDbDataReader reader = command.ExecuteReader();   
String id = "";   
while (reader.Read())
{  
  id = reader.GetString(0);
}    
reader.Close();

strSql = "insert into tranjaction (Base_id,quentity,price,other) values ('" + id + "' , ' " + row.Cells[2].Value + "','" + row.Cells[1].Value + "')";

objCmd = new OleDbCommand(strSql, lConn);  
objCmd.ExecuteNonQuery();

当我运行此命令时,第 10 行出现 Microsoft Visual Studio 错误。(ExecuteReader)

错误就在这里。

“System.Data.OleDb.OleDbException”类型的未处理异常 发生在System.Data.dll

附加信息:没有为一个或多个必需的值给出值 参数。

我该如何修复这个错误?

String strSql = "insert into BaseData (Item," + dataGridView1.Columns[3].Name + "," + dataGridView1.Columns[4].Name + ") values ('" + row.Cells[0].Value + "','" + row.Cells[3].Value + "','" + row.Cells[4].Value + "')";                        

objCmd = new OleDbCommand(strSql, lConn);  
objCmd.ExecuteNonQuery();

strSql = "select id from BaseData where Item = '" + row.Cells[0].Value + "' and " + dataGridView1.Columns[1].Name + " = '" + row.Cells[3].Value + "' And " + dataGridView1.Columns[2].Name + " = '" + row.Cells[4].Value + "'";

OleDbCommand command = new OleDbCommand(strSql, lConn);                           
OleDbDataReader reader = command.ExecuteReader();   
String id = "";   
while (reader.Read())
{  
  id = reader.GetString(0);
}    
reader.Close();

strSql = "insert into tranjaction (Base_id,quentity,price,other) values ('" + id + "' , ' " + row.Cells[2].Value + "','" + row.Cells[1].Value + "')";

objCmd = new OleDbCommand(strSql, lConn);  
objCmd.ExecuteNonQuery();

When I run this, Microsoft Visual Studio Error occur on Line 10. (ExecuteReader)

The Error is here.

An unhandled exception of type 'System.Data.OleDb.OleDbException'
occurred in System.Data.dll

Additional information: No value given for one or more required
parameters.

How could I fix this error?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哭了丶谁疼 2024-12-19 05:22:48

您的字段名称错误(数量?)或者是因为您的参数字段不平衡。您要插入 4 个字段,但仅提供 3 个值。使用参数代替,它会让你的生活更轻松。

尝试将其更改为这样:

strSql = "insert into tranjaction (Base_id,quentity,price,other) values (@id , @quentity, @price, @other)";
using (OleDbCommand cmd = new OleDbCommand(sqlSql, IConn)) {
  cmd.Parameters.AddWithValue("@id", id);
  cmd.Parameters.AddWithValue("@quentity", row.Cells[2].Value);
  cmd.Parameters.AddWithValue("@price", row.Cells[1].Value);
  cmd.Parameters.AddWithValue("@other", other);  //  <- missing
  cmd.ExecuteNonQuery();
}

Either your field names are wrong (quentity?) or it's because you have unbalanced fields to parameters. You are inserting into 4 fields, but you are only supplying 3 values. Use parameters instead, it would make your life easier.

Try changing it to this:

strSql = "insert into tranjaction (Base_id,quentity,price,other) values (@id , @quentity, @price, @other)";
using (OleDbCommand cmd = new OleDbCommand(sqlSql, IConn)) {
  cmd.Parameters.AddWithValue("@id", id);
  cmd.Parameters.AddWithValue("@quentity", row.Cells[2].Value);
  cmd.Parameters.AddWithValue("@price", row.Cells[1].Value);
  cmd.Parameters.AddWithValue("@other", other);  //  <- missing
  cmd.ExecuteNonQuery();
}
雨夜星沙 2024-12-19 05:22:48

当我过去收到此错误时,总是因为一个简单的拼写错误。就像其他人提到的那样,您似乎将“数量”拼写错误,这可能是问题的一部分。

When I've received this error in the past it has always been because of a simple typo. Like the others have mentioned it looks like you spelled "quantity" wrong which could be part of the problem.

≈。彩虹 2024-12-19 05:22:48

在此行中,

strSql = "insert into tranjaction (Base_id,quentity,price,other) values ('" + id + "' , ' " + row.Cells[2].Value + "','" + row.Cells[1].Value + "')";

您将插入 4 个值(Base_id、数量、价格、其他),但仅插入 3 个值(id、row.Cells[2].Value、row.Cells[1].Value)。为 other 指定一个值应该可以解决问题。

另一件可能导致此错误的事情(这可能是您的情况)是,如果您错误地拼写了其中一个列名称。由于您使用的是 datagridview 中的列名称,并且某些编写的列名称似乎拼写错误,因此您应该仔细检查查询字符串中的拼写。

In this line

strSql = "insert into tranjaction (Base_id,quentity,price,other) values ('" + id + "' , ' " + row.Cells[2].Value + "','" + row.Cells[1].Value + "')";

you are inserting 4 values (Base_id,quentity,price,other), but you are only inserting 3 values (id, row.Cells[2].Value, row.Cells[1].Value). Giving a value for other should fix the problem.

Another thing that would cause this error, which is probably your situation, is if you've spelled one of the column names incorrectly. And since you are using column names from your datagridview, and some of your written column names seem mis-spelled, you should double check the spelling in your query strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文