Search code examples
c#dsn

PostgreSQL DSN Connection string in C#


I am trying to connect PostgreSQL database to C# application. For that I have created ODBC data source, now I need the connection string to include in C#.

Question: How to write DSN connection string for PostgreSQL in C#?


Solution

  • Try this

     // PostgeSQL-style connection string
       string connstring = String.Format("Server={0};Port={1};" + 
                        "User Id={2};Password={3};Database={4};",
                        tbHost.Text, tbPort.Text, tbUser.Text, 
                        tbPass.Text, tbDataBaseName.Text );
                    // Making connection with Npgsql provider
       NpgsqlConnection conn = new NpgsqlConnection(connstring);
       conn.Open();
       // quite complex sql statement
       string sql = "SELECT * FROM simple_table";
       // data adapter making request from our connection
       NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
    

    More details here http://www.codeproject.com/Articles/30989/Using-PostgreSQL-in-your-C-NET-application-An-intr