テーブルがあります。これはテーブルのスクリプトです:-
CREATE TABLE news
(
news_id bigint NOT NULL DEFAULT nextval('news_seq'::regclass),
title character varying(1024),
description character varying(2024),
CONSTRAINT pk_news_newsid PRIMARY KEY (news_id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE news OWNER TO viewer;
news_id
ここで、テーブルに新しいレコードを挿入するときに自動生成を取得したいと考えて います。
これは、ニュースを挿入するための C# 関数です:-
public Int64 AddNews(News newNews)
{
string query = string.Empty;
try
{
string dateFormat = ConfigurationManager.AppSettings["DateFormat"];
NpgsqlParameter[] parm = new NpgsqlParameter[2];
parm[0] = new NpgsqlParameter("title", newNews.NewsTitle);
parm[1] = new NpgsqlParameter("des", newNews.NewsDescription);
query = @" INSERT INTO news(title, description)
VALUES (:title, :des)
Returning news_id";
int id=NpgSqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, query, parm);
return id;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
On executing this function I always get the -1 value
前もって感謝します
pgAdmin で次のクエリを実行すると、正しい結果が得られます。
INSERT INTO news(title, description)
VALUES ('title','description')
Returning news_id