该OleDbParameter的名字痴迷 [英] The OleDbParameter's name obsession
问题描述
由于 OleDbParameter
不使用命名参数(由于其性质),
那为什么在.NET OleDbParameter
类需要一个名字吗? (字符串参数名称......)
Since the OleDbParameter
does not use named parameters (due to its nature),
why is it that the .NET OleDbParameter
class expects a name? (string parametername ...)
所有构造函数需要一个参数名称,而且我从来不知道什么名字给它;我的名字叫好吗?还是我的祖母的名字?
All constructors require a parameter name, and I'm never sure what name to give it; my name is ok? or my grandmothers name?
推荐答案
虽然使用OLEDB / ODBC提供使用命名参数位置参数来代替 -
Although the OleDb/Odbc providers use positional parameters instead of named parameters -
-
该参数将需要以某种方式OleDbParameter集合中,如果您需要参考他们被认出。
the parameters will need to be identified in some way inside the OleDbParameter collection should you need to reference them.
重要的时候,参数化构造SQL语句,为每个参数变量声明,并赋予其各自的价值。然后在被执行的SQL语句中使用。
importantly when the parameterised sql statement is constructed, a variable for each parameter is declared and assigned it's respective value. and then used in the sql statement that is executed.
举个例子:
string sql = "SELECT * FROM MyTable WHERE Field = ?";
OleDbCommand cmd = new OleDbCommmand(sql, sqlConnection);
cmd.Parameters.Add("@FieldValue", OleDbType.Int32, 42);
OleDbDataReader dr = cmd.ExecuteReader();
执行的SQL将类似(或接近我认为):
The SQL executed would be something like (or close to I think):
DECLARE @FieldValue INT;
SET @FieldValue = 42;
SELECT * FROM MyTable WHERE Field = @FieldValue
您会被建议使用正在操作的最佳匹配的列的名称参数名称。
You would be advised to use a parameter name that best matches the name of the column being operated on.
这篇关于该OleDbParameter的名字痴迷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!