Display Database File Using TreeView And ListView With ADO.NET

This is a new article to display database file using TreeView and ListView with ADO.NET.
 
This article show how to,
  • Use some Classes from System.Data.OleDb Namespace.
  • Populate TreeView with Tables name and Fields name.
  • Populate ListView with Records of selected Table.
I wrote the code using C# (2003).
 
The demonstration project has one Form, I add following controls to my Form (frmDataView),
  • Two Labels (lblDatabase) to display the file name and (lblTableName) to display the table name.
  • Two Buttons, one (btnLoadData) to connect with database file, other (btnExit) to end show.
  • ImageList (ImageList1) to load some icons.
  • TreeView (tvData) and ListView(lvData).
I have used two arrays as ArrayList to save tables name and fields name, tblArray to save tables name and fldArray to save fields name.
 
The code contains the following procedures,
  • DataConnection() .. to connect with database file.
  • GetTables() .. to fill tblArray with tables name.
  • GetFields() .. to fill fldArray with fields name.
  • FillTreeView() .. to fill TreeView control with tables name and fields name.
  • FillListView() .. to fill ListView control with records of selected table.
After you expand the file (prjC.zip) you can view the full code in the Form (frmDataView).
 
I hope this article is useful and helps you to display tables, fields and records from database file to TreeView and ListView. Please tell me if you have any idea or if you find any problems. 

Important remark

Using [OleDbSchemaGuid] to get the name of fields return this fields are not in the same order as its table but the fields name are sorted (A-Z), therefore you can,
  1. Create new SQL statement in the procedure [FillListView] wherein replace (*) with then fields name as ListView headers.

    or,

  2. Use the following procedure [GetFields] instead of the procedure in my article because the following procedure uses [DataSet] to get fields name in order as fields in the table.
VB.NET
  1. Private Sub GetFields(ByVal cnn As OleDbConnection, ByVal tabName As String)  
  2. Dim strSql As String = "SELECT * FROM " + tabName  
  3. Try  
  4. cnn.Open()  
  5. Dim cmdSelect As OleDbCommand = New OleDbCommand(strSql, datCon)  
  6. Dim datAdp As OleDbDataAdapter = New OleDbDataAdapter(cmdSelect)  
  7. Dim datSet As DataSet = New DataSet  
  8. datAdp.FillSchema(datSet, SchemaType.Source)  
  9. Dim columns As DataColumnCollection = datSet.Tables(0).Columns  
  10. fldArray = New ArrayList  
  11. For Each datColumn As DataColumn In columns  
  12. fldArray.Add(datColumn.ColumnName)  
  13. Next  
  14. cnn.Close()  
  15. Catch ex As Exception  
  16. MessageBox.Show(ex.Message)  
  17. End Try  
  18. End Sub  
C#
  1. private void GetFields(OleDbConnection cnn, string tabName) {  
  2.     string strSql = "SELECT * FROM " + tabName;  
  3.     try {  
  4.         cnn.Open();  
  5.         OleDbCommand cmdSelect = new OleDbCommand(strSql, datCon);  
  6.         OleDbDataAdapter datAdp = new OleDbDataAdapter(cmdSelect);  
  7.         DataSet datSet = new DataSet();  
  8.         datAdp.FillSchema(datSet, SchemaType.Source);  
  9.         DataColumnCollection columns = datSet.Tables[0].Columns;  
  10.         fldArray = new ArrayList();  
  11.         foreach(DataColumn datColumn in columns) {  
  12.             fldArray.Add(datColumn.ColumnName);  
  13.         }  
  14.         cnn.Close();  
  15.     } catch (Exception ex) {  
  16.         MessageBox.Show(ex.Message);  
  17.     }  
  18. }