martes, 16 de julio de 2019

How to execute simple Database Queries with VB .NET

The SqlCommand class in the .NET Framework Data Provider has four methods that you can use to execute SQL statements:

  1. ExecuteScalar: Executes a query that returns a single scalar value.
  2. ExecuteReader: Executes a query that returns a result set.
  3. ExecuteNonQuery: Executes a data update statements or a catalog update statement.
  4. ExecuteXmlReader: Executes a query that returns an Extensible Markup Language (XML) result set, this method is only avaliable in the SqlCommand class.

To execute a simple database query

  1. Import the System.Configuration namespace
  2. Use the ConfigurationManager.ConnectionStrings property to get a collection of connection strings from the application configuration file.
  3. Index into the collection of connection strings by using the programmatic name of the connection string you want to access.
  4. Use the ConnectionString property to get the connection string information.
  5. Create a connection object.
  6. Create a command object.
  7. If you want to execute an SQL statement, set the CommandType property of the command object to the CommandType.Text enumeration value. If you want to call a stored procedure, set the CommandType property of the command object to the CommandType.StoredProcedure enumeration value.
  8. Call the Open method on the connection object.
  9. Call the ExecuteScalar method on the command object. Assign the result to a suitably typed variable.
  10. Call the Close method on the connection object.

The following example shows how to execute a query to determine the number of products in the AdventureWorks2016CTP3 database on the local SQL Server instance.

Fig 1. Main program
Fig 2. App config
Fig 3. Output program

jueves, 11 de julio de 2019

Understanding OOP Inheritance with Python

One of the most common goals for the OOP is code reusability. Characteristics such as inheritance contributes to achieving this goal.

Inheritance

Inheritance is the most used mechanism to optimise the coding, since it allows to reuse methods defined in superclasses, to define new subclasses. The following example uses the class Person as its superclass.

Fig 1. Inheritance
Fig 2. Person Class

We know that a person also can be an employee in addition to talking, and Employee can show its earnings so we will declare a class called Employee.

Fig 3. Employee Class

Who inherits the talk() method of the Person class to implement inheritance in this example:

Fig 4. Main program

You will notice how the "John" object, which is now an instance of Employee continues to behave as an instance of Person because it has inherited its methods.

Fig 4. Run the example
$ py Sample1OOP.py