Professional Documents
Culture Documents
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
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.
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();
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.