REST 调用从 Oracle 数据库返回空列表 [英] REST Calls return empty List From Oracle Database

查看:71
本文介绍了REST 调用从 Oracle 数据库返回空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库 (Oracle 11G) 返回列表现在当我直接从 Sql Explorer 运行它时,它返回数据.现在将它放在 REST API 中而不是返回它,它只返回 [] 作为结果

I am trying to return List from a Database (Oracle 11G) Now When I run it directly from Sql Explorer, it returns the Data. Now Having it inside the REST API rather than return it, it just returns [] as results

我的代码看起来像这样:

My code Looks something Like this :

public IEnumerable<VaultService> Get(string Branch_Desg)
{
    OracleConnection con = new OracleConnection(constr);
    con.Open();
    DataTable dt = new DataTable();
    //string sql = "select  from wemadummyvaulttable where branch_desg = '" + Branch_Desg + "'";
    string sql = "select BRANCH_ID ,BRANCH_NAME ,BRANCHID_NUMBER ,BRANCH_ACCOUNTNO ,BRANCH_DESG ,CURRENCY ,BRANCH_BALANCE from wemadummyvaulttable where branch_desg = '"+Branch_Desg+"' ";
    OracleDataAdapter da = new OracleDataAdapter(sql, con);
    List<VaultService> vr = new List<Models.VaultService>(dt.Rows.Count);
    if (dt.Rows.Count > 0)
    {
        foreach (DataRow vaultrecord in dt.Rows)
        {
            vr.Add(new ReadVaultBal(vaultrecord));
        }
    }
    return vr;
}

稍后我将修复 Sql 注入的部分,我只需要知道我在这里缺少什么以及为什么它返回 Emptyset 而不是 Json 结果

I will fix the part for the Sql injections later, i just need to know what I am Missing here and why its returning an Emptyset rather than Json results

完整代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WEMAVaultREST.Models;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;
using System.Data;

namespace JBVaultREST.Controllers
{
    public class VaultServiceController : ApiController
    {
        string constr = "User ID=*; Password=*; Data Source=SAM;";
        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // GET api/<controller>/5
        /* public VaultService Get(string Branch_Desg)
         {
             OracleConnection con = new OracleConnection(constr);
             con.Open();
             DataTable dt = new DataTable();

             string sql = "select * from wemadummyvaulttable where branch_desg = '"+Branch_Desg+"'";
             OracleDataAdapter da = new OracleDataAdapter(sql,con);
             da.Fill(dt);
             if (dt.Rows.Count > 0)
             {
                 return new ReadVaultBal(dt.Rows[0]);
             }
             throw new Exception("Account not found");
         }*/

        public IEnumerable<VaultService> Get(string Branch_Desg)
        {
            OracleConnection con = new OracleConnection(constr);
            con.Open();
            DataTable dt = new DataTable();
            //string sql = "select  from wemadummyvaulttable where branch_desg = '" + Branch_Desg + "'";
            string sql = "select BRANCH_ID ,BRANCH_NAME ,BRANCHID_NUMBER ,BRANCH_ACCOUNTNO ,BRANCH_DESG ,CURRENCY ,BRANCH_BALANCE from wemadummyvaulttable where branch_desg = '"+Branch_Desg+"' ";
            OracleDataAdapter da = new OracleDataAdapter(sql, con);
            da.Fill(dt);
            List<VaultService> vr = new List<Models.VaultService>(dt.Rows.Count);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow vaultrecord in dt.Rows)
                {
                    vr.Add(new ReadVaultBal(vaultrecord));
                }
            }
            return vr;
        }


        // POST api/<controller>
        public void Post([FromBody]string value)
        {
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
}

类看起来像这样

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using WEMAVaultREST.Models;

namespace JBVaultREST.Models
{
    public class VaultService
    {
        public string Branch_name { get; set; }
        public string BranchID_Number { get; set; }
        public string Branch_AccountNo { get; set; }
        public string Branch_Desg { get; set; }
        public string Currency { get; set; }
        public decimal Branch_Balance { get; set; }
    }
}

public class CreateTransaction : VaultService
{
}

public class ReadVaultBal : VaultService
{
    public ReadVaultBal(DataRow row)
    {
        Branch_name = row["Branch_name"].ToString();
        BranchID_Number = row["BranchID_Number"].ToString();
        Branch_AccountNo = row["Branch_AccountNo"].ToString();
        Branch_Desg = row["Branch_Desg"].ToString();
        Currency = row["Currency"].ToString();
        Branch_Balance = Convert.ToDecimal(row["Branch_Balance"]);
    }

    public string Branch_name { get; set; }
    public string BranchID_Number { get; set; }
    public string Branch_AccountNo { get; set; }
    public string Branch_Desg { get; set; }
    public string Currency { get; set; }
    public decimal Branch_Balance { get; set; }
}

推荐答案

您的代码中缺少 da.Fill(dt);.

另外,请像这样从查询中删除 ''.

Also please remove the '' from the query like this.

string sql = "select BRANCH_ID ,BRANCH_NAME ,BRANCHID_NUMBER ,BRANCH_ACCOUNTNO ,BRANCH_DESG ,CURRENCY ,BRANCH_BALANCE from wemadummyvaulttable where branch_desg = " + Branch_Desg + " ";

这篇关于REST 调用从 Oracle 数据库返回空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆