添加参数到一个SqlCommand .NET的更有效的方法 [英] More Efficient Way of Adding Parameters to a SqlCommand .NET

查看:184
本文介绍了添加参数到一个SqlCommand .NET的更有效的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是读一本有关如何将参数添加到一个SqlCommand在.NET中的问题,并提出了一个问题对我来说。在我所有的节目,这是我如何将参数添加到我的命令:

I was just reading a question about how to add parameters to a SqlCommand in .NET, and it raised a question for me. In all of my programs, this is how I add parameters to my commands:

SqlCommand cmd = new SqlCommand(cmdText,conn);
cmd.Parameters.Add(new SqlParameter("@name",value));

我知道,你也可以通过以下方式添加参数:

I know that you can also add parameters in the following way:

cmd.Parameters.Add(name, dbType, size).Value = value;

其中的添加参数比较好这些方法?有关系吗?我知道,使用SQL命名空间是SQL Server的查询效率更高,所以我的第一反应将是使用SqlParameter的效率会比较高。但是,由于它已经一个SqlCommand,我不太肯定。另外,由于使用的SqlParameter实例化新的对象时,可以使得它比其它情况下的效率较低?

Which of these methods of adding parameters is better? Does it matter? I know that using the Sql namespaces is more efficient with SQL Server queries, so my first response would be that using the SqlParameter would be more efficient. However, since it's already a SqlCommand, I'm not quite sure about that. Also, since using the SqlParameter instantiates a new object, could that make it less efficient than the other case?

推荐答案

这两种方式要创建的对象 - 不管你调用构造自己或其他方法是否存在,该对象仍然会被创建

Both ways are going to create objects - whether you call the constructor yourself or whether another method does, the object is still going to be created.

更重要的是,你要做出一个数据库调用。创建了十几个对象的成本将是绝对的花生的与数据库调用,即使它是一个非常快速的调用相比。

More importantly, however, you're about to make a database call. The cost of creating a dozen objects is going to be absolutely peanuts compared with the database call, even if it's a very fast call.

不要担心它 - 只是用最的的code

Don't worry about it - just use the most readable code.

这篇关于添加参数到一个SqlCommand .NET的更有效的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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