C # remotely connect to the postgresql database,
The first time I encountered a remote access to the postgresql database in the project, I often encountered database connection errors and garbled connection strings.
Solution
Add a connection string to the configuration file.
<add key="Information" value="server=182.76.17.254;Port=5432;Database=wos;uid=postgres;pwd=postgres;Encoding=UNICODE" />
Background code
String connectionString = ConfigurationManager. deleetask[ "Information"]; // create the database connection object NpgsqlConnection con = new NpgsqlConnection (connectionString); // define the query statement, it is best to write the SQL statement in the SQL statement and verify that the SQL statement is correctly copied and pasted. (when querying data, it is best not to retrieve the unnecessary data, this improves the running efficiency.) string strSql = "select * from terminals"; // con. open (); // Open the database connection (of course this sentence can be left blank) NpgsqlDataAdapter sda = new NpgsqlDataAdapter (strSql, con); DataSet ds = new DataSet (); sda. fill (ds, "terminals"); // put the executed data in the dataset. dataSource = ds. tables [0]. defaultView; // put the data in the dataset into the paging data source // DataList1.DataSource = pds; // bind Datalist DataList1.DataSource = ds. tables ["terminals"]; DataList1.DataBind (); con. close ();
Last connection successful