postgresql - Querying postgres from c# using npgsql -
i have installed postgres in windows machine , started working on poc. able connect database windows command line. but, unable connect c# application.
it seems have issues in connection string. have gone through multiple tutorials every tutorial has own way of providing connection string parameters.is there standard way of giving hostname,port,username,password , database.
i trying query using rest api. am, doing right way.
// api/values [httpget] public iactionresult get() { test test = new test(); return ok(test.table2json()); } using npgsql; using system; namespace zeiss.mccneo.datamigration.utilities { public class test { private readonly npgsqlconnection conn; public test() { conn = new npgsqlconnection("server=127.0.0.1;user id=postgres;" + "password=postgres;database=postgres;"); //also tried using conn = new npgsqlconnection("server=127.0.0.1;port=5432;user id=postgres;" + "password=postgres;database=postgres;"); } public string table2json() { string value = null; npgsqlcommand command = new npgsqlcommand("select * test", conn); npgsqldatareader dr = command.executereader(); while (dr.read()) { value = dr[0].tostring(); } return value; } } }
exception:
- $exception {system.invalidoperationexception: connection not open @ npgsql.npgsqlconnection.checkreadyandgetconnector() @ npgsql.npgsqlcommand.<executedbdatareader>d__92.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.exceptionservices.exceptiondispatchinfo.throw() @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter`1.getresult() @ system.runtime.compilerservices.valuetaskawaiter`1.getresult() @ npgsql.npgsqlcommand.executedbdatareader(commandbehavior behavior) @ npgsql.npgsqlcommand.executereader() @ zeiss.mccneo.datamigration.utilities.test.table2json() in c:\users\inpyadav\documents\visual studio 2017\projects\datamigration\zeiss.mccneo.datamigration.utilities\test.cs:line 19 @ datamigration.controllers.valuescontroller.get() in c:\users\inpyadav\documents\visual studio 2017\projects\datamigration\datamigration\controllers\valuescontroller.cs:line 18 @ lambda_method(closure , object , object[] ) @ microsoft.aspnetcore.mvc.internal.controlleractioninvoker.<invokeactionmethodasync>d__27.movenext()} system.invalidoperationexception
why don't open connection says in error message?
conn.open();
Comments
Post a Comment