創建一個Web API,通過該API傳遞id數組並返回OracleDB的結果。在ASP .NET Web API中傳遞參數數組
public class SampleController : ApiController
{
public string Getdetails([FromUri] int []id)
{
string inconditons = "";
for (int i = 0; i < id.Length; i++)
{
if (i == id.Length - 1)
{
inconditons = inconditons + id[i];
}
else
{
inconditons = inconditons + id[i] + ", ";
}
}
using (var dbConn = new OracleConnection("DATA SOURCE=X;PASSWORD=03JD;PERSIST SECURITY INFO=True;USER ID=IN"))
{
dbConn.Open();
var strQuery = @"Select PRIO_CATEGORY_ID as PRIO,LANG_ID as LANG, REC_DATE as REC, REC_USER as RECUSER, DESCR,COL_DESCR AS COL,ROW_DESCR as DROW,ABBR from STCD_PRIO_CATEGORY_DESCR where REC_USER IN (" + inconditons + ");";
var queryResult = dbConn.Query<SamModel>(strQuery);
return JsonConvert.SerializeObject(queryResult);
}
}
}
而稱爲http://localhost:35432/api/Sample?id=1&id=83它引發錯誤的API上VAR QueryResult中= dbConn.Query(strQuery)話說; 但是,如果我只給一個參數如下它的工作原理
var strQuery = @"Select PRIO_CATEGORY_ID as PRIO,LANG_ID as LANG, REC_DATE as REC, REC_USER as RECUSER, DESCR,COL_DESCR AS COL,ROW_DESCR as DROW,ABBR from STCD_PRIO_CATEGORY_DESCR where REC_USER =" +id ;
任何人都可以請建議我什麼是這裏的問題作爲一個參數的作品。謝謝
按下'View Details'並展開'Internal Exception'屬性以獲得更準確的信息。 –
它說{「ORA-00911:invalid character」} – trx
不要使用連接字符串來執行您的查詢。使用[參數化查詢](https://blogs.msdn.microsoft.com/sqlphp/2008/09/30/how-and-why-to-use-parameterized-queries/),否則你很容易出錯這和SQL注入攻擊。 – mason