如何在C#中从PostgreSQL获取所选行的值?

如何在C#中从PostgreSQL获取所选行的值?,c#,sql,postgresql,npgsql,C#,Sql,Postgresql,Npgsql,我正在使用带有C#和Npgsql库的PostgreSQL数据库 现在我可以选择表中的最后一行,但我不知道如何给它分配一个C变量。我知道我的选择是有效的,因为我已经成功地编辑了我之前的最后一个条目 你可以在下面找到我的代码。注意,我没有粘贴其余的方法,因为我认为它们是不相关的 public void myMethod() { this.OpenConn(); //opens the connection string sql = "SELECT id FROM informati

我正在使用带有C#和Npgsql库的PostgreSQL数据库

现在我可以选择表中的最后一行,但我不知道如何给它分配一个C变量。我知道我的选择是有效的,因为我已经成功地编辑了我之前的最后一个条目

你可以在下面找到我的代码。注意,我没有粘贴其余的方法,因为我认为它们是不相关的

public void myMethod()
{
    this.OpenConn(); //opens the connection

    string sql = "SELECT id FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'customers' ORDER BY id DESC, LIMIT 1";

    using (NpgsqlCommand command = new NpgsqlCommand(sql, conn))
    {
        int id = 0; //instead of '0' I want it to be equal to the ID value from the row
        //something like "int id = sqlSelection.id;" -- this obviously doesn't work

        this.CloseConn(); //close the current connection
    }
}

您可以通过使用特定的DataReader来实现此目标:

有用的注释

  • 在某些情况下,ExecuteScalar是一个很好的选择

您可以通过使用特定的数据读取器来实现此目标:

有用的注释

  • 在某些情况下,ExecuteScalar是一个很好的选择

    • 使用也可以使用以下代码变体

       using (var command = new NpgsqlCommand(sql, conn))
       {
              int id = 0; 
              var reader = command.ExecuteReader();
              while(reader.Read())
              { 
                 var id = Int32.Parse(reader["id"].ToString());
              }
              this.CloseConn(); 
       }
      

      使用也可以使用以下代码变体

       using (var command = new NpgsqlCommand(sql, conn))
       {
              int id = 0; 
              var reader = command.ExecuteReader();
              while(reader.Read())
              { 
                 var id = Int32.Parse(reader["id"].ToString());
              }
              this.CloseConn(); 
       }
      

      您可以使用ExecuteScalarSync方法

      public void myMethod()
      {
          this.OpenConn(); //opens the connection
      
          string sql = "SELECT id FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'customers' ORDER BY id DESC, LIMIT 1";
      
          using (NpgsqlCommand command = new NpgsqlCommand(sql, conn))
           {
             int id= (int)DBHelperRepository.ExecuteScalarSync(sqlString, CommandType.Text);
      
             this.CloseConn(); //close the current connection
           }
      }
      

      您可以使用ExecuteScalarSync方法

      public void myMethod()
      {
          this.OpenConn(); //opens the connection
      
          string sql = "SELECT id FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'customers' ORDER BY id DESC, LIMIT 1";
      
          using (NpgsqlCommand command = new NpgsqlCommand(sql, conn))
           {
             int id= (int)DBHelperRepository.ExecuteScalarSync(sqlString, CommandType.Text);
      
             this.CloseConn(); //close the current connection
           }
      }
      

      使用
      int-id=sqlSelection.id时是否出现错误?必须执行该命令才能执行提供的操作使用
      int id=sqlSelection.id时是否出现错误?为了执行提供的操作,必须执行该命令一个小旁注,而不是使用parse,tostring您只需调用该方法。GetInt(columnindex)一个小旁注,而不是使用parse,tostring您只需调用该方法。GetInt(columnindex)