Special Class Of C# Series - Part Two - OLE DB (Extended Properties)

Introduction:

In this series of C# Special class, we are going to see the special feature available with the “OleDbConnection” class and its specialties.

What is OleDbConnection?

An OleDbConnection object represents a unique connection to a data source. With a client/server database system, it is equivalent to a network connection to the server. Depending on the functionality supported by the native OLE DB provider, some methods or properties of an OleDbConnection object may not be available, such as -

 public sealed class OleDbConnection : DbConnection, ICloneable, IDbConnection, IDisposable

Reference

https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection(v=vs.110).aspx

When to use OleDbConnection?

When the connection is established between C# application and the specified data source, SQL commands will execute with the help of the Connection Object and retrieve or manipulate the data in the database.

What is the group of OleDb?

The OleDb has a set of dependent classes which is used to build the connection with the external data sources and also, adapter classes are used to read the data from the source of data.

  1. OleDbConnection
  2. OleDbDataAdapter
  3. OleDbSchemaGuid

Where to use this OleDbConnection – Extended Properties?

The Extended Properties help to identify the type of data sources and it will customize itself to read the data from the sources, say, for example, I am going to show you how to read the data from the EXCEL data sources.

The Extended Properties are responsible to specify the type of sources.

Microsoft.Jet.OLEDB.4.0; data source={0};Extended Properties=Excel 8.0

Sample Code

The oledb should need the supporting framework files to be included in the project.

  1. using System;  
  2. using System.Data;  
  3. using System.Data.OleDb;  

Connection String

This is the place which provides the exact location of the file available.

  1. string connectionString = string.Format("provider=Microsoft.Jet.OLEDB.4.0; data source={0};Extended Properties=Excel 8.0;", fileName);  

Sample Excel used

This is single excel file which has three sheets and different data in each of the sheets,

  • Admin
  • HR
  • Account

    C#

    C#

    C#

Code Implementation

This part of the code snippet is used to define the Excel sheet names and then fetch data.

  1. static string[] GetExcelSheetNames(string connectionString)  
  2.       {  
  3.           OleDbConnection con = null;  
  4.           DataTable dt = null;  
  5.           con = new OleDbConnection(connectionString);  
  6.           con.Open();  
  7.           dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);  
  8.   
  9.           if (dt == null)  
  10.           {  
  11.               return null;  
  12.           }  
  13.   
  14.           String[] excelSheetNames = new String[dt.Rows.Count];  
  15.           int i = 0;  
  16.   
  17.           foreach (DataRow row in dt.Rows)  
  18.           {  
  19.               excelSheetNames[i] = row["TABLE_NAME"].ToString();  
  20.               i++;  
  21.           }  
  22.   
  23.           return excelSheetNames;  
  24.       }  

Data Set manipulation

This part of code is responsible for manipulation of data and getting the data to assign to the appropriate data table and then add all the data tables to the data set.

  1. static DataSet Parse(string fileName)  
  2.         {  
  3.             string connectionString = string.Format("provider=Microsoft.Jet.OLEDB.4.0; data source={0};Extended Properties=Excel 8.0;", fileName);  
  4.   
  5.             DataSet data = new DataSet();  
  6.   
  7.             foreach (var sheetName in GetExcelSheetNames(connectionString))  
  8.             {  
  9.                 using (OleDbConnection con = new OleDbConnection(connectionString))  
  10.                 {  
  11.                     var dataTable = new DataTable();  
  12.                     dataTable.TableName = Convert.ToString(sheetName).TrimEnd('$');  
  13.                     string query = string.Format("SELECT * FROM [{0}]", sheetName);  
  14.                     con.Open();  
  15.                     OleDbDataAdapter adapter = new OleDbDataAdapter(query, con);  
  16.                     adapter.Fill(dataTable);  
  17.                     data.Tables.Add(dataTable);  
  18.                 }  
  19.             }  
  20.   
  21.             return data;  
  22.         }  

Full Code Implementation

  1. using System;  
  2. using System.Data;  
  3. using System.Data.OleDb;  
  4.   
  5. namespace ReadExcel  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             var ds = Parse(@"C:\Selenium\samp.xls");  
  12.         }  
  13.   
  14.         static DataSet Parse(string fileName)  
  15.         {  
  16.             string connectionString = string.Format("provider=Microsoft.Jet.OLEDB.4.0; data source={0};Extended Properties=Excel 8.0;", fileName);  
  17.   
  18.             DataSet data = new DataSet();  
  19.   
  20.             foreach (var sheetName in GetExcelSheetNames(connectionString))  
  21.             {  
  22.                 using (OleDbConnection con = new OleDbConnection(connectionString))  
  23.                 {  
  24.                     var dataTable = new DataTable();  
  25.                     dataTable.TableName = Convert.ToString(sheetName).TrimEnd('$');  
  26.                     string query = string.Format("SELECT * FROM [{0}]", sheetName);  
  27.                     con.Open();  
  28.                     OleDbDataAdapter adapter = new OleDbDataAdapter(query, con);  
  29.                     adapter.Fill(dataTable);  
  30.                     data.Tables.Add(dataTable);  
  31.                 }  
  32.             }  
  33.   
  34.             return data;  
  35.         }  
  36.   
  37.         static string[] GetExcelSheetNames(string connectionString)  
  38.         {  
  39.             OleDbConnection con = null;  
  40.             DataTable dt = null;  
  41.             con = new OleDbConnection(connectionString);  
  42.             con.Open();  
  43.             dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);  
  44.   
  45.             if (dt == null)  
  46.             {  
  47.                 return null;  
  48.             }  
  49.   
  50.             String[] excelSheetNames = new String[dt.Rows.Count];  
  51.             int i = 0;  
  52.   
  53.             foreach (DataRow row in dt.Rows)  
  54.             {  
  55.                 excelSheetNames[i] = row["TABLE_NAME"].ToString();  
  56.                 i++;  
  57.             }  
  58.   
  59.             return excelSheetNames;  
  60.         }  
  61.   
  62.   
  63.     }  
  64. }  

Result of the Dataset

Account Table

C#

Admin Table

C#

HR Table

C#

Conclusion

This is one of the features where the OLEDB classes are used and in most of the data source transactions and connections, this class plays a major role.

Post your questions in the comments.

Happy learning.


Similar Articles