Search code examples
postgresqlnpgsql

Why I can't delete the database?


I'm getting Npgsql.PostgresException: '42601: syntax error at or near "$1" POSITION: 25'

public async Task Delete(string dbName) { 
    using(var connection = GetNewConnection()) { 
    connection.Open();
        using(var command = new NpgsqlCommand("DROP DATABASE IF EXISTS @dbName;", connection)) {
            command.Parameters.AddWithValue("@dbName", dbName);
            command.ExecuteNonQuery();
        } 
    }
    await Task.CompletedTask;
}

Solution

  • As in the comment, DDL statements such as DROP DATABASE cannot be used with parameters.