对 OleDbParameter 名称的痴迷

发布于 2024-07-10 14:25:25 字数 207 浏览 8 评论 0原文

由于 OleDbParameter 不使用命名参数(由于其性质), 为什么 .NET OleDbParameter 类需要一个名称? (stringparametername ...)

所有构造函数都需要一个参数名称,但我永远不确定该给它起什么名字; 我的名字可以吗? 或者我祖母的名字?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

真心难拥有 2024-07-17 14:25:25

尽管 OleDb/Odbc 提供程序使用位置参数而不是命名参数 -

  1. 如果您需要引用参数,则需要在 OleDbParameter 集合中以某种方式标识这些参数。

  2. 重要的是,当构造参数化 SQL 语句时,会声明每个参数的变量并为其分配各自的值。 然后在执行的 sql 语句中使用。

举个例子:

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 类似于(或者我认为接近):

DECLARE @FieldValue INT;
SET @FieldValue = 42;
SELECT * FROM MyTable WHERE Field = @FieldValue

建议您使用与正在操作的列的名称最匹配的参数名称。

Although the OleDb/Odbc providers use positional parameters instead of named parameters -

  1. the parameters will need to be identified in some way inside the OleDbParameter collection should you need to reference them.

  2. 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.

Take for example:

string sql = "SELECT * FROM MyTable WHERE Field = ?";
OleDbCommand cmd = new OleDbCommmand(sql, sqlConnection);
cmd.Parameters.Add("@FieldValue", OleDbType.Int32, 42);
OleDbDataReader dr = cmd.ExecuteReader();

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.

时光是把杀猪刀 2024-07-17 14:25:25

就像编程中的其他一切一样,将其命名为对您的上下文有意义的名称! (名称、订单 ID、城市等)。

在 C# 代码中,您可以使用名称按名称访问参数集合:

OleDbCommand command = new OleDbCommand();
...//Add your parameters with names, then reference them later if you need to:
command.Parameters["name"].Value = "your name or your grandmothers name here";

当您在执行语句后使用 OUT 参数提取值时,这也很有用:

OleDbParameter outParam = new OleDbParameter();
outParam.Direction = ParameterDirection.Output;
outParam.DbType = DbType.Date;
outParam.ParameterName = "outParam";
command.Parameters.Add(outParam);
command.ExecuteNonQuery();
DateTime outParam = Convert.ToDateTime(command.Parameters["outParam"].Value);

Just like everything else in programming, name it something meaningful to your context! (name, orderid, city, etc).

You use the names to access the parameters collection by name while in your c# code:

OleDbCommand command = new OleDbCommand();
...//Add your parameters with names, then reference them later if you need to:
command.Parameters["name"].Value = "your name or your grandmothers name here";

This is also useful when you use OUT parameters to extract the value after you execute your statement:

OleDbParameter outParam = new OleDbParameter();
outParam.Direction = ParameterDirection.Output;
outParam.DbType = DbType.Date;
outParam.ParameterName = "outParam";
command.Parameters.Add(outParam);
command.ExecuteNonQuery();
DateTime outParam = Convert.ToDateTime(command.Parameters["outParam"].Value);
乖乖 2024-07-17 14:25:25

有一些接口和工厂可以为数据访问提供一定程度的底层提供程序的独立性 - IDataParameter、DbParameter、DbProviderFactory 等。

为了支持这一点,所有参数都可以命名,即使底层提供程序未使用该名称。

There are interfaces and factories to provide a degree of independence of the underlying provider for data access - IDataParameter, DbParameter, DbProviderFactory etc.

To support this, all parameters can be named, even when the name is not used by the underlying provider.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文