Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
OracleConnection is a class provided by the Oracle Data Provider for .NET (ODP.NET) that allows developers to connect and interact with Oracle databases using the .NET framework. This is particularly important for Windows-based applications that require robust and efficient database connectivity. In this article, we will discuss how to create and manage OracleConnection in a Windows environment, covering the necessary installations, configurations, and practical examples.
To use OracleConnection in a Windows environment, you need to have the following prerequisites:
Examples:
Installing Oracle Data Provider for .NET (ODP.NET):
Creating a Simple OracleConnection in C#:
using System;
using Oracle.ManagedDataAccess.Client;
class Program
{
static void Main()
{
string connectionString = "User Id=myUsername;Password=myPassword;Data Source=myDataSource";
using (OracleConnection connection = new OracleConnection(connectionString))
{
try
{
connection.Open();
Console.WriteLine("Connection successful!");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
myUsername
, myPassword
, and myDataSource
with your actual Oracle database credentials and data source.Executing a Query via OracleConnection:
using System;
using Oracle.ManagedDataAccess.Client;
class Program
{
static void Main()
{
string connectionString = "User Id=myUsername;Password=myPassword;Data Source=myDataSource";
using (OracleConnection connection = new OracleConnection(connectionString))
{
try
{
connection.Open();
string query = "SELECT * FROM myTable";
OracleCommand command = new OracleCommand(query, connection);
OracleDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["ColumnName"]);
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
myUsername
, myPassword
, myDataSource
, and myTable
with your actual Oracle database credentials, data source, and table name.Running the C# Program via Command Prompt:
csc /reference:Oracle.ManagedDataAccess.dll Program.cs
Program.exe