0

Is there a way to query a PostgreSQL db in C# in a database agnostic way ie without Npgsql. I want use built in .NET libraries and a connection-string that contains the DB IP, Username, Password and any other info that the connection-string might require.

5
  • 1
    one option would be ODBC connection Commented Sep 1, 2017 at 17:21
  • Could you please explain why Npgsql is not a solution? Commented Sep 1, 2017 at 17:57
  • I want use built in .NET libraries. Also ODBC would require the setup of a DSN and installation of a driver on the server which isn't an option. There are no built in classes or providers in the .NET framework to achieve what Im trying to do ? Commented Sep 1, 2017 at 18:08
  • With ODBC, you can pass all the connection arguments in your .net code, no need to set up the DSN. As for the driver, it is not installed on the DB server but on the client side. You use generic .net objects, but and the driver translates them to postgres. Commented Sep 2, 2017 at 12:00
  • do you have a code sample for this ? Commented Sep 2, 2017 at 12:28

1 Answer 1

0

You can use ODBC and construct the connection within your app. The Postgres driver has to be installed on the machine running the program. Your app would then only be using the generic ODBC classes. Below is a simple example.

using System.Data.Odbc;

string connectString = "Driver={PostgreSQL UNICODE};Port=5432;Server=localhost;Database=myDBname;Uid=myusername;Pwd=mypassword;";

OdbcConnection connection = new OdbcConnection(connectString);

connection.Open();

string sql = "select version()";
OdbcCommand cmd = new OdbcCommand(sql, connection);

OdbcDataReader dr = cmd.ExecuteReader();
dr.Read();
string dbVersion = dr.GetString(0);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.