Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用npgsql从c#查询postgres_C#_Postgresql - Fatal编程技术网

使用npgsql从c#查询postgres

使用npgsql从c#查询postgres,c#,postgresql,C#,Postgresql,我已经在我的windows机器上安装了postgres,并开始使用POC。我可以从windows命令行连接到数据库。但是,我无法从C#应用程序连接 似乎我在连接字符串中遇到了一些问题。我已经看过多个教程,但是每个教程都有自己的方法来提供连接字符串参数。有没有标准的方法来提供主机名、端口、用户名、密码和数据库 我正在尝试使用RESTAPI进行查询。是的,我做得对 // GET api/values [HttpGet] public IActionResult Get() { Test tes

我已经在我的windows机器上安装了postgres,并开始使用POC。我可以从windows命令行连接到数据库。但是,我无法从C#应用程序连接

似乎我在连接字符串中遇到了一些问题。我已经看过多个教程,但是每个教程都有自己的方法来提供连接字符串参数。有没有标准的方法来提供主机名、端口、用户名、密码和数据库

我正在尝试使用RESTAPI进行查询。是的,我做得对

// GET 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 this
     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 * from test", 
                                        conn);
         NpgsqlDataReader dr = command.ExecuteReader();
         while (dr.Read())
         {
         value = dr[0].ToString();
         }
         return value;
         }
    }
 }
例外情况:

-       $exception  {System.InvalidOperationException: Connection is not open
   at Npgsql.NpgsqlConnection.CheckReadyAndGetConnector()
   at Npgsql.NpgsqlCommand.<ExecuteDbDataReader>d__92.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at Npgsql.NpgsqlCommand.ExecuteReader()
   at Zeiss.MCCNeo.DataMigration.Utilities.Test.Table2Json() in c:\users\inpyadav\documents\visual studio 2017\Projects\DataMigration\Zeiss.MCCNeo.DataMigration.Utilities\Test.cs:line 19
   at DataMigration.Controllers.ValuesController.Get() in c:\users\inpyadav\documents\visual studio 2017\Projects\DataMigration\DataMigration\Controllers\ValuesController.cs:line 18
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__27.MoveNext()}  System.InvalidOperationException
-$exception{System.invalidoOperationException:连接未打开
在Npgsql.NpgsqlConnection.CheckReadyAndGetConnector()中
在Npgsql.NpgsqlCommand.d__92.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()中
在System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()中
在Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior)处
在Npgsql.NpgsqlCommand.ExecuteReader()处
在c:\users\inpyadav\documents\visualstudio 2017\Projects\DataMigration\Zeiss.MCCNeo.DataMigration.Utilities.Test.Table2Json()中
在c:\users\inpyadav\documents\visual studio 2017\Projects\DataMigration\DataMigration\Controllers\ValuesController.cs中的DataMigration.Controllers.ValuesController.Get()处:第18行
在lambda_方法(闭包、对象、对象[])
位于Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d_u27.MoveNext()}System.InvalidOperationException

为什么不按照错误消息中的说明打开连接


conn.Open()

谢谢,我错过了。