IMAT1604: Visual Web Development

DatabaseQuery Code - Constructors

In C# the name of the Constructor method is the same as the class name, the constructor does not have a return type. DatabaseQuery has two constructors, the constructor without a parameter assumes that the database connection string is called AddressBookDbConnectionString. The other constructor is the one that will be used more often as it creates a connection to any database given the appropriate connection string name.

DatabaseQuery myQuery = new DatabaseQuery("WinesDbConnectionString");

C# DatabaseQuery Class

DatabaseQuery Code - AddParameter

This method needs to be called once for each SQL Parameter in the stored procedure query. The parameters must be added in the correct order. With an INSERT query there will one SQL Parameter for each field in the record, whereas the simplest SELECT query might have no parameters.

C# DatabaseQuery Class

DatabaseQuery Code - ExecuteSelect()

The code to execute SELECT stored procedures is simpler than the code needed for update queries. The database connection is opened, and a DataAdaptor is used to fill the DataTable with the records retrieved by the SELECT stored procedure. The database connection is automatically closed at the end of the Using block.

C# DatabaseQuery Class

DatabaseQuery Code - ExecuteUpdate()

To execute an Update query, an SQLCommand object is required based on the database connection and the required stored procedure. Any SQL parameters in the parameter list have to be added to the command parameters before the command is executed. If the command executes sucessfully then the parameter list is cleared.

C# DatabaseQuery Class

There is a deliberate mistake in this DatabaseQuery class. What is it? Answer.