string connectionString = "Data Source=datasource;User Id=username;Password=password;"; string sql = "SELECT COUNT(*) FROM employees WHERE department='Marketing'"; using (OracleConnection connection = new OracleConnection(connectionString)) { connection.Open(); OracleCommand command = new OracleCommand(sql, connection); int count = Convert.ToInt32(command.ExecuteScalar()); Console.WriteLine($"There are {count} employees in the Marketing department."); }
string connectionString = "Data Source=datasource;User Id=username;Password=password;"; string sql = "SELECT firstname, lastname FROM employees WHERE employee_id=:id"; using (OracleConnection connection = new OracleConnection(connectionString)) { connection.Open(); OracleCommand command = new OracleCommand(sql, connection); command.Parameters.Add("id", OracleDbType.Int32).Value = 123; OracleDataReader reader = command.ExecuteReader(); if (reader.Read()) { string firstName = reader.GetString(0); string lastName = reader.GetString(1); Console.WriteLine($"Employee name: {firstName} {lastName}"); } }In this example, we connect to an Oracle database using a connection string and select the firstname and lastname of an employee specified by their employee_id. We set the value for the employee_id parameter using the Parameters collection. We execute the SQL statement using the ExecuteReader method and read the results using a data reader. We retrieve the firstname and lastname values from the data reader and print out the employee name. Package library: Oracle.ManagedDataAccess