我正在玩PostgreSQL和c#。我正在输入多个.AddWithValues()来填充所有数据。考虑到真正的数据库将有更多的密钥,这可能会变得毛茸茸。 有没有办法将多个AddWithValues()组合成一行或操作?
这是我的代码:
//Open the connection
//Made the id as a Guid. For now just using var id = new Guid()
//Insert multiple rows
using(var command = new NpgsqlCommand())
{
command.Connection = connection;
command.CommandText = "INSERT INTO testschema.testtable" +
"(name, id, number)" +
"VALUES (@name, @id, @number)";
command.Parameters.AddWithValue("name", "Greg");
command.Parameters.AddWithValue("id", id);
command.Parameters.AddWithValue("number", 7);
command.ExecuteNonQuery(); //Needs to be inside the loop
//TODO: have the command text and parameters constructed once(Using the Parameters.Add for the parameters
// and change values in the loop.
//Also should have one connection open. So open just before the loop and close it after
}
//Close the connection