Question

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.ParameterName = "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 transaction is of course created before this code and is commited afterwards.

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...

Was it helpful?

Solution

Don't forget the Commit since you are using a transaction

OTHER TIPS

Try to use

command.ExecuteNonQuery();

insetad of command.ExecuteScalar()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top