jueves, 26 de julio de 2018

PDF documents about .NET Patterns & practices.

In 2007, Microsoft published a series of documents about .NET architecture. Although the documents reference to old technologies, such as Enterprise Library, Web Services Enhancements, etc. Some themes have not lost their validity. Remember that the architectural principles are agnostic.

  1. Application Architecture for .NET: Designing Applications and Services
  2. Designing Data Tier Components and Passing Data Through Tiers
  3. Caching Architecture Guide for .NET Framework Applications
  4. .NET Data Access Architecture Guide
  5. Deploying .NET Applications Lifecycle Guide
  6. Designing Application-Managed Authorization
  7. Data Patterns
  8. Design and Implementation Guidelines for Web Clients
  9. Guidelines for Application Integration
  10. Exception Management in .NET
  11. Describing the Enterprise Architectural Space
  12. Enterprise Solution Patterns Using Microsoft .NET
  13. Integration Patterns
  14. Web Service Security
  15. Application Interoperability: Microsoft .NET and J2EE
  16. Smart Client Architecture and Design Guide
  17. Improving .NET Application Performance and Scalability
  18. Building Secure ASP.NET Applications
  19. Security Engineering Explained
  20. Team Development with Visual Studio® .NET and Visual SourceSafe
  21. Improving Web Application Security: Threats and Countermeasures

Documentos acerca de Microsoft patterns & practices

En 2007, Microsoft publicó una serie de documentos acerca de arquitectura y buenas prácticas. Aunque los documentos se refieren a tecnologías ya descontinuadas, algunos temas no han perdido vigencia.

  1. Application Architecture for .NET:Designing Applications and Services
  2. Designing Data Tier Components and Passing Data Through Tiers
  3. Caching Architecture Guide for .NET Framework Applications
  4. .NET Data Access Architecture Guide
  5. Deploying .NET Applications Lifecycle Guide
  6. Designing Application-Managed Authorization
  7. Data Patterns
  8. Design and Implementation Guidelines for Web Clients
  9. Guidelines for Application Integration
  10. Exception Management in .NET
  11. Describing the Enterprise Architectural Space
  12. Enterprise Solution Patterns Using Microsoft .NET
  13. Integration Patterns
  14. Web Service Security
  15. Application Interoperability: Microsoft .NET and J2EE
  16. Smart Client Architecture and Design Guide
  17. Improving .NET Application Performance and Scalability
  18. Building Secure ASP.NET Applications
  19. Security Engineering Explained
  20. Team Development with Visual Studio® .NET and Visual SourceSafe
  21. Improving Web Application Security: Threats and Countermeasures

martes, 24 de julio de 2018

Oracle Recipe #3: How to Execute a query that returns a Single Row with the method GetOracleValues.

if you want to obtain multiple values from a database, you can call the executeReader method once on a OracleCommand object, to execute a SQL statement that returns a collection of values in a single row result.

The ExecuteReader method returns an instance of a class that implements the IDataReader interface. Each of the data reader classes provided by .NET Framework has a GetValues method, which returns an array of column values for the current row.

To obtain a single row from a database.

  1. Open a OracleConnection.
  2. Create and initialize a OracleCommand object.
  3. Call the ExecuteReader method on the command object. Assign the return value from this method to a data reader variable.
  4. Call the Read method on the data reader object to move to the first(and only) row in the result set.
  5. Call the GetOracleValues method on the data reader object. Pass an object array as a parameter to retrieve the scalar results of the query.
  6. Convert each element in the array to an appropriate data type, if necessary.
  7. Close the OracleDataReader object.
  8. Dispose the OracleCommand object.
  9. Close the database connection.

The following example shows how to execute a query that returns a set of values. The example place the results into an array named results.

Fig 1. Using the GetOracleValues of an OracleDataReader object.


Fig 2. Testing the program.


Download example source code.

lunes, 9 de julio de 2018

Oracle Recipe #2: How to execute a query that returns a Scalar result with OracleCommand.

  1. Microsoft ADO.NET command objects have an ExecuteScalar method, which enables you to execute a query that returns a single result.
  2. Open a database connection.
  3. Create and initialize a command object.
  4. Call the ExecuteScalar method on the command object.
  5. Convert the return value from ExecuteScalar into an appropriate data type.
  6. Dispose the command object.
  7. Close the database connection.

The following example, show how to execute a query that determines the average salary from the table employees on the HR schema provided by Oracle Database XE. The example assume that the query does not return a NULL result.

Fig 1. OracleCommand ExecuteScalar method code example.


Download example source code.

jueves, 5 de julio de 2018

Utilizando las clases de ADO.NET en Oracle.

Hace bastante tiempo que hice unas clases de ADO.NET para Oracle (un tipo helper de manera elemental). Quizás no es la manera más optima de acceder a una base de datos, pero al menos en ambientes restringidos en donde existen aplicaciones legadas donde no es posible actualizar otro proveedor de ADO.NET para Oracle que no sea el que viene predeterminado por .NET.

Aquí esta la clase para manejar la conexión, se llama OracleDataBase

Fig 1. Clase para manejar la conexion a la base de datos.


Utilizo otra clase llamada OracleDataBaseCommand para auxiliarme con los comandos.
Fig 2. Clase auxiliar para ejecutar los comandos en la base de datos.


Su utilización dentro de una clase que sirva para persistir o extraer datos sería de la siguiente manera.

Fig 3. Clase que utiliza las clases auxiliares para guardar un objeto.


Fig 4. Clase principal que crea, actualiza y consulta un cliente en la base de datos.


Descargar ejemplo.

viernes, 22 de junio de 2018

C# Recipe 3: How to format a string as currency

You can use the String.Format() method to localize the format of data such as dates,times, numbers, and currencies, according to the culture in the currently executing thread. The following example shows how to display a currency according to the current culture.

Fig 1. Example of how strings are formatted.


Fig 2. Displays a localized currency value.


Download source code.

jueves, 21 de junio de 2018

How to connect an Oracle Data Source by using ADO.NET

Step 1: Provide a connection string

To connect to a database, you must provide a connection string to identify the database. The following list describes several common parameters of connection strings.

  • Persist Security Info: if is false, the data source does not return security-sensitive if the connection is open.
  • User ID and Password: The data source login and password to use if you are not using integrated security.
  • Integrated Security or Trusted_Connection: if true, the data source uses the current account credentials for authentication, if false, you must specify the User ID and Password in the connection string.
  • Data Source: The name or network address of the data source instance.
  • Initial Catalog or Database: the name of the database.
  • Connection Timeout: The length of time in seconds to wait for a connection to the server before the data source terminates the attempt and returns an error. The default timeout is 15 seconds.
Fig 1. Provide a connection string.


Step 2: Retrieve a connection string from an application configuration file.

You can either store connection strings in an application configuration file or you can hard-code them directly in your application. If you store connection strings in the configuration file, you can modify them easily, without having to edit the source code and recompile the application. Each connection string that is stored in an application configuration file has its own assigned name. In an application, you can access a connection string by its programmatic name.

Fig 2. Create and return an OracleConnection object.


Step 3: Handle connection events

ADO.NET connection objects have two events that you can use to retrieve informational messages from a data source or to determine if the state of a connection has changed.

  • InfoMessage: Occurs when a data source returns an informational message. Informational messages are messages from a data source that do not result in an exception being thrown.
  • StateChage: Occurs when a connection changes from the closed state to the open state or from the open state to the closed state.

If an error occurs at the data source, the data provider throws an exception. However, if the data source returns an informational message, the data provider raises an InfoMessage event instead.

Step 4: Handle connection exceptions

The .NET Data provider for Oracle throws an OracleException when it encounters an error or warning generated by an Oracle database. OracleException objects have a Code property that gets the code portion of the error as an integer and message property that gets a description of the error.

Fig 3. Step 3 and 4: Handle connection events and exceptions, you can see the example below.


The following code show how to test each step in C# code.

Fig 4. The code to test our classes.


Fig 5. We run the program and verify the events in the log.


Fig 6. The program wrote the events in the log.


Download source code.