|
Data Access in .Net using ADO.Net
If you are new to VB.Net School For previous lessons click here Lesson Plan Introducing ADO.NET Most of today's applications need to interact with database systems to persist, edit or view data. In .NET, data access services are provided through ADO.NET components. ADO.NET is an object oriented framework that allows you to interact with database systems. We usually interact with database systems through SQL queries or stored procedures. The best thing about ADO.NET is that it is extremely flexible and efficient. ADO.NET also introduces the concept of disconnected data architecture. In traditional data access components, you made a connection to the database system and then interacted with it through SQL queries using the connection. The application stays connected to the DB system even when it is not using DB services. This commonly wastes valuable and expensive database resources, as most of the time applications only query and view the persistent data. ADO.NET solves this problem by managing a local buffer of persistent data called a data set. Your application automatically connects to the database server when it needs to run a query and then disconnects immediately after getting the result back and storing it in the dataset. This design of ADO.NET is called disconnected data architecture and is very much similar to the connectionless services of HTTP on the internet. It should be noted that ADO.NET also provides connection oriented traditional data access services. Traditional Data Access Architecture
Different components of ADO.NET Before going into the details of implementing data access applications using ADO.NET, it is important to understand its different supporting components or classes. All of the generic classes for data access are contained in the System.Data namespace.
ADO.NET also contains some database specific classes. This means that different database system providers may provide classes (or drivers) optimized for their particular database system. Microsoft itself has provided the specialized and optimized classes for their SQL server database system. The names of these classes start with 'Sql' and are contained in the System.Data.SqlClient namespace. Similarly, Oracle has also provides its classes (drivers) optimized for the Oracle DB System. Microsoft has also provided the general classes which can connect your application to any OLE supported database server. The name of these classes start with 'OleDb' and these are contained in the System.Data.OleDb namespace. In fact, you can use OleDb classes to connect to SQL server or Oracle database; using the database specific classes generally provides optimized performance however.
A review of basic SQL queries SQL SELECT Statement SELECT * from empselects all the fields of all the records from the table named 'emp' SELECT empno, ename from empselects the fields empno and ename for all of the records from the table named 'emp' SELECT * from emp where empno < 100selects all records from the table named 'emp' where the value of the field empno is less than 100 SELECT * from article, author where article.authorId = author.authorIdselects all records from the tables named 'article' and 'author' that have the same value of the field authorId SQL INSERT Statement INSERT INTO emp(empno, ename) values(101, 'John Guttag')inserts a record in to the emp table and sets its empno field to 101 and its ename field to 'John Guttag' SQL UPDATE Statement UPDATE emp SET ename = 'Eric Gamma' WHERE empno = 101updates the record whose empno field is 101 by setting its ename field to 'Eric Gamma' SQL DELETE Statement DELETE FROM emp WHERE empno = 101deletes the record whose empno field is 101 from the emp table
UPDATE emp SET enddate = GetNow(date) WHERE empno = 101 To remove this record from the users reach in future quieries. Select * FROM emp WHERE enddate = Null
School Home
|
|
|