I'm trying to set up a connection to a PostgreSQL database as a datasource for a WinForms DataGridView.
I can't figure out how to set up the connection.
I'm using DBeaver for my DBMS and have set up a test table with some simple data. Right now, I'm just trying to test the connection by sending the version to the console. The server is on a network and the host is an IP address.
In Form1_Load I've tried this:
string strConnString = "Server=<ipaddress>;Port=5432;Username=<username>;Password=<password>;Database=test.cars"; NpgsqlConnection objConn = new NpgsqlConnection(strConnString); try { objConn.Open(); string strSelectCmd = "select version()"; var cmd = new NpgsqlCommand(strSelectCmd, objConn); var version = cmd.ExecuteScalar().ToString(); Console.WriteLine($"PostgreSQL version: {version}"); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error message", MessageBoxButtons.OK, MessageBoxIcon.Error); }
But I get an exception at the `objConn.Open();` line.
So, am I not setting up the connection string correctly? Once I get that connected, is the remainder of the code correct?
Then there's the question of how to connect the db to a DataGridView (the only way I know how to do that is via the wizard and I've only done that with a SQL Server db - but first things first...).