Junior — Middle
Tell us about your experience with integrating and using PostgreSQL in C# projects.
sobes.tech AI
Answer from AI
In C# projects for working with PostgreSQL, I used the Npgsql library — a .NET Data Provider for PostgreSQL that provides convenient connection and query execution.
My experience includes:
- Setting up the connection string with server, database, user, and password parameters.
- Executing SQL queries using
NpgsqlCommandand reading results viaNpgsqlDataReader. - Using parameterized queries to prevent SQL injection.
- Integrating with Entity Framework Core through the Npgsql provider for ORM approach, which simplifies data handling and migrations.
Example of a simple query with Npgsql:
using (var conn = new NpgsqlConnection(connectionString))
{
conn.Open();
using (var cmd = new NpgsqlCommand("SELECT name FROM users WHERE id = @id", conn))
{
cmd.Parameters.AddWithValue("@id", 1);
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader.GetString(0));
}
}
}
}
This approach allows efficient integration of PostgreSQL into C# projects with good performance and security.