Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
OracleCommand is a class provided by the Oracle Data Provider for .NET (ODP.NET), which is used to execute SQL commands and stored procedures against an Oracle database. This is particularly important for developers and database administrators who work with Oracle databases in a .NET environment on Windows. The OracleCommand class is part of the Oracle.ManagedDataAccess.Client namespace and provides a way to interact with Oracle databases using .NET languages like C#.
In this article, we will explore how to create and use OracleCommand in a Windows environment. We will cover the installation of the necessary libraries, setting up a connection to an Oracle database, and executing SQL commands. This guide assumes that you have some basic knowledge of C# and .NET development.
Examples:
Installing ODP.NET: First, you need to install the Oracle Data Provider for .NET. You can do this via NuGet Package Manager in Visual Studio.
Install-Package Oracle.ManagedDataAccess
Setting Up a Connection: To use OracleCommand, you first need to set up a connection to your Oracle database. Below is a sample code to establish a connection.
using Oracle.ManagedDataAccess.Client;
string connectionString = "User Id=<username>;Password=<password>;Data Source=<datasource>";
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
Console.WriteLine("Connected to Oracle Database");
}
Executing a SQL Command: Once the connection is established, you can use OracleCommand to execute SQL commands. Here’s an example of how to execute a simple SQL SELECT command.
using Oracle.ManagedDataAccess.Client;
string connectionString = "User Id=<username>;Password=<password>;Data Source=<datasource>";
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
string sql = "SELECT * FROM Employees";
using (OracleCommand command = new OracleCommand(sql, connection))
{
using (OracleDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine("Employee ID: " + reader["EmployeeID"]);
Console.WriteLine("Employee Name: " + reader["EmployeeName"]);
}
}
}
}
Executing a Stored Procedure: OracleCommand can also be used to execute stored procedures. Below is an example of how to call a stored procedure named "GetEmployeeDetails".
using Oracle.ManagedDataAccess.Client;
string connectionString = "User Id=<username>;Password=<password>;Data Source=<datasource>";
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
using (OracleCommand command = new OracleCommand("GetEmployeeDetails", connection))
{
command.CommandType = System.Data.CommandType.StoredProcedure;
// Adding parameters
command.Parameters.Add("p_EmployeeID", OracleDbType.Int32).Value = 123;
command.Parameters.Add("p_EmployeeName", OracleDbType.Varchar2, 50).Direction = System.Data.ParameterDirection.Output;
command.ExecuteNonQuery();
// Retrieving output parameter
string employeeName = command.Parameters["p_EmployeeName"].Value.ToString();
Console.WriteLine("Employee Name: " + employeeName);
}
}