Bobney
Bobney

Reputation: 320

OleDb paramater syntax

When adding a parameter to an OLEDB command, is there a need to use 'new'. Both of the following work. Is one preferable or are they doing the same thing?

command.Parameters.Add(new OleDbParameter("name", OleDbType.VarChar)); 
command.Parameters.Add("name", OleDbType.VarChar);

Upvotes: 0

Views: 199

Answers (1)

Soner Gönül
Soner Gönül

Reputation: 98750

Yes, Add(string, OleDbType) overload calls the Add(OleDbParameter) overload. That means they do the same thing under the hood.

public OleDbParameter Add(string parameterName, OleDbType oleDbType)
{
     return Add(new OleDbParameter(parameterName, oleDbType));
}

https://referencesource.microsoft.com/#System.Data/fx/src/data/System/Data/OleDb/OleDbParameterCollection.cs,78

As Steve commented, most common way to use it (as far as I see) like;

command.Parameters.Add("name", OleDbType.VarChar).Value = "YourValue";

or if you know (of specify) your db column size (let's assume it is varchar(10)), you can add an integer as a third parameter like calling the Add(parameterName, oleDbType, size) overload;

command.Parameters.Add("name", OleDbType.VarChar, 10).Value = "YourValue";

Upvotes: 1

Related Questions