我正在创建一个Web API,并尝试在web.config文件中创建连接字符串
<connectionStrings>
<add name="ProConnection" connectionString="Data Source=J;User ID=T;Password=C;pooling=true;min pool size=5;Max Pool Size=60" providerName="Oracle.DataAccess.Client"/>
<connectionStrings>
所以我可以使用aspnet_regiis.exe加密连接字符串。所以在控制器中我使用下面的代码,其中dbconn保存连接细节
public HttpResponseMessage Getdetails([FromUri] string[] id)
{
string dbconn = ConfigurationManager.ConnectionStrings["ProConnection"].ConnectionString;
{
var inconditions = id.Distinct().ToArray();
var srtcon = string.Join(",", inconditions);
DataSet userDataset = new DataSet();
var strQuery = @"SELECT * from STCD_PRIO_CATEGORY_DESCR where STCD_PRIO_CATEGORY_DESCR.STD_REF IN (" + srtcon + ")";
OracleCommand selectCommand = new OracleCommand(strQuery, dbconn);
OracleDataAdapter adapter = new OracleDataAdapter(selectCommand);
DataTable selectResults = new DataTable();
adapter.Fill(selectResults);
string result = JsonConvert.SerializeObject(selectResults);
string contentDisposition = "inline; filename=abc.json";
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, result, MediaTypeHeaderValue.Parse("application/json"));
response.Content.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse(contentDisposition);
return response;
}
}
答案 0 :(得分:1)
您试图直接在OracleCommand
中使用您的连接字符串,而不是使用它创建OracleConnection
并将其传递给您的OracleCommand
:
public HttpResponseMessage Getdetails([FromUri] string[] id)
{
var connectionStringSection = ConfigurationManager.ConnectionStrings["ProConnection"];
string connectionString = connectionStringSection.ConnectionString;
using (OracleConnection dbConn = new OracleConnection(connectionString))
{
var inconditions = id.Distinct().ToArray();
var srtcon = string.Join(",", inconditions);
DataSet userDataset = new DataSet();
var strQuery = @"SELECT * from STCD_PRIO_CATEGORY_DESCR where STCD_PRIO_CATEGORY_DESCR.STD_REF IN (" + srtcon + ")";
using (OracleCommand selectCommand = new OracleCommand(strQuery, dbConn))
{
using (OracleDataAdapter adapter = new OracleDataAdapter(selectCommand))
{
DataTable selectResults = new DataTable();
adapter.Fill(selectResults);
string result = JsonConvert.SerializeObject(selectResults);
string contentDisposition = "inline; filename=abc.json";
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, result, MediaTypeHeaderValue.Parse("application/json"));
response.Content.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse(contentDisposition);
return response;
}
}
}
}
值得注意的是,您应该在using
周围使用OracleConnection/Command/DataAdapters
语句,否则可能会造成内存泄漏。
答案 1 :(得分:0)
尝试将其设为字符串:
var strQuery = @"SELECT * from STCD_PRIO_CATEGORY_DESCR where STCD_PRIO_CATEGORY_DESCR.STD_REF IN (" + srtcon + ")";
OracleCommand selectCommand = new OracleCommand(strQuery, dbconn);
为:
String strQuery = @"SELECT * from STCD_PRIO_CATEGORY_DESCR where STCD_PRIO_CATEGORY_DESCR.STD_REF IN (" + srtcon + ")";
OracleCommand selectCommand = new OracleCommand(strQuery, dbconn);
也试试这个:
dbconn.Open();
OracleCommand selectCommand = new OracleCommand ();
selectCommand = dbconn.CreateCommand();
selectCommand = new OracleCommand (strQuery, dbconn);