You are on page 1of 4

In e-commerce applications it is very typical for the Web server to contact the database to get information as needed. C#.

NET uses a technology called ActiveX Data Objects.NET (ADO.NET) to connect to the database. Here is an example of a database, which was created with Microsoft Access. It was stored in a file named bank.mdb. It consists of two tables: the first was named UserInfo, and the second was named Transaction table. Incidentally, we could have created the database by writing C# code, but using Access is simpler.

Microsoft has provided a series of approaches for database manipulation, including ODBC (Open DataBase Connectivity), and ADO (ActiveX Data Objects). The .NET framework now provides ADO.NET. It is termed an API (applications program interface) a set of classes and their methods which provide access to a complex item. For example, the file classes such as Directory are a kind of API, in that they enable programs to access to the Windows file system.

DATA PROVIDER
CONNECTION DATA ADAPTER COMMAND

DATASET
DataTable1 DataRow/DataColumn DATA RELATION D

DATA READER DataTable2

XML OUTPUT MSSQL ORACLE ACEESS DATAVIEW

DATAVIEW

DATA STORE

The ADO.NET classes we will discuss here are: OleDbConnection, OleDbDataAdapter, DataTable, DataGrid, & OleDbCommandBuilder The namespaces for these classes are automatically imported into our program.

The OleDbConnection class


This class deals with connecting to the database. We choose to initialise the connection via the Server Explorer in the IDE, rather than by code. During this stage, we select the appropriate software to access the type of database (Ms-Access). The database can be on a computer, or on a network. After the initialisation process has been carried out, we use the Open and Close methods to manipulate the connection.

The OleDbDataAdapter class


This class provides facilities for sending SQL statements to a database. This involve transferring data from the database into DataTable objects held temporarily in RAM. To use the class, we drop an instance on our form, and then work through the steps provided by a wizard. The instance is then ready for use. The class structure of OleDbDataAdapter is rather complicated: it has some properties (named SelectCommand, InsertCommand, and DeleteCommand) which in their turn are instances of another complex class named OleDbCommand. In its turn, this class has its own properties and methods (such as CommandText and ExecuteNonQuery). However, because we will not use the OleDbCommand class separately, we will present the facilities as if they all belonged to OleDbDataAdapter. The main methods and properties are:
CommandText

This property allows us to set up an SQL string to be used later. For example:
oleDbDataAdapter1.SelectCommand.CommandText= "select * from Table where Company ='ABC'"; Fill

This method provides a query facility. It copies the contents of the connected database into a DataTable held in RAM. The returned integer provides a count of the number of matching records found. We can then access the data table by row and column position. Here is an example of Fill, showing CommandText in use:
DataTable table = new DataTable(); int recordCount; recordCount = oleDbDataAdapter1.Fill(table); The Fill method automatically opens the connection

if it needs to. This is not the case with other methods, where we need to explicitly open and close the connection.
ExecuteNonQuery

This method executes SQL commands which can update, delete, and insert records.

UpdateCommand

This property provides facilities for updating existing records via an SQL command. For example:
oleDbDataAdapter1.UpdateCommand.CommandText = "update Artists set Sales = 30.8 where Artist = 'RadioStar'"; oleDbDataAdapter1.UpdateCommand.ExecuteNonQuery(); DeleteCommand

This property provides facilities for deleting records via an SQL command. For example:
oleDbDataAdapter1.DeleteCommand.CommandText = "delete from Artists where Artist = 'RadioStar'"; oleDbDataAdapter1.DeleteCommand.ExecuteNonQuery(); InsertCommand

This property provides facilities for inserting new records via an SQL command. For example:
oleDbDataAdapter1.InsertCommand.CommandText = "insert into Artists(Artist, Company, Sales) " + "values ('The Famous Five', 'Class UK', 0)"; oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();

The DataTable class


Instances can hold a single table in RAM. The data is presented to the programmer in row/column form, rather like a 2-dimensional array. We can choose to fill a data table with an exact copy of a table in a database, or we can pick out the records we need. This is determined by the SQL string we place in CommandText. For example:
DataTable table = new DataTable(); int recordCount; recordCount = oleDbDataAdapter1.Fill(table);

We can access individual items of a data table by using the Rows property, followed by a row number and column number, as in:
string name; name = Convert.ToString(table.Rows[0][1]);

The data table holds instances of the class Object, so when we extract an item, we convert it to the appropriate type. To find out how many rows a data table currently contains, we use the Count property, as in:
int lastRowNumber; lastRowNumber = table.Rows.Count - 1; To empty a data table, we use the Clear method.

The DataGrid class


This control provides for the display of a DataTable in row/column format. It also allows the editing of existing data, and the insertion of new records at the bottom of the grid. Note that changes to the data are not automatically transferred to the database. We make use of an OleDbCommandBuilder to perform the updating. We use the DataSource property to 'bind' a data grid to a data table. From then on, any changes to the table are automatically displayed in the grid. The data grid is in the toolbox, and is positioned in the same way as most controls.

The OleDbCommandBuilder class


This class deals with generating a set of commands to update a database, based on changes that might have been made to a data table (perhaps via a data grid user interface). The Save button in the following DataGrid program shows it in use.

Creating a database program


Whatever database program you are creating, the connection and the data adapter need initialising. It is possible to do this via code, but here we show how you do the setting-up via the IDE. Follow the steps carefully. 1. Create a new project. Wait until the blank form appears. 2. View | Server Explorer 3. Right-click on Data Connections, then choose Add Connection... 4. A Data Link Properties windows appears. Select Provider at the top, then select the appropriate provider for your database. (For Microsoft Access databases, choose Microsoft Jet 4.0) , then click Next 5. The Connection tab appears. At step 1, browse to your database file, and select it. 6. Delete Admin from the User name text box. 7. Click OK 8. Move to the Server Explorer window and left-click on Data Connections. The file name that you selected should appear. (Double- clicking on the name allows you to examine the database.) 9. Now we add the data connection to the program. Click on the name of your database file in the Server Explorer window, and drag it onto your form. An object named oleDbConnection1 appears in the system tray. Now we place a data adapter on the form: 1. close the Server Explorer window, and view the toolbox. Instead of selecting Windows Forms, choose Data 2. Place an OleDbDataAdapter on the form. (Assuming you have an Access database). A wizard opens up. 3. The wizard can create SQL commands for you. We will write our own, but the wizard steps must be followed. Click Next 4. Ensure that your data connection is selected in the drop-down list. Click Next. 5. Ensure the Use SQL Statements is selected, and click Next. 6. On the Generate the SQL statements pane, choose Query Builder... An Add Table window appears. 7. Select one of your data tables (any one will do) and click Add, then Close. 8. A list of all the fields of the chosen table appears. Put a tick against All Columns. Click OK 9. An SQL Select query is shown. Click Finish. The setting-up is now complete, and you can enter code which manipulates the database.