ADO.NET:如何有N个参数?
我需要将数据库中的一批行标记为“已处理”。
在过去,当事情变得更容易时,我会创建一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你可以做的是这样的:-
但是, 我认为在这种情况下专门设置参数是不必要的。
考虑到您的动态值是
日期
而不是字符串
,您可以直接使用TryParse
验证日期,以确保它们是正确的数据类型在将它们添加到原始解决方案之前!我不知道在这种情况下使用参数是否可以实现任何额外的目标。
I think what you could do is something like this:-
However, i think that having parameters specifically in this scenario is kind of unnecessary.
Considering that your dynamic values are
dates
and notstrings
, you can instead validate the dates directly using aTryParse
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.
您可以尽可能少地使用:提供程序将接受名为
@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 from0..n
. Easy peasy.