ADO.NET:如何有N个参数?

发布于 2024-09-26 18:21:59 字数 1155 浏览 2 评论 0原文

我需要将数据库中的一批行标记为“已处理”。

在过去,当事情变得更容易时,我会创建一些 SQL 来表示:

UPDATE Readings SET IsProcessed = 1 WHERE ReadingDateTime IN (
    "2010-10-07 22:02:13.327",
    "2010-10-07 22:02:14.213",
    "2010-10-07 22:02:15.595",
    ...
    "2010-10-07 23:03:36.981")

通过循环遍历日期列表:

sql = "UPDATE Readings SET IsProcessed = 1 WHERE ReadingDateTime IN (";
foreach (DateTime datetime in dates)
{
   sql = sql + CRLF+ DateTimeToSql(datetime)+",";
}
sql = sql+")";

并发出 SQL:

ExecuteNonQuery(connection, sql);

一切都很好。

现在我想尝试用困难的方式做事;我想尝试使用参数:

sql = ???;
command.CommandText = sql;

DbCommand command = connection.CreateCommand();
foreach (DateTime datetime in readings)
{
    command.Parameters.Add(new SqlParameter(???, SqlDbType.DateTime) {Value = datetime});
}
using (DbTransaction transaction = connection.BeginTransaction())
{
    command.Transaction = transaction;
    command.ExecuteNonQuery();
    transaction.Commit();
}

唯一的技巧是在 sql 字符串中放入什么,以及在参数循环的每次迭代中放入什么。他们需要一些名字。

命名任意数量的参数的推荐方法是什么?

i need to mark a batch of rows in the database as "processed".

In the olden days, when things were easier, i would create some SQL that said:

UPDATE Readings SET IsProcessed = 1 WHERE ReadingDateTime IN (
    "2010-10-07 22:02:13.327",
    "2010-10-07 22:02:14.213",
    "2010-10-07 22:02:15.595",
    ...
    "2010-10-07 23:03:36.981")

by looping through a list of dates:

sql = "UPDATE Readings SET IsProcessed = 1 WHERE ReadingDateTime IN (";
foreach (DateTime datetime in dates)
{
   sql = sql + CRLF+ DateTimeToSql(datetime)+",";
}
sql = sql+")";

And issue the SQL:

ExecuteNonQuery(connection, sql);

And all was good.

Now i want to try to do things the hard way; i want to try to use parameters:

sql = ???;
command.CommandText = sql;

DbCommand command = connection.CreateCommand();
foreach (DateTime datetime in readings)
{
    command.Parameters.Add(new SqlParameter(???, SqlDbType.DateTime) {Value = datetime});
}
using (DbTransaction transaction = connection.BeginTransaction())
{
    command.Transaction = transaction;
    command.ExecuteNonQuery();
    transaction.Commit();
}

The only trick is what to put in the sql string, and what to put in each iteration of the parameters loop. They need some name.

What's the recommended way to name arbitrary number of parameters?

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

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

发布评论

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

评论(2

戴着白色围巾的女孩 2024-10-03 18:21:59

我认为你可以做的是这样的:-

sql = "UPDATE Readings SET IsProcessed = 1 WHERE ReadingDateTime IN (";
for (int count=0; count<dates.Length; count++)
{
   sql = sql + CRLF + ":param" + count;
}
sql = sql+")";

for (int count=0; count<dates.Length; count++)
{
    command.Parameters.Add(new SqlParameter(":param" + count, SqlDbType.DateTime) {Value = datetime});
}

但是, 我认为在这种情况下专门设置参数是不必要的。

考虑到您的动态值是日期而不是字符串,您可以直接使用TryParse验证日期,以确保它们是正确的数据类型在将它们添加到原始解决方案之前!

我不知道在这种情况下使用参数是否可以实现任何额外的目标。

I think what you could do is something like this:-

sql = "UPDATE Readings SET IsProcessed = 1 WHERE ReadingDateTime IN (";
for (int count=0; count<dates.Length; count++)
{
   sql = sql + CRLF + ":param" + count;
}
sql = sql+")";

for (int count=0; count<dates.Length; count++)
{
    command.Parameters.Add(new SqlParameter(":param" + count, SqlDbType.DateTime) {Value = datetime});
}

However, i think that having parameters specifically in this scenario is kind of unnecessary.

Considering that your dynamic values are dates and not strings, you can instead validate the dates directly using a TryParse to ensure that they are the correct datatype before you append them in your original solution!!

I dont know if you are achieving anything extra over that by using parameters in this case.

鲜血染红嫁衣 2024-10-03 18:21:59

您可以尽可能少地使用:提供程序将接受名为 @0@1 等的参数。但是如果您想更具描述性,只需使用基数@ReadingDateTime 的名称,带有 0..n 的整数后缀。简单易行。

You can go as minimal as you like: the provider will accept parameters named @0, @1, etc. But if you want to be more descriptive, just use a base name of @ReadingDateTime, with an integer suffix from 0..n. Easy peasy.

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