如何生成下一个主键值

发布于 2024-12-19 14:03:00 字数 740 浏览 0 评论 0原文

我正在使用 Visual Studio 2010 PostgreSQL 9.x Npgsql

我正在尝试从 C# WinForms 应用程序上的字段插入数据。我只是不知道如何动态生成/检索下一个主键值。这是我的专栏名称:

epiphanyKey [PK] bigserial 交易数字 许可证字符 活动日期日期 我需要

插入命令是“会话安全的”,因为多个人可以同时将数据输入数据库。

 NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=password;Database=epiphany;"); // postgres 8.3 on my test system
 conn.Open(); // opens the connection

 NpgsqlCommand cmd = new NpgsqlCommand("INSERT INTO wsmsmrs210 (epiphanyKey,transaction,license,dateOfActv,time, conn);

   NpgsqlDataReader dr = cmd.ExecuteReader();

在上面的代码中,NpgsqlCommand cmd = ... 语句无法正常工作,因为我不知道主键值 epiphanyKey 的下一个主键值。

将查询发送到数据库时生成下一个主键值的任何想法或代码片段?

I'm using Visual Studio 2010
PostgreSQL 9.x
Npgsql

I'm trying to insert data from fields on a C# WinForms app. I just can't figure out how to generate/retrieve the next primary key value on the fly. Here are my column names:

epiphanyKey [PK] bigserial
transaction numeric
license character
dateOfActv date
time time

I need the insert command to be "session-safe" since multiple people could be entering data into the database at the same time.

 NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=password;Database=epiphany;"); // postgres 8.3 on my test system
 conn.Open(); // opens the connection

 NpgsqlCommand cmd = new NpgsqlCommand("INSERT INTO wsmsmrs210 (epiphanyKey,transaction,license,dateOfActv,time, conn);

   NpgsqlDataReader dr = cmd.ExecuteReader();

In the code above the NpgsqlCommand cmd = ... statement doesn't work correctly because I don't know the next primary key value for the primary key value epiphanyKey.

Any ideas or code sniplets to generate the next primary key value when sending the query to the db?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扮仙女 2024-12-26 14:03:00

您可以使用returning关键字使查询返回刚刚创建的id。 文档中的示例:

INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
RETURNING did;

您发布的代码不完整,甚至可以编译,所以我不能给你一个完整的例子如何在你的情况下使用它,但这是一个开始:

NpgsqlCommand cmd = new NpgsqlCommand(
  "INSERT INTO wsmsmrs210 " +
  "(epiphanyKey,transaction,license,dateOfActv,time, conn) " +
  "VALUES (...) " +
  "RETURNING epiphanyKey");

int id = cmd.ExecuteScalar();

You can use the returning keyword to make the query return the id that was just created. Example from the docs:

INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
RETURNING did;

The code that you posted is not complete, or even possible to compile, so I can't give you a complete example how to use it in your case, but here is a start:

NpgsqlCommand cmd = new NpgsqlCommand(
  "INSERT INTO wsmsmrs210 " +
  "(epiphanyKey,transaction,license,dateOfActv,time, conn) " +
  "VALUES (...) " +
  "RETURNING epiphanyKey");

int id = cmd.ExecuteScalar();
治碍 2024-12-26 14:03:00

您应该使用 序列 在数据库内自动生成主 ID插入新对象。

要获得更详细的答案(代码),我需要了解 wsmsmrs210 表结构。

一般来说,如果您的数据表是用以下内容创建的:

CREATE SEQUENCE sequence_name;

CREATE TABLE wsmsmrs210 (
epiphanyKey bigint default nextval('sequence_name'),
...
)

那么您的代码应该如下所示:

NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=password;Database=epiphany;"); 
 conn.Open(); // opens the connection

NpgsqlCommand cmd = new NpgsqlCommand("INSERT INTO wsmsmrs210 (transaction,license,dateOfActv,time) VALUES(:a,:b,:c,:d)", conn);
... // add parameters :a, :b, :c, :d via cmd.Parameters.Add(...) here
cmd.ExecuteNonQuery();

// Add next two lines if you need last inserted id in code
cmd = new NpgsqlCommand("SELECT CURRVAL('sequence_name')", conn);
var id = cmd.ExecuteScalar(); 


conn.Close();

如果您想获得最大的可靠性,那么您应该在 PLPGSQL 中编写一个存储函数,该函数将接受您的字段值作为输入参数,将值插入到表中,获取最后插入 id 并返回它。

You should use Sequences for generating your primary ID automatically inside database when inserting new objects.

For more elaborate answer (code) I'll need to know the wsmsmrs210 table structure.

Generally, if your datatable is created with:

CREATE SEQUENCE sequence_name;

CREATE TABLE wsmsmrs210 (
epiphanyKey bigint default nextval('sequence_name'),
...
)

then your code should look like:

NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=password;Database=epiphany;"); 
 conn.Open(); // opens the connection

NpgsqlCommand cmd = new NpgsqlCommand("INSERT INTO wsmsmrs210 (transaction,license,dateOfActv,time) VALUES(:a,:b,:c,:d)", conn);
... // add parameters :a, :b, :c, :d via cmd.Parameters.Add(...) here
cmd.ExecuteNonQuery();

// Add next two lines if you need last inserted id in code
cmd = new NpgsqlCommand("SELECT CURRVAL('sequence_name')", conn);
var id = cmd.ExecuteScalar(); 


conn.Close();

If you want to have maximum reliability then you should write a stored function in PLPGSQL that will accept your field values as input parameters, insert the value to the table, get last insert id and return it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文