在asp.net webapi中使用Db中的值检索多个对象

时间:2017-06-09 06:22:22

标签: c# sql npgsql

我正在学习asp.net webapi并尝试获取不同对象的值。我有一个名为

的函数
  

SELECT * FROM test.get_accounts_info('CS-01')

返回5列。

  • account_number [value-CS-001]
  • account_name [Sam]
  • 产品[测试]
  • interest_rate [1]
  • 余额[1000]

    现在在我的控制器中,我传递了一个AccountInfo类的对象。

    [HttpPost]
    [ActionName("info")]
    public IHttpActionResult GetAccountInfo([FromBody]AccountInfo 
         accountinfo)
    {
        accountinfo.accountNumber = BusinessLayer.Api.AccountHolderApi.GetAccountInfo(accountinfo.accountNumber);
        return Ok(accountinfo);
    }
    

AccountInfo类

    public class AccountInfo
      {
        public string accountNumber { get; set; }
        public string balance { get; set; }
        public int interestRate { get; set; }
        public string accountName { get; set; }
        public string accountType { get; set; }
      }

我的路线

  

config.Routes.MapHttpRoute(“test”,“v1 / {controller} / {action}”);

我调用函数的Db Layer代码

      ` public static string 
        GetAccountInfo(string accountNumber)
        {
            var sql = "SELECT * FROM 
            test.get_accounts_info(@AccountNumber);";
            using (var command = new NpgsqlCommand(sql))
            {
               command.Parameters.AddWithValue("@AccountNumber", 
               accountNumber);
               return Common.Conversion.TryCastString(DBOperations.GetScalarValue(command));
            }
        }`

现在我是邮递员,当我从身体传递价值时

  

{“accountNumber”:“CS-001”}

在我的回复中我得到了这个

  

{     “accountNumber”:“CS-0000001”,     “balance”:null,     “interestRate”:0,     “accountName”:null,     “accountType”:null   }

我哪里错了?请帮助。

已更新

我的DBOperation.cs有这些类

  public static NpgsqlDataReader GetDataReader(NpgsqlCommand command)
    {
        if (command != null)
        {
            if (ValidateCommand(command))
            {
                command.Connection = Connection;
                var reader = command.ExecuteReader();
                command.Connection.Close();
                command.Connection.Dispose();
                return reader;

            }
        }

        return null;
    }



  public static object GetScalarValue(NpgsqlCommand command)
    {
        if (command != null)
        {

            if (ValidateCommand(command))
            {
                command.Connection = Connection;
                var val = command.ExecuteScalar();
                command.Connection.Close();
                command.Connection.Dispose();
                return val;

            }
        }

        return null;
    }





  public static bool ExecuteNonQuery(NpgsqlCommand command)
    {
        if (command != null)
        {
            if (ValidateCommand(command))
            {
                command.Connection = Connection;
                command.ExecuteNonQuery();
                command.Connection.Close();
                command.Connection.Dispose();
                return true;
            }
        }

        return false;
    }

1 个答案:

答案 0 :(得分:1)

从以下位置看这个例子: http://www.sqlines.com/postgresql/npgsql_cs_result_sets

   using System;
   using Npgsql;

   class Sample
   {
     static void Main(string[] args)
     {
         // Connect to a PostgreSQL database
         NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=postgres; " + 
             "Password=pwd;Database=postgres;");
         conn.Open();

         // Define a query
         NpgsqlCommand command = new NpgsqlCommand("SELECT city, state FROM cities", conn);

         // Execute the query and obtain a result set
         NpgsqlDataReader dr = command.ExecuteReader();

         // Output rows
         while (dr.Read())
           Console.Write("{0}\t{1} \n", dr[0], dr[1]);

         conn.Close();
     }
   }

保留您已有的使用和内容。只需将其更新为command.ExecuteReader,因为此方法读取所有行和列。 Execute Scalar只返回第一行的第一列