Npgsql: INSERT command does not insert
The Problem:
NpgsqlCommand.Text: INSERT INTO mytable (id, name) VALUES (:id, :name)
Parameter 1: name=id, value=42 Parameter 2: name=name, value="whatever"command.ExecuteScalar()
does not fail, but there is no new row. No duplicate id either.
The Code:
using (var command = connection.CreateCommand()) {
command.Transaction = transaction;
command.CommandText = "INSERT INTO mytable (id, name) VALUES (:id, :name)";
var idParameter = command.CreateParameter();
idParameter.Direction = ParameterDirection.Input;
idParameter.DbType = DbType.Int32;
idParameter.ParameterNam开发者_运维百科e = "id";
idParameter.Value = 42;
command.Parameters.Add( idParameter );
var nameParameter = command.CreateParameter();
nameParameter.Direction = ParameterDirection.Input;
nameParameter.DbType = DbType.String;
nameParameter.ParameterName = "name";
nameParameter.Value = "whatever";
command.Parameters.Add( nameParameter );
command.ExecuteScalar();
}
The code above is not the real code. I had to collect it from several DAL classes to linearize it...
The Table Definition:
CREATE TABLE mytable
(
"name" character varying NOT NULL,
id integer NOT NULL,
CONSTRAINT mytable_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE mytable OWNER TO myuser;
The Question(s):
- Hu?
- Why?
- What am I doing wrong?
The Result:
Always check if your transactions are commited and double check if the Commit() is not commented out...
Try to use
command.ExecuteNonQuery();
insetad of command.ExecuteScalar()
Don't forget the Commit since you are using a transaction
精彩评论