0

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#?

Sarfaraz Makandar
  • 5,933
  • 16
  • 59
  • 84

2 Answers2

2

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

MRebai
  • 5,344
  • 3
  • 33
  • 52
0

this worked for me

 "DefaultConnection": "User ID=postgres;Password=aamir7882;Server=localhost;Port=5432;Database=kingsurplus;Integrated Security=true;Pooling=true;

get server details and database details from pgAdmin 4