Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
C# 使用NpgSql和Postgres 9.4从数据库读取记录_C#_Postgresql_Npgsql - Fatal编程技术网

C# 使用NpgSql和Postgres 9.4从数据库读取记录

C# 使用NpgSql和Postgres 9.4从数据库读取记录,c#,postgresql,npgsql,C#,Postgresql,Npgsql,我正在为我的.Net应用程序使用Postgres 9.4和NpgSql 2.2.5。Postgres对我来说是新的,当我尝试在数据库中执行Postgres函数时,它会给我带来一个错误 这是我的C代码 public List<District> DistrictReadAll() { try { List<District> districts = new List<District>

我正在为我的.Net应用程序使用Postgres 9.4和NpgSql 2.2.5。Postgres对我来说是新的,当我尝试在数据库中执行Postgres函数时,它会给我带来一个错误

这是我的C代码

public List<District> DistrictReadAll()
      {
          try
          {
              List<District> districts = new List<District>();
              using (var conn = new NpgsqlConnection(this.RealEsateDB))
              {
                  conn.Open();
                  using (var cmd = conn.CreateCommand())
                  {
                      cmd.CommandText = "district_read_all";
                      cmd.CommandType = System.Data.CommandType.StoredProcedure;                      
                      NpgsqlDataReader rdr = cmd.ExecuteReader();
                      while (rdr.Read())
                      {
                          District objDist = new District();
                          objDist.DistrictId = (int)rdr["districtid"];
                          objDist.Name = rdr["name"].ToString();
                          districts.Add(objDist);
                      }
                      return districts;
                  }
              }
          }
          catch ( Exception ex)
          {

              throw;
          }
      }
public.district表定义

CREATE TABLE district
(
  districtid serial NOT NULL,
  name character varying(250) NOT NULL,
  activeflag boolean DEFAULT true,
  CONSTRAINT pk_district_districtid PRIMARY KEY (districtid)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE district
  OWNER TO postgres;
和C错误堆栈跟踪

Npgsql.NpgsqlException was caught
  HResult=-2147467259
  Message=ERROR: 34000: cursor "<unnamed portal 1>" does not exist
  Source=Npgsql
  ErrorCode=-2147467259
  BaseMessage=cursor "<unnamed portal 1>" does not exist
  Code=34000
  ColumnName=""
  ConstraintName=""
  DataTypeName=""
  Detail=""
  ErrorSql=SELECT * FROM district_read_all()
  File=src\backend\commands\portalcmds.c
  Hint=""
  Line=168
  Position=""
  Routine=PerformPortalFetch
  SchemaName=""
  Severity=ERROR
  TableName=""
  Where=""
  StackTrace:
       at Npgsql.NpgsqlState.<ProcessBackendResponses>d__0.MoveNext()
       at Npgsql.ForwardsOnlyDataReader.GetNextResponseObject(Boolean cleanup)
       at Npgsql.ForwardsOnlyDataReader.GetNextRowDescription()
       at Npgsql.ForwardsOnlyDataReader.NextResultInternal()
       at Npgsql.ForwardsOnlyDataReader..ctor(IEnumerable`1 dataEnumeration, CommandBehavior behavior, NpgsqlCommand command, NotificationThreadBlock threadBlock, Boolean preparedStatement, NpgsqlRowDescription rowDescription)
       at Npgsql.NpgsqlCommand.GetReader(CommandBehavior cb)
       at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior cb)
       at Npgsql.NpgsqlCommand.ExecuteReader()
       at CarRental.DataAccess.DistrictAccess.DistrictReadAll() in c:\DotNetApplications\CarRentalGit\RJ.CarRental\CarRental.DataAccess\DistrictAccess.cs:line 58
  InnerException: 
如果我使用Sql Server,这是一件非常简单的事情


有人能告诉我我做错了什么吗?

您必须在NpgsqlTransaction中包装并执行查询,查询才能正常工作-请参阅手册的一节

using (var conn = new NpgsqlConnection(this.RealEsateDB))
{
    conn.Open();

    using (NpgsqlTransaction t = conn.BeginTransaction()) 
    {
        using (var cmd = conn.CreateCommand())
        {
            ...
        }

        t.Commit();
    }
}
using (var conn = new NpgsqlConnection(this.RealEsateDB))
{
    conn.Open();

    using (NpgsqlTransaction t = conn.BeginTransaction()) 
    {
        using (var cmd = conn.CreateCommand())
        {
            ...
        }

        t.Commit();
    }
}