DbCommand参数集合缺少AddWithValue方法 [英] DbCommand Parameters collection missing AddWithValue method

查看:62
本文介绍了DbCommand参数集合缺少AddWithValue方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在xxxCommand类中的每个ADO.NET提供程序中,有很多"Add"的Parameters集合。方法之一是AddWithValue,接受参数的名称和参数的值。
使用不可知的DBCommand,只有Add方法和AddWithValue缺失。
你必须用factory.CreateParameter()创建一个新的DbParameter然后设置参数名称和值。
为什么这个类中缺少这个有用的方法???
提前谢谢。

解决方案

< p class = MsoNormal style ="margin:0cm 0cm 0pt"> 嗨delfo,



我使用反射器来查看DbParameterCollection和SqlParameterCollection的机制。


在SqlParameterCollection的AddWithValue方法中,它实际调用构造函数:


public SqlParameter AddWithValue( string parameterName, object value)


{


返回 .Add( n ew SqlParameter(parameterName,value));


}


< span style ="font-size:10pt; font-family:'Verdana','sans-serif'"lang = EN-US>


所以它就像你在DbPrameter中做的一样。



并且SqlParameterCollection继承自DbParameterCollection。其他ParameterCollections也继承 形式。


在我的选择中,让父类更整洁更有利。当AddWithValue可以由构造函数完成时,虽然方便,但是不必在父类中添加它。但是,当涉及到子类时,对其他类的影响较小。



你可以从这个URL下载反射器:http://www.red-gate.com/products/reflector/


最好的问候


Yichun Feng



此回复包含对第三方Worl的引用d宽网站。 Microsoft提供此信息是为了方便您。 Microsoft不控制这些网站,也未测试在这些网站上找到的任何软件或信息;因此,Microsoft不能就其中发现的任何软件或信息的质量,安全性或适用性做出任何陈述。使用互联网上的任何软件都存在固有的危险,并且Microsoft提醒您在从互联网上检索任何软件之前确保您完全了解风险。


Hi,
in every ADO.NET provider inside the xxxCommand class there's the Parameters collection that have a lot of "Add" method one of them is AddWithValue, that accept parameter's name and parameter's value.
Using the agnostic DBCommand, there's only the Add method and AddWithValue is missing.
You must create a new DbParameter with factory.CreateParameter() and than set Parameter name and value.
Why is this usefull method missing in this class ???
Thanks in advance.

解决方案

Hi delfo,

 

I use reflector to look at the mechanism of DbParameterCollection and SqlParameterCollection.

In the SqlParameterCollection’s AddWithValue method, it calls the constructor actually:

        public SqlParameter AddWithValue(string parameterName, object value)

        {

            return this.Add(new SqlParameter(parameterName, value));

        }

 

So it just do the same thing as you do in DbPrameter.

 

And SqlParameterCollection inherits from DbParameterCollection. other ParameterCollections inherit form  it too.

In my option, making the parent class neat is more favorable. When AddWithValue can be done by constructor, it is not very necessary to add it in the parent class although it is convenient. However, when it comes to child class, the effect to other classes is lower.


You can download reflector form this URL: http://www.red-gate.com/products/reflector/
 

Best Regards

Yichun Feng


This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


这篇关于DbCommand参数集合缺少AddWithValue方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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