更新 Access 数据库的某些参数的查询

发布于 2024-09-05 12:13:40 字数 391 浏览 2 评论 0原文

我使用 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 技术交流群。

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

发布评论

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

评论(2

傾旎 2024-09-12 12:13:40

每当遇到此类错误时,请插入断点并检查查询以确保其看起来符合您的预期。例如,查询中是否确实存在 _UserRole、_authId 和 _Id。

您还可以在准备语句之前添加一些防御性代码来检查它们 - 此示例检查以确保 _UserRole 不为 null 或为空。

if (!string.IsNullOrEmpty(_UserRole)) { ...

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.

if (!string.IsNullOrEmpty(_UserRole)) { ...
厌倦 2024-09-12 12:13:40

很难说没有看到你的代码,但根据错误消息,我猜测以下之一:

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.

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