更新 Access 数据库的某些参数的查询
我使用 MS Access 作为数据库,并使用 c#、.net 来更新其中的一些记录。但它给出错误消息
“没有为一个或多个必需参数给出值”。
表中有 5 列,我只想更新 2 列,因为我已经编写了查询,其中
"update User_DTL set user_role_id = '" + _UserRole + "', auth_id ='" + _authId + "'"
+ " WHERE Id = '" + _Id + "' ";
_UserRole、_authId、_Id 是字符串。
可能是什么错误。我需要在更新语句中给出每个参数还是有其他方法。
谢谢
I am using MS Access as a database and using c#, .net for updating some records in it. But it is giving error saying
"No value given for one or more required parameters."
There are 5 colums in Table and I want to update only 2, for that I have written the query like
"update User_DTL set user_role_id = '" + _UserRole + "', auth_id ='" + _authId + "'"
+ " WHERE Id = '" + _Id + "' ";
where _UserRole, _authId, _Id are strings.
What may be the error. Do I need to give every parameter in update statement or there is some other way.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每当遇到此类错误时,请插入断点并检查查询以确保其看起来符合您的预期。例如,查询中是否确实存在 _UserRole、_authId 和 _Id。
您还可以在准备语句之前添加一些防御性代码来检查它们 - 此示例检查以确保 _UserRole 不为 null 或为空。
Whenever you encounter such an error, stick a breakpoint in and examine your query to ensure it looks as you expect. For example, is there actually a _UserRole, _authId and _Id present in the query.
You could also add some defensive code to check them before you prepare the statement - this example checks to make sure the _UserRole isn't null or empty.
很难说没有看到你的代码,但根据错误消息,我猜测以下之一:
1)User_DTL 中不存在以下字段之一:user_role_id、auth_id、Id
2) _UserRole、_authId、_Id 包含单引号字符。
解决此问题的最佳方法是打印实际的串联查询字符串,然后在 Access 中打开 SQL 查询并运行它。那么问题是什么就应该很明显了。
顺便说一句:这段代码可能存在一些 SQL 注入漏洞。
Hard to say without seeing your code, but based on the error message I'm guessing one of the following:
1) One of the following fields does not exist in User_DTL: user_role_id, auth_id, Id
2) _UserRole, _authId, _Id contains a single-quote character.
The best way to troubleshoot this is to print the actual concatenated query string and then open a SQL Query in Access and run it. It should be pretty obvious what the problem is then.
BTW: You likely have some SQL Injection vulnerabilities with this code.