using (var conn = new NpgsqlConnection(connectionString)) { conn.Open(); var cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Students"; NpgsqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader[0] + " " + reader[1]); } }
string connectionString = "Server=localhost;User Id=username;Password=password;Database=database"; using (var conn = new NpgsqlConnection(connectionString)) { conn.Open(); var cmd = new NpgsqlCommand("INSERT INTO Students (Name,Age) VALUES ('Tom', 25)", conn); int rows = cmd.ExecuteNonQuery(); }In this code example, a new connection is established with the PostgreSQL database. The CreateCommand method is not used, rather a new command is created using the NpgsqlCommand constructor. The command is used to insert a new record into the Students table, and the number of affected rows is returned and stored in the rows variable. Package library: `Npgsql`
public CreateCommand ( ) : |
||
return |