C # remotely connect to the postgresql database,

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.