我使用PostgreSQL和ADO.NET NpgSQL。当我刚刚开始使用PostgreSQL和NpgSQL时,请原谅这个问题的简单性。PostgreSQL参数化插入与ADO.NET
像这样工作得很好:
[Test]
public void InsertNoParameters()
{
NpgsqlConnection conn = new NpgsqlConnection("Host=localhost; Database=postgres; User ID=postgres; Password=password");
conn.Open();
IDbCommand command = conn.CreateCommand();
string sql = "INSERT INTO Customers (FirstName,LastName) VALUES ('Test','Tube')";
command.CommandText = sql;
command.ExecuteNonQuery();
conn.Close();
}
当我把参数我得到的错误信息: Npgsql.NpgsqlException:错误:42703:列 “_FirstName” 不存在
[Test]
public void InsertWithParameters()
{
NpgsqlConnection conn = new NpgsqlConnection("Host=localhost; Database=postgres; User ID=postgres; Password=password");
conn.Open();
IDbCommand command = conn.CreateCommand();
string sql = "INSERT INTO Customers (FirstName,LastName) VALUES (_FirstName,_LastName)";
command.CommandText = sql;
var parameter = command.CreateParameter();
parameter.ParameterName = "_FirstName";
parameter.Value = "Test";
command.Parameters.Add(parameter);
parameter = command.CreateParameter();
parameter.ParameterName = "_LastName";
parameter.Value = "Tube";
command.Parameters.Add(parameter);
command.ExecuteNonQuery();
conn.Close();
}
大写<>小写。请仅使用小写字母或引用您的标识符(使用双引号)。 – wildplasser
我只是试过所有的小写参数名称,我仍然得到相同的错误。 psql(或pgadmin)中的 –
检查列的实际名称。 – wildplasser