SqlCommand对象的prepare方法在什么情况下有用?

发布于 2024-08-06 23:25:39 字数 47 浏览 2 评论 0原文

有谁知道 ADO.NET SqlCommand 对象的准备方法在什么情况下有用?

Does any body know that in what situations the prepare method of an ADO.NET SqlCommand Object is useful?

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

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

发布评论

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

评论(2

梦幻之岛 2024-08-13 23:25:39

我们谈论的是 SqlCommand,它意味着 Microsoft SQL Server。据我所知SQL Server一直支持它。

但尚不清楚它是否有任何用处。我还没有找到真正了解的人。许多人声称它没有任何用处,并且没有可测量的性能差异。我想以某种方式看到证据。

在另一个数据库(使用 FbCommand 对象的 Firebird)上,我知道如果您不小心,它可能会导致问题。如果您准备了一份声明但从未使用它,则交易将永远保持开放状态。我们必须从 Firebird 代码中删除所有准备工作。我们已经习惯于准备任何可能被多次执行的事情。

We are talking about a SqlCommand which implies Microsoft SQL Server. As far as I know SQL Server has always supported it.

It's not clear however if it does anything useful. I've yet to find anyone who really knows. A lot of people claim it does nothing useful and there is no measurable performance difference. I'd like to see evidence one way or another.

On another database (Firebird using an FbCommand object) I know it can cause problems if you are not careful. If you prepare a statement but then never use it a transaction gets held open forever. We had to remove all prepares from our code for Firebird. We had been in the habit of preparing anything that might get executed multiple times.

巷雨优美回忆 2024-08-13 23:25:39
  • 如果底层数据库支持它并且
  • 您要多次调用相同的命令(具有不同的参数值)。这样您可以节省一些时间,因为数据库可以重用相同的查询计划。

通常,现代数据库会缓存命令和查询计划,但通过准备好查询或命令,您仍然可以节省一些时间。

  • If the underlying database supports it and
  • If you're going to invoke the same command (with different parameter values) a number of times. This way you can save a some time because the database can reuse the same query plan.

Normally modern databases cache commands and query plans but by having your queries or commands preprepared you can still save some time.

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