Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
IDbCommand and Parameters.AddWithValue()
Message
 
 
General information
Forum:
ASP.NET
Category:
ADO.NET
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01538772
Message ID:
01538788
Views:
29
>>>>>>Hi,
>>>>>>
>>>>>>I am working on a case of replacing what the gurus call "programming to class" with "programming to interface" for the following code:
>>>>>>
>>>>>>First, the code the way it works currently (not with interface)
>>>>>>
>>>>>>OleDbCommand oCommand = new OleDbCommand();
>>>>>>// connectionString is valid string (not shown here)
>>>>>>OleDbConnection oConnection = new OleDbConnection(connectionString);
>>>>>>oConnection.Open();
>>>>>>oCommand.CommandText = "SP_MyProc";
>>>>>>oCommand.CommandType = CommandType.StoredProcedure;
>>>>>>oCommand.Parameters.AddWithValue( "@par1", cPar1 );
>>>>>>
>>>>>>
>>>>>>The following is my replacement code (simplified):
>>>>>>
>>>>>>IDbCommand oCommand;
>>>>>>idbCommand = new OleDbCommand();
>>>>>>// connectionString is valid string (not shown here)
>>>>>>OleDbConnection oConnection = new OleDbConnection(connectionString);
>>>>>>oConnection.Open();
>>>>>>oCommand.CommandText = "SP_MyProc";
>>>>>>oCommand.CommandType = CommandType.StoredProcedure;
>>>>>>oCommand.Parameters.AddWithValue( "@par1", cPar1 );
>>>>>>
>>>>>>
>>>>>>The problem is that in the lower case code the method .AddWithValue has an error:
>>>>>>
>>>>>>'System.Data.IDataParameterCollection' does not contain definition for 'AddWithValue'
>>>>>>
>>>>>>
>>>>>>What am I missing? TIA.
>>>>>
>>>>>I suggest to not use AddWithValue method in any of your new code. Use more verbose, but correct Parameters.Add method where you can specify parameter's type. AddWithValue method has problems and it's even being deprecated.
>>>>
>>>>But I read in MSDN help that the method Add is deprecated and AddViewValue replaced it.
>>>>
>>>>http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue(v=vs.90).aspx
>>>
>>>No, only one particular implementation of the Add method is deprecated (one overload). Use the correct overload.
>>
>>I will try. Thank you.
>>
>>But my question was/is still, why the "interface"-based code didn't work but the class-based code does. I am trying to understand these concepts better.
>
>If you look at IDbCommand (http://msdn.microsoft.com/en-us/library/system.data.idbcommand.aspx) you will see that its 'Parameters' property is an IDataParameterCollection (http://msdn.microsoft.com/en-us/library/system.data.idataparametercollection.aspx) which does not have a AddValue() method. When a class implements an interface it may well (indeed is likely to) have methods and properties which are not defined in the interface.....

I see now. I think I will have to figure how to use the interface Add method (which takes one parameter) to add two parameter values. Thank you.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Reply
Map
View

Click here to load this message in the networking platform