简体   繁体   中英

LastInsertedId in Npgsql(PostgreSQL)

How is this correct for Postgresql? Error on 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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