15/11/2015
ADO.Net
Pretty much every application deals with data in some manner, whether that data comes from memory, databases, XML files, text files, or something else. The location where we store the data can be called as a Data Source or Data Store where a Data Source can be a file, database, or indexing server etc.
Programming Languages cannot communicate with Data Sources directly because each Data Source adopts a different Protocol (set of rules) for communication, so to overcome this problem long back Microsoft has introduced intermediate technologies like JET, Odbc and Oledb which works like bridge between the Applications and Data Sources to communicate with each other.
The Microsoft Jet Database Engine is a database engine on which several Microsoft products have been built. A database engine is the underlying component of a database, a collection of information stored on a computer in a systematic way. The first version of Jet was developed in 1992, consisting of three modules which could be used to manipulate a database. JET stands for Joint Engine Technology, sometimes being referred to as Microsoft JET Engine or simply Jet. Microsoft Access and Excel uses Jet as their underlying database engine. Over the years, Jet has become almost synonymous with Microsoft Access, to the extent where many people refer to a Jet database as an "Access database". MS developed Jet database system, a C-based interface allowing applications to access that data, and a selection of driver DLLs that allowed the same C interface to redirect input and output to databases. However, Jet did not use SQL; the interface was in C and consisted of data structures and function calls.
ODBC (Open Database Connectivity) is a standard C programming language middleware API for accessing database management systems (DBMS). ODBC accomplishes DBMS independence by using an ODBC driver as a translation layer between the application and the DBMS. The application uses ODBC functions through an ODBC driver manager with which it is linked, and the driver passes the query to the DBMS. An ODBC driver will be providing a standard set of functions for the application to use, and implementing DBMS-specific functionality. An application that can use ODBC is referred to as "ODBC-compliant". Any ODBC-compliant application can access any DBMS for which a driver is installed. Drivers exist for all major DBMSs as well as for many other data sources like Microsoft Excel, and even for text or CSV files. ODBC was originally developed by Microsoft during the early 1990s.
OLE DB (Object Linking and Embedding, Database, sometimes written as OLEDB or OLE-DB), an API designed by Microsoft, allows accessing data from a variety of sources in a uniform manner. The API provides a set of interfaces implemented using the Component Object Model (COM). Microsoft originally intended OLE DB as a higher-level replacement for, and successor to, ODBC, extending its feature set to support a wider variety of non-relational databases, such as object databases and spreadsheets that do not necessarily implement SQL. OLE DB is conceptually divided into consumers and providers. The consumers are the applications that need access to the data, and the providers are the software components that implement the interface and thereby provide the data to the consumer. An OLE DB provider is a software component enabling an OLE DB consumer to interact with a data source. OLE DB providers are alike to ODBC drivers or JDBC drivers for Java. OLE DB providers can be created to access such simple data stores as a text file and spreadsheet, through to such complex databases as Oracle, Microsoft SQL Server, and many others. It can also provide access to hierarchical data stores.
DAO’s, RDO’s and ADO’s in Visual Basic Language:
Visual Basic Language used DAO’s, RDO’s and ADO’s for data source communication without having to deal with the comparatively complex JET or ODBC or OLEDB API.
Data Access Objects is a deprecated general programming interface for database access on Microsoft Windows systems using Joint Engine Technology.
Remote Data Objects (abbreviated RDO) is the name of an obsolete data access application programming interface primarily used in Microsoft Visual Basic applications on Windows 95 and later operating systems. This includes database connection, queries, stored procedures, result manipulation, and change commits. It allowed developers to create interfaces that can directly interact with Open Database Connectivity (ODBC) data sources on remote machines.
Microsoft's ActiveX Data Objects (ADO) is a set of Component Object Model (COM) objects for accessing data sources. It provides a middleware layer between programming languages and OLE DB (a means of accessing data stores, whether they be databases or otherwise, in a uniform manner). ADO allows a developer to write programs that access data without knowing how the database is implemented; developers must be aware of the database for connection only. ADO is positioned as a successor to Microsoft's earlier object layers for accessing data sources, including RDO (Remote Data Objects) and DAO (Data Access Objects). ADO was introduced by Microsoft in October 1996.
ADO.NET Providers:
ADO.NET providers can be created to access such simple data stores as a text file and spreadsheet, through to such complex databases as Oracle, Microsoft SQL Server, MySQL, PostgreSQL, SQLite, DB2, Sybase ASE, and many others. They can also provide access to hierarchical data stores such as email systems. However, because different data store technologies can have different capabilities, every ADO.NET provider cannot implement every possible interface available in the ADO.NET standard.
ADO.Net:
It is a set of classes that expose data access services to the .NET programmer. ADO.NET provides functionality to developers writing managed code similar to the functionality provided to native COM developers by ADO. ADO.NET provides consistent access to data sources such as Microsoft SQL Server, as well as data sources exposed through OLE DB and XML. Data-sharing consumer applications can use ADO.NET to connect to these data sources and retrieve, manipulate, and update data. It is an integral part of the .NET Framework, providing access to relational data, XML, and application data. ADO.NET supports a variety of development needs, including the creation of front-end database clients and middle-tier business objects used by applications or Internet browsers. ADO.Net provides libraries for Data Source communication under the following namespaces:
• System.Data
• System.Data.Odbc
• System.Data.Oledb
• System.Data.SqlClient
• System.Data.OracleClient
Note: System.Data, System.Data.Oledb, System.Data.SqlClient and System.Data.Odbc namespaces are under the assembly System.Data.dll whereas System.Data.OracleClient is under System.Data.OracleClient.dll assembly.
System.Data: types of this namespace are used for holding and managing of data on client machines. This namespace contains following set of classes in it: DataSet, DataTable, DataRow, DataColumn, DataView, DataRelation etc.
System.Data.Odbc: types of this namespace can communicate with any Data Source like files, databases, and indexing servers etc. using Un-Managed Odbc Drivers.
System.Data.Oledb: types of this namespace can communicate with any Data Source like files, databases, and indexing servers etc. using Oledb Providers (Un-Managed COM Providers).
System.Data.SqlClient: types of this namespace can purely communicate with Sql Server database only using SqlClient Provider (Managed ADO.Net Provider).
System.Data.OracleClient: types of this namespace can purely communicate with Oracle database only using OracleClient Provider (Managed ADO.Net Provider).
All the above 4 namespaces contains same set of types as following: Connection, Command, DataReader, DataAdapter, CommandBuilder etc, but here each class is referred by prefixing with their namespace before the class name to discriminate between each other as following:
OledbConnection OledbCommand OledbDataReader OledbDataAdapter OledbCommandBuilder
SqlConnection SqlCommand SqlDataReader SqlDataAdapter SqlCommandBuilder
OracleConnection OracleCommand OracleDataReader OracleDataAdapter OracleCommandBuilder
OdbcConnection OdbcCommand OdbcDataReader OdbcDataAdapter OdbcCommandBuilder
Performing Operations on a DataSource: Each and every operation we perform on a Data Source involves in 3 steps, like:
• Establishing a connection with data source.
• Sending request to data source as a sql statement.
• Capturing the results given by data source.
Establishing a Connection with Data Source:
It's a process of opening a channel for communication between Application and Data Source that is present on a local or remote machine to perform any operations. To open the channel for communication we can use the Connection class.
Constructors of the Class:
Connection() Connection(string ConnectionString)
Note: ConnectionString is a collection of attributes that are used for connecting with a DataSource, those are:
• Provider
• Data Source
• User Id and Password
• Database or Initial Catalog
• Trusted_Connection or Integrated Security
• DSN
Provider: as discussed earlier provider is required for connecting with any data source, so we have a different provider available for each data source.
Oracle Msdaora Sql Server SqlOledb
MS-Access or MS-Excel Microsoft.Jet.Oledb.4.0 MS-Indexing Server Msidxs
Data Source: it is the name of target machine to which we want to connect with but it is optional when the data source is on a local machine.
User Id and Password: as db's are secured places for storing data, to connect with them we require a valid user id and password.
Oracle: Scott/tiger Sql Server: Sa/
Database or Initial Catalog: these attributes are used while connecting with Sql Server Database to specify the name of database we want to connect with.
Trusted_Connection or Integrated Security: these attributes are also used while connecting with Sql Server Database only to specify that we want to connect with the Server using Windows Authentication. In this case we should not again use User Id and Password attributes.
DSN: this attribute is used to connect with data sources by using Odbc Drivers.
Connection String for Oracle: "Provider=Msdaora;User Id=Scott;Password=tiger[;Data Source=]"
Connection String for Sql Server: "Provider=SqlOledb;User Id=Sa;Password=;Database=[;Data Source=]"
Note: in case of Windows Authentication in place of User Id and Password attributes we need to use Integrated Security = SSPI (Security Support Provider Interface) or Trusted_Connection = True.
Members of Connection class:
1. Open(): a method which opens a connection with data source.
2. Close(): a method which closes the connection that is open.
3. State: an enumerated property which is used to get the status of connection.
4. ConnectionString: a property with is used to get or set a connection string which is associated with the connection object.
The Object of class Connection can be created in any of the following ways:
Connnection con = new Connection(); con.ConnectionString = "";
or
Connection con = new Connection("");
Testing the process of establishing a connection: open a new project of type Windows Forms Application and name it as DBOperations. Place 2 buttons on the form and set their caption as “Connect with Oracle using OLEDB Provider” and “Connect with Sql Server using OLEDB Provider”. Now go to code view and write the following code:
using System.Data.OleDb;
Declarations: OledbConnection ocon, scon;
Under Connect with Oracle using OLEDB Provider:
ocon = new OleDbConnection(“Provider=Msdaora;User Id=Scott;Password=tiger;Data Source=");
ocon.Open(); MessageBox.Show(ocon.State.ToString()); ocon.Close(); MessageBox.Show(ocon.State.ToString());
Under Connect with Sql Server using Oledb Provider:
scon = new OleDbConnection(); scon.ConnectionString =
"Provider=SqlOledb;User Id=Sa;Password=;Database=Master;Data Source=";
scon.Open(); MessageBox.Show(scon.State.ToString()); scon.Close(); MessageBox.Show(scon.State.ToString());
Sending request to Data Source as a Sql Statement: In this process we send a request to Data Source by specifying the type of action we want to perform using a Sql Statement like Select, Insert, Update, and Delete or by calling a Stored Procedure. To send and execute those statements on data source we use the class Command.
Constructors of the class: Command() Command(string CommandText, Connection con)
Note: CommandText means it can be any Sql Stmt like Select or Insert or Update or Delete Stmts or Stored Procedure Name.
Properties of Command Class:
1. Connection: sets or gets the connection object associated with command object.
2. CommandText: sets or gets the sql statement or SP name associated with command object.
The object of class Command can be created in any of the following ways:
Command cmd = new Command(); cmd.Connection = ; cmd.CommandText = "";
or
Command cmd = new Command("", con);
Methods of Command class:
• ExecuteReader() -> DataReader
• ExecuteScalar() -> object
• ExecuteNonQuery() -> int
Note: after creating object of Command class we need to call any of these 3 methods to execute that statement.
Use ExecuteReader() method when we want to execute a Select Statement that returns data as rows and columns. The method returns an object of class DataReader which holds data that is retrieved from data source in the form of rows and columns.
Use ExecuteScalar() method when we want to execute a Select Statement that returns a single value result. The method returns result of the query in the form of an object.
Use ExecuteNonQuery() method when we want to execute any SQL statement other than select, like Insert or Update or Delete etc. The method returns an integer that tells the no. of rows affected by the statement.
Note: The above process of calling a suitable method to capture the results is our third step i.e. capturing the results given by data source.
Accessing data from a DataReader: DataReader is a class which can hold data in the form of rows and columns, to access data from DataReader it provides the following methods:
1. GetName(int columnindex) -> string
Returns name of the column for given index position.
2. Read() -> bool
Moves record pointer from the current location to next row and returns a bool value which tells whether the row to which we have moved contains data in it or not, that will be true if data is present or false if data is not present.
3. GetValue(int coloumnindex) -> object
Used for retrieving column values from the row to which pointer was pointing by specifying the column index position. We can also access the row pointed by pointer in the form of a single dimensional array also, either by specifying column index position or name, as following:
[column index] -> object
[column name] -> object