MOCKSTACKS
EN
Questions And Answers

More Tutorials









postgreSQL Accessing Data Programmatically

Examples

Accessing Postgresql from .NET using the Npgsql provider


One of the more popular .NET providers for Postgresql is Npgsql, which is ADO.NET compatible and is used nearly identically as other .NET database providers.

A typical query is performed by creating a command, binding parameters, and then executing the command. In C#:

var connString = "Host=myserv;Username=myuser;Password=mypass;Database=mydb";
using (var conn = new NpgsqlConnection(connString))
{
 var querystring = "INSERT INTO data (some_field) VALUES (@content)";
 conn.Open();
 // Create a new command with CommandText and Connection constructor
 using (var cmd = new NpgsqlCommand(querystring, conn))
 {
 // Add a parameter and set its type with the NpgsqlDbType enum
 var contentString = "Hello World!";
 cmd.Parameters.Add("@content", NpgsqlDbType.Text).Value = contentString;
 // Execute a query that returns no results
 cmd.ExecuteNonQuery();
 /* It is possible to reuse a command object and open connection instead of creating
new ones */
 // Create a new query and set its parameters
 int keyId = 101;
 cmd.CommandText = "SELECT primary_key, some_field FROM data WHERE primary_key =
@keyId";
 cmd.Parameters.Clear();
 cmd.Parameters.Add("@keyId", NpgsqlDbType.Integer).Value = keyId;
 // Execute the command and read through the rows one by one
 using (NpgsqlDataReader reader = cmd.ExecuteReader())
 {
 while (reader.Read()) // Returns false for 0 rows, or after reading the last row
of the results
 {
 // read an integer value
 int primaryKey = reader.GetInt32(0);
 // or
 primaryKey = Convert.ToInt32(reader["primary_key"]);
 // read a text value
 string someFieldText = reader["some_field"].ToString();
 }
 }
 }
} // the C# 'using' directive calls conn.Close() and conn.Dispose() for us

Accessing PostgreSQL with the C-API


The C-API is the most powerful way to access PostgreSQL and it is surprisingly comfortable.

Conclusion

In this page (written and validated by ) you learned about postgreSQL Accessing Data Programmatically . What's Next? If you are interested in completing postgreSQL tutorial, your next topic will be learning about: postgreSQL Compilation and linking.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.