惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
A
About on SuperTechFans
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
The Blog of Author Tim Ferriss
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
The Cloudflare Blog
F
Fortinet All Blogs
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
罗磊的独立博客
量子位
有赞技术团队
有赞技术团队
V
V2EX
Engineering at Meta
Engineering at Meta

博客园 - Laser.NET

WumiiRelatedItems C#创建一个线程为什么会消耗那么多的内存? 【跨平台的.NET解决方案】-Mono越来越成熟! [转]英语中符号以及标点符号的读法(或许对查看英文资料的人有些帮助) 06年中国互联网调查结果之:网民上网经常使用的网络服务 让短信更好的为企业服务!——短消息交互平台简介 GoF Patterns [ASP.NET2.0]一个方便使用的在线截图Web控件-WebImageMaker [转]经典的几个网络签名:) [原创] 我的生活我安排,我的调度我做主!——介绍一个 日程安排/工作计划/任务调度 的Scheduling组件。(二) [原创] 我的生活我安排,我的调度我做主!——介绍一个 日程安排/工作计划/任务调度 的Scheduling组件。(一) [ADO.NET]由数据库触发器引发的问题 [转] IE的bug列表 [C#语言] C#语言中又一个自认为不太理想的地方。。。 [辅助工具] 一个方便将ASP代码升级到ASP.NET的小工具 -- ASP Code Migrator! 关于ASP.NET页面嵌入代码的高亮显示的问题。 [C#语言] C#语法中一个很奇怪的地方:) 这样的网站,2个月是否可能?? [新手点滴] C# vs VB.NET 哪个好?
[新手点滴] IDbCommand.Prepare()方法。
Laser.NET · 2006-03-06 · via 博客园 - Laser.NET

今天在逛新闻组的时候无意中看到了SqlCommand.Prepare()方法。在IDbCommand接口上就定义了Prepare()方法,这个方法可以把CommandType为Text的SQL语句提前在数据库中编译为一个临时的StoredProcedure然后再执行,这样对于需要多次执行的DbCommand来说,可以提高一定的执行效率:)注意:请在指定了Command的Connection之后再调用Prepare()方法。

此方法在msdn上的介绍如下:

Creates a prepared (or compiled) version of the command on the data source.

Remarks

If the CommandType property is set to TableDirect, Prepare does nothing. If CommandType is set to StoredProcedure, the call to Prepare should succeed, although it may result in a no-op.

Example
The following example creates an instance of the derived class, OleDbCommand, and opens the connection. The example then prepares a stored procedure on the data source by passing a string that is a SQL Select statement and a string to use to connect to the data source.
[C#]

public void OleDbCommandPrepareEx() {
   
int  id = 20;
   
string  desc = "myFirstRegion" ;
   OleDbConnection rConn 
= new OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");
   rConn.Open();
   OleDbCommand command    
= new OleDbCommand(null, rConn);

   
// Create and prepare an SQL statement.
   command.CommandText = "insert into Region (RegionID, RegionDescription) values (@id, @desc)" ;
   command.Parameters.Add ( 
"@id", id) ;
   command.Parameters.Add ( 
"@desc", desc) ;
   command.Prepare() ;  
// Calling Prepare after having set the Commandtext and parameters.
   command.ExecuteNonQuery();

   
// Change parameter values and call ExecuteNonQuery.
   command.Parameters[0].Value = 21;
   command.Parameters[
1].Value = "mySecondRegion";
   command.ExecuteNonQuery();
}