[英]Npgsql: INSERT command does not insert
NpgsqlCommand.Text: INSERT INTO mytable (id, name) VALUES (:id, :name) NpgsqlCommand.Text:插入mytable(id,name)值(:id,:name)
Parameter 1: name=id, value=42 参数1:名称= id,值= 42
Parameter 2: name=name, value="whatever" 参数2:name = name,value =“ whatever”
command.ExecuteScalar()
does not fail, but there is no new row. command.ExecuteScalar()
不会失败,但是没有新行。 No duplicate id either. 也没有重复的ID。
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...
我必须从几个DAL类中收集它以使其线性化...
The transaction is of course created before this code and is commited afterwards.
事务当然是在此代码之前创建的,然后再提交。
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;
Always check if your transactions are commited and double check if the Commit() is not commented out... 始终检查是否已提交事务,并仔细检查Commit()是否未注释掉...
Try to use 尝试使用
command.ExecuteNonQuery();
insetad of command.ExecuteScalar() 命令的insetad.ExecuteScalar()
不要忘了提交,因为您正在使用事务
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.