C # remote Connect PostgreSQL database

Source: Internet
Author: User
Tags postgresql



The first time you encounter a remote access PostgreSQL database in a project, there are often errors connecting to the database, and the connection string is garbled






Solution Solutions






To add a connection string to a 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.AppSettings ["Information"];
            // Create a database connection object
            NpgsqlConnection con = new NpgsqlConnection (connectionString);
            // Define the query statement, here it is better to write the SQL statement in SQL and verify that it is copied and pasted correctly. Operating efficiency)
            string strSql = "select * from terminals";
            
           //con.Open();//Open the database connection (Of course, this sentence can not be written)
            NpgsqlDataAdapter sda = new NpgsqlDataAdapter (strSql, con);
            DataSet ds = new DataSet ();
            sda.Fill (ds, "terminals"); // Put the execution data in the data set
            //pds.DataSource = ds.Tables [0] .DefaultView; // Put the data in the data set into the paging data source
            //DataList1.DataSource = pds; // Bind Datalist
            DataList1.DataSource = ds.Tables ["terminals"];
            DataList1.DataBind ();
            con.Close (); 





Last connection succeeded






C # remote Connect PostgreSQL database


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.