In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to access the postgresql database in C#, the content is very detailed, interested friends can refer to, hope to be helpful to you.
In order to access the PostgreSQL database, you need to use the Npgsql package, the. Net Data Provider for Postgresql component. Access PostgreSQL in C # by calling the encapsulated library.
Extract the zip file, copy the Npgsql.dll and Mono.Security.dll in the extracted directory to the obj directory of the C# project, and then add the Npgsql.dll file to References in VS2008.
In the header of the C# file that needs to use Npgsql, add the following using statement.
Using Npgsql
Create a database connection for PostgreSQL
/ * de-instantiate an NpsqlConnection object with the specified connectionString * / string connectionString = "Server=127.0.0.1;Port=5432;User Id=test;Password=test;Database=testdb;" NpgsqlConnection conn = new NpgsqlConnection (string connectionString); / / Open a database connection and call conn.Open () before executing the relevant SQL; / / close a database connection and call conn.Close () after the relevant SQL is executed / * de-instantiate an NpsqlConnection object with the specified connectionString * / string connectionString = "Server=127.0.0.1;Port=5432;User Id=test;Password=test;Database=testdb;" NpgsqlConnection conn = new NpgsqlConnection (string connectionString); / / Open a database connection and call conn.Open () before executing the relevant SQL; / / close a database connection and call conn.Close () after the relevant SQL is executed
Using NpgsqlCommand.ExecuteScalar () method to obtain the retrieval result of unique value
Try {string sql = "select count (*) from test"; conn.Open (); NpgSqlCommand objCommand = new NpgSqlCommand (sql, conn); int count = Convert.ToInt32 (objCommand.ExecuteScalar ());} finally {conn.Close ();} try {string sql = "select count (*) from test"; conn.Open (); NpgSqlCommand objCommand = new NpgSqlCommand (sql, conn); int count = Convert.ToInt32 (objCommand.ExecuteScalar ());} finally {conn.Close ();}
Use the NpgsqlCommand.ExecuteReader () method to get the retrieval result of a result set (omit the Open and Close calls of conn)
String sql = "select * from test"; NpgsqlCommand objCommand = new NpgsqlCommand (sql,conn); NpgsqlDataReader dr = command.ExecuteReader (); while (dr.Read ()) {for (I = 0; I < dr.FieldCount; iTunes +) {Console.Write ("{0}\ t", dr [I]); / get the field name} int testId = dr ["id"]; / / get the value of the specified field. (id is a field of the test table). Console.WriteLine ();} dr.Close (); string sql = "select * from test"; NpgsqlCommand objCommand = new NpgsqlCommand (sql,conn); NpgsqlDataReader dr = command.ExecuteReader (); while (dr.Read ()) {for (I = 0; I < dr.FieldCount; iTunes +) {Console.Write ("{0}\ t", dr [I]); / get the field name} int testId = dr ["id"]; / / get the value of the specified field. (id is a field of the test table). Console.WriteLine ();} dr.Close ()
Use the NpgsqlCommand.ExecuteNonQuery () method to add, update and delete a record from a specified table
Add records: string sql = "insert test values (1200)"; NpgsqlCommandobjCommand = new NpgsqlCommand (sql, conn); objCommand.ExecuteNonQuery (); string sql = "insert test values (1200)"; NpgsqlCommandobjCommand = new NpgsqlCommand (sql, conn); objCommand.ExecuteNonQuery (); update records sql = "update test set price=300 where id=1"; NpgsqlCommandobjCommand = new NpgsqlCommand (sql, conn); objCommand.ExecuteNonQuery (); sql = "update test set price=300 where id=1"; NpgsqlCommandobjCommand = new NpgsqlCommand (sql, conn); objCommand.ExecuteNonQuery () Delete record sql = "delete from test where id=1"; NpgsqlCommandobjCommand = new NpgsqlCommand (sql, conn); objCommand.ExecuteNonQuery (); sql = "delete from test where id=1"; NpgsqlCommandobjCommand = new NpgsqlCommand (sql, conn); objCommand.ExecuteNonQuery ()
Using the NpgsqlDataAdapter.Fill method, put the retrieved result set into DataSet object, which can be set using DataSet object. The DataSource property of the DataGridView control of DotNet, so that all records fetched from the table are displayed in DataGridView.
String sql = "select id,price from test"; DataSet ds = new DataSet (); NpgsqlDataAdapter objAdapter = new NpgsqlDataAdapter (sql, conn); objAdapter.Fill (ds, "a"); / / "a" this table is custom dgvBaseResult.DataSource = ds.Tables ["a"]; / / dgvBaseResult is an Object of DataGridView. String sql = "select id,price from test"; DataSet ds = new DataSet (); NpgsqlDataAdapter objAdapter = new NpgsqlDataAdapter (sql, conn); objAdapter.Fill (ds, "a"); / / "a" this table is custom dgvBaseResult.DataSource = ds.Tables ["a"]; / / dgvBaseResult is an Object of DataGridView. Note: SQL statement settings can also use the following statement objAdapter.SelectCommand.CommandText = sql; on how to access the postgresql database in C # to share here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.