Ask Your Question
3

How to integrate a value from an oracle database into an MVC Bootstrap project using .NET 5 and C#?

asked 2022-05-21 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-02-27 12:00:00 +0000

qstack gravatar image

Here are the steps to integrate a value from an Oracle database into an MVC Bootstrap project using .NET 5 and C#:

  1. Create an Oracle connection string in your web.config or appsettings.json file. Here's an example:
<connectionStrings>
  <add name="OracleConnection" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=XE))); User Id=your_user_id; Password=your_password;" providerName="Oracle.ManagedDataAccess.Client" />
</connectionStrings>
  1. Install the Oracle.ManagedDataAccess NuGet package in your project.

  2. In your controller, create an instance of the OracleConnection class and open the connection using the connection string.

using Oracle.ManagedDataAccess.Client;

...

var connectionString = ConfigurationManager.ConnectionStrings["OracleConnection"].ConnectionString;

using (var connection = new OracleConnection(connectionString))
{
    connection.Open();
}
  1. Prepare an OracleCommand object with your SQL query and execute the command to retrieve the value from the database.
var command = connection.CreateCommand();
command.CommandText = "SELECT value FROM your_table WHERE id = :id";
command.Parameters.Add("id", OracleDbType.Int32).Value = id;

var value = command.ExecuteScalar();
  1. Pass the value to your view or return it as JSON to an AJAX call.
return Json(new { value }, JsonRequestBehavior.AllowGet);
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-05-21 11:00:00 +0000

Seen: 11 times

Last updated: Feb 27 '22