繁体   English   中英

Npgsql(PostgreSQL) 中的 LastInsertedId

[英]LastInsertedId in Npgsql(PostgreSQL)

这对于 Postgresql 来说如何正确? LastInsertedId 上的错误

int playerId = 0;
using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
{
    try
    {
        connection.Open();
        NpgsqlCommand command = connection.CreateCommand();
        command.CommandText = "INSERT INTO users (name) VALUES (@playerName)";
        command.Parameters.AddWithValue("@playerName", playerModel.realName);
        command.ExecuteNonQuery();

        // Get the inserted identifier LastInsertedId
        playerId = (int)command.LastInsertedId;

        command.Parameters.AddWithValue("@playerId", playerId);
    }
    catch (Exception ex)
    {
        //
    }
}
command.CommandText = "INSERT INTO users (name) VALUES (@playerName) RETURNING id";
playerId = (int)command.ExecuteScalar();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM