gpt4 book ai didi

c# - 从列表中删除重复项,但保留其计数

转载 作者:太空宇宙 更新时间:2023-11-03 23:31:00 25 4
gpt4 key购买 nike

我正在尝试将(未知数量)多个值放入数据库(使用 ASP.NET MVC - C#),创建如下查询:

INSERT INTO [dbo].[names](name, lastname) 
VALUES ("Foo", "Bar"),
("John, "Smith"),
("Var", "Dar")

在 C# 代码中看起来像这样:

//conn is an SqlConnection object
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = @"INSERT INTO [dbo].[names](name, lastname) VALUES ";
//The reason it goes backwards is because this is only simplified insert query,
//real query also does some sorting, which does Not cause the issue.
for (int i = articleFullList.Count - 1; i > 0; i--)
{
cmd.CommandText += "(@firstname" + i + ", @lastname" + i + ")";
if(i > 0)
cmd.CommandText += ", ";
cmd.Parameters.AddWithValue("@firstname" + i, someFirstNameValue);
cmd.Parameters.AddWithValue("@lastname" + i, someLastNameValue);
}
}

但我总是得到一个

Syntax error near ,

错误,但是如果我删除 if(i > 0) 语句(及其下面的行),我会得到类似的错误

Syntax error near @firstname1

感谢阅读!

最佳答案

循环必须继续直到达到零:

for (int i = articleFullList.Count - 1; i >= 0; i--) 

关于c# - 从列表中删除重复项,但保留其计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32219502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com