这是什么的SqlCommand。prepare()做的,当它应该被使用? [英] What does SqlCommand.Prepare() do and when should it be used?

查看:563
本文介绍了这是什么的SqlCommand。prepare()做的,当它应该被使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/2449827/pros-and-cons-of-using-sqlcommand-$p$ppare-in-c">Pros使用SqlCommand的prepare在C#和缺点?

这是什么 MSDN说有关的SqlCommand prepare()

创建ppared版本的一个$ P $   命令的SQL Server实例。

Creates a prepared version of the command on an instance of SQL Server.

任何人都可以提供更深入的了解,以什么方式以及何时应该使用?

Can anybody provide more insight as to what that means and when it should be used?

推荐答案

的<一个href="http://msdn.microsoft.com/en-us/library/system.data.common.dbcommand.$p$ppare.aspx"><$c$c>$p$ppare方法其实对的DbCommand ,从它这派生的所有类将回升。

The Prepare method is actually on DbCommand, which all classes which derive from it will pick up.

它的作用是特定于数据库供应商的的DbCommand 是。但是,可以肯定地说(虽然不是一个绝对的规则),在大多数地方,如果命令是一个存储过程,它会产生一个无操作(它记录这样的<一个href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.$p$ppare.aspx">override对 prepare 的SqlCommand ),存储过程通常有自己的查询计划的优化,因为以前调用,显式调用优化,或创建(再次,取决于基础数据库)。

What it does is specific to the database provider that the DbCommand is for. However, it's safe to say (although not an absolute rule) that in most places, if the command is a stored procedure, it will produce a no op (it is documented as such for the override of Prepare on SqlCommand), as stored procedures typically have their query plans optimized because of prior calls, explicit calls to optimize, or on creation (again, depending on the underlying database).

不过,如果你的没有的使用存储过程,而在即时生成的参数化查询,则此调用将会给底层数据库的机会,去创造的一个优化版本查询。

However, if you are not using a stored procedure, but rather a parameterized query generated on-the-fly, then this call will give the underlying database the opportunity to generate an optimized version of the query.

您通常会做到这一点,当你知道你要在规定时间内(这取决于,真的,在数据​​库上,多长时间的查询计划被缓存)。

You would typically do this when you know you are going to execute the command multiple times within a short amount of time (it depends, really, on the database, and how long the query plans are cached).

应该指出的SQL Server(截至2005年,IIRC)缓存第一个执行(我认为缓存后,根据使用参数化查询计划是一个时间退化的高速缓存可复位,或有其速度衰减的放缓对后续使用),所以如果你要使用相同的参数化查询多次调用,那么你可能不会获得太大通过调用 prepare 不是移动查询等preparation前期工作(这可能是一个好处还有,根据工作是你必须执行)。

It should be stated that SQL Server (as of 2005, IIRC) caches parameterized query plans depending on usage after the first execution (I think the cache is a time-degraded cache which resets or has its rate of decay slowed on subsequent uses), so if you are going to make multiple calls with the same parameterized query, then you might not gain much with a call to Prepare other than moving the query preparation work upfront (which might be a benefit as well, depending on what the work is you have to perform).

这篇关于这是什么的SqlCommand。prepare()做的,当它应该被使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆