循环内添加SQL参数,循环外执行。 (在 C# SQL 客户端中使用参数进行单查询多插入)
Add SQL Parameters inside the loop, excecute outside the loop. (Single query multi-insert with parameters in C# SQL Client)
所以,想要进行多行插入查询,我需要用我有值的循环内的值替换键。
它通过将值硬编码到查询字符串中来工作,但我需要使用“cmd.Parameters.AddValue()
或 cmd.Parameters.AddWithValue()
”来完成它,因为我需要防止 SQL 注入.
所以,我的代码是这样的:
string query = "insert into dbo.Foo (column1, column2, column3) values ";
SqlCommand cmd
foreach (line in rowsArray) {
cmd.Parameters.Clear();
cmd = new SqlCommand(query, cnn); //So, the problem is this override
query += "(@key1, @key2, @key3), ";
cmd.Parameters.AddWithValue("@key1", line.value1);
cmd.Parameters.AddWithValue("@key2", line.value2);
cmd.Parameters.AddWithValue("@key3", line.value3);
}
query = query.Substring(0, query.Length-2); //Last comma
cmd.ExecuteNonQuery();
cnn.Close();
我想ExecuteNonQuery();
在循环外,只插入一个。
有什么想法吗?
我考虑过制作一个循环,在该循环中我使用标识符在字符串中添加键,然后用相同的 ID 迭代另一个循环来替换所有键,但我认为这不是非常有效或好的做法。
我最终决定制作两个循环,因为它的效果比预期的要好。
I thought about making a loop where I add the keys in the string with a identifier and then replacing all of them iterating another loop with the same id's, but I don't see that very efficient or a good practice.
通过这种方法,我完成了字符串查询,然后添加了值。我给了键 ID,这样我就可以在下一个循环中使用相同的 ID 按顺序用值替换它们。
string query = "insert into dbo.Foo (column1, column2, column3) values ";
int id = 0;
foreach (line in rowsArray) {
query += "(@key1"+id+", @key2"+id+", @key3"+id+"), ";
id++;
}
query = query.Substring(0, query.Length-2); //Last comma
SqlCommand cmd = new SqlCommand(query, cnn);
id = 0;
foreach (line in rowsArray) {
cmd.Parameters.AddWithValue("@key1"+id, line.value1);
cmd.Parameters.AddWithValue("@key2"+id, line.value2);
cmd.Parameters.AddWithValue("@key3"+id, line.value3);
}
cmd.ExecuteNonQuery();
cnn.Close();
此外,提及 SqlBulkCopy 的存在,它允许上传 DataTables 并将产生更清晰的代码。
所以,想要进行多行插入查询,我需要用我有值的循环内的值替换键。
它通过将值硬编码到查询字符串中来工作,但我需要使用“cmd.Parameters.AddValue()
或 cmd.Parameters.AddWithValue()
”来完成它,因为我需要防止 SQL 注入.
所以,我的代码是这样的:
string query = "insert into dbo.Foo (column1, column2, column3) values ";
SqlCommand cmd
foreach (line in rowsArray) {
cmd.Parameters.Clear();
cmd = new SqlCommand(query, cnn); //So, the problem is this override
query += "(@key1, @key2, @key3), ";
cmd.Parameters.AddWithValue("@key1", line.value1);
cmd.Parameters.AddWithValue("@key2", line.value2);
cmd.Parameters.AddWithValue("@key3", line.value3);
}
query = query.Substring(0, query.Length-2); //Last comma
cmd.ExecuteNonQuery();
cnn.Close();
我想ExecuteNonQuery();
在循环外,只插入一个。
有什么想法吗?
我考虑过制作一个循环,在该循环中我使用标识符在字符串中添加键,然后用相同的 ID 迭代另一个循环来替换所有键,但我认为这不是非常有效或好的做法。
我最终决定制作两个循环,因为它的效果比预期的要好。
I thought about making a loop where I add the keys in the string with a identifier and then replacing all of them iterating another loop with the same id's, but I don't see that very efficient or a good practice.
通过这种方法,我完成了字符串查询,然后添加了值。我给了键 ID,这样我就可以在下一个循环中使用相同的 ID 按顺序用值替换它们。
string query = "insert into dbo.Foo (column1, column2, column3) values ";
int id = 0;
foreach (line in rowsArray) {
query += "(@key1"+id+", @key2"+id+", @key3"+id+"), ";
id++;
}
query = query.Substring(0, query.Length-2); //Last comma
SqlCommand cmd = new SqlCommand(query, cnn);
id = 0;
foreach (line in rowsArray) {
cmd.Parameters.AddWithValue("@key1"+id, line.value1);
cmd.Parameters.AddWithValue("@key2"+id, line.value2);
cmd.Parameters.AddWithValue("@key3"+id, line.value3);
}
cmd.ExecuteNonQuery();
cnn.Close();
此外,提及 SqlBulkCopy 的存在,它允许上传 DataTables 并将产生更清晰的代码。