ASP.NetWeb API服务连接到oracle数据库
本文关键字:oracle 数据库 连接 服务 NetWeb API ASP | 更新日期: 2023-09-27 17:57:58
我是AP.Net MVC服务的新手,正在尝试创建一个web服务,在那里我们提供一个输入数组,该数组将连接到oracle数据库并返回查询(过滤器中使用的输入需求数组)结果。我尝试不使用实体数据模型,并在model类中给出POCO类。因此,我尝试创建一个简单的控制器,从数据库中选择整个数据
public class TestOraConnController : ApiController
{
public string Get(int id)
{
var strQuery = @"Select PRIO_CATEGORY_ID as PRIO,LANG_ID as LANG, REC_DATE as REC, REC_USER as RECUSER, RFCH_ID as RFCH,DESCR,COL_DESCR AS COL,ROW_DESCR as DROW,ABBR from STCD_PRIO_CATEGORY_DESCR where REC_USER =" +id;
OracleConnection dbConn = new OracleConnection("DATA SOURCE=ABC;PASSWORD=ABCD;PERSIST SECURITY INFO=True;USER ID=ABC");
dbConn.Open();
OracleCommand selectCommand = new OracleCommand(strQuery, dbConn);
OracleDataAdapter adapter = new OracleDataAdapter(selectCommand);
DataTable selectResults = new DataTable();
adapter.Fill(selectResults);
dbConn.Close();
return JsonConvert.SerializeObject(selectResults);
}
所以结果看起来像
"[{'"PRIO'":1,'"LANG'":1,'"REC'":'"2011-10-23T20:32:18'",'"RECUSER'":1,'"RFCH'":null,'"DESCR'":'"Internal Study'",'"COL'":'"Internal Study'",'"DROW'":'"Internal Study'",'"ABBR'":'"Intern'"},{'"PRIO'":1,'"LANG'":2,'"REC'":'"2011-10-23T20:32:18'",'"RECUSER'":1,'"RFCH'":null,'"DESCR'":'"Internal Study'",'"COL'":'"Internal Study'",'"DROW'":'"Internal Study'",'"ABBR'":'"Intern'"},{'"PRIO'":2,'"LANG'":1,'"REC'":'"2011-10-23T20:32:31'",'"RECUSER'":1,'"RFCH'":null,'"DESCR'":'"Client Study'",'"COL'":'"Client Study'",'"DROW'":'"Client Study'",'"ABBR'":'"Client'"}]"
现在,如果我需要给出输入参数的数组,我将如何通过参数数组进行交互来选择。我知道我们给
public string get( int []d)
但不确定我们需要如何更改查询。任何hep都将不胜感激。
如果保留"strQuery"变量,则可以使用IN关键字:
SELECT * FROM TABLE WHERE ID IN (id1, id2, ..., idn)
还有绳子。Join方法格式化您的Ids
var ints = new int[] {1, 2, 3, 4, 5};
var ids= string.Join(",", ints.Select(x => x.ToString()).ToArray());// "1,2,3,4,5"
var strQuery = $@"Select PRIO_CATEGORY_ID as PRIO,LANG_ID as LANG, REC_DATE as REC, REC_USER as RECUSER, RFCH_ID as RFCH,DESCR,COL_DESCR AS COL,ROW_DESCR as DROW,ABBR from STCD_PRIO_CATEGORY_DESCR where REC_USER IN({ids})";