Showing posts with label ADO.NET introduction. Show all posts
Showing posts with label ADO.NET introduction. Show all posts

Using ADO Extensions

Data is stored and maintained in different data sources. Some data source applications include MS-Access, SQL Server, Oracle, and Sybase. Each data source uses its own native syntax. Therefore, when you need to manage data stored in data sources from your applications, you would prefer to use standard objects and syntaxes irrespective of the data sources. It is inconvenient to use different objects, methods, and syntaxes to manage different data sources. ADOX provides a set of standard objects that you can use to manage data stored in different data sources.

ActiveX Data Objects Extensions (ADOX) is an extension of the ADO objects and programming model that allows creation, modification, and manipulation of schema objects, such as databases, tables, and columns. ADOX also includes security objects that enable you to maintain users and groups that access the schema objects. ADOX security objects can be used to grant and revoke permissions on objects that are accessed by different users and groups.


You can use ADOX objects in your Web applications to manage data stored in different data sources. However, before you can use ADOX objects, you need to establish a reference to the ADOX type library. The name of the ADOX library is Msadox.dll. To establish a reference to this type library, select Add Reference from the Project menu. Then, you can specify the path for the library.

ADOX requires an interop assembly. When you add reference for the ADOX library, a dialog box appears asking you if you want to generate an interop wrapper. At this stage, click Yes to add the ADOX reference.

Afte r adding the reference to the type library, you can go ahead and write the code to create databases, tables, or columns in a table.

To create a database, use the following syntax:

Dim ObjectName as New ADOX.Catalog
ObjectName.Create "Provider = Name of the provider; Data Source = Path of the database"

In this syntax:

ObjectName refers to the instance of the ADOX Catalog object.

The Create method takes two parameters:

Provider: Specifies the name of the database provider.

The different providers include Microsoft OLE DB

Provider for ODBC, Microsoft OLE DB Provider for the Microsoft Jet Database Engine, Microsoft OLE DB Provider for Oracle, and Microsoft OLE DB Provider for SQL Server.

Data Source: Specifies the path where you want to create the database.

In addition to managing data with ADOX objects, you can set the security options associated with different schema objects. Before you can use the ADOX objects to set user or group permissions, you must open the connection with the system database that stores all security information. Then, you can use the GetPermissions and SetPermissions functions to grant and revoke user or group access permissions on an object.

related post

CREATING ASSEMBLIES WITH ASP.NET

BUILDING HANDLERS IN ASP.NET

INTERFACES AND CLASSES CREATION IN ASP.NET

CACHING IN ASP.NET

CACHING IN ASP.NET PART TWO

WIRE LESS APPLICATION WITH ASP.NET

SECURITY IN ASP.NET PART ONE

SECURITY IN ASP.NET PART TWO

LOCALIZING ASP.NET APPLICATIONS

DEPLOYING ASP.NET APPLICATIONS

ASP.NET CONFIGURATION SYSTEM

WEB SERVICES IN ASP.NET

WEB SERVICES PART TWO

WEB SERVICE INFRASTRUCTURE

WEB SERVICE INFRASTRUCTURE PART TWO

EXCHANGING MESSAGES IN ASP.NET

MICROSOFT HAIL STORM

SOAP AND DOT NET PART ONE

SOAP AND DOT NET PART TWO


Communicating OLEDB using ADO.NET

Every application that needs to retrieve data from a database needs to establish a connection to the database. In ADO, this was achieved using the Connection object. In ADO.NET, the classes to be used for establishing the connection depend on the data source being used. For instance, to connect to SQL Server databases, the classes in the System.Data.SqlClient namespace are used. To connect to OLE DB data sources, you need to use classes in the System.Data.

OleDb namespace. It must be noted that both the SQL Server and OLE DB providers are managed providers. These providers act as a thin layer that connects the application to the database without adding any unnecessary overhead, such as converting from OLE DB -compatible data types to native SQL Server data types and vice versa when communicating between the client and the server.

The SQL Server data provider, for example, does not depend on OLE DB/ODBC. Instead, it uses the Tabular Data Stream (TDS) protocol of SQL Server to natively communicate with the SQL Server. The use of the TDS provides a tremendous performance boost to applications.

System.Data.OleDb.OleDbConnection class

This class encapsulates the connection to an OLE DB data source. Applications that need to use an OLE DB provider to connect to the data source should use this class, because not all data sources will have managed .NET data providers. When the .NET Framework is shipped by Microsoft, it provides managed data providers for some of the popular DBMSs like MS Access and SQL Server.

But, the data providers for the other DBMSs will be developed by the respective vendors. To avoid waiting for the availability of managed data providers, ADO.NET has the option of connecting to any OLE DBcompliant data sources. The OLE DB provider makes it easier for the application to be migrated to ADO.NET. All the features of ADO.NET can be readily used in an application without having to depend on the availability of a managed .NET data provider.

System.Data.OleDb.OleDbCommand class

This class encapsulates the commands that need to be sent to the OLE DB data source. Applications use the OleDbCommand class to create select, insert, update, and delete commands that need to be sent to the data source. Also, this class can be used to execute stored procedures besides sending input parameters to the stored procedure and retrieving output parameters from the stored procedure.

System.Data.OleDb.OleDbDataReader class

This class is equivalent to a forward-only, read-only Recordset object in classic ADO. This class is very useful to all applications that want to retrieve data returned from a query to the database and want to process one record at a time. A classic example for this would be to populate a list box with values retrieved from, say, a master table. The OleDbDataReader is independent of the OLE DB data source from which the data is retrieved.

The process of reading data using the OleDbDataReader object is similar to reading data from any stream.

System.Data.OleDb.OleDbDataAdapter class

The data adapter acts as the conduit between the client application and the database connection, command objects. The data adapter represents the command and connection that are used to populate the client dataset. In case of a disconnected client, the data adapter has the responsibility of firing the appropriate insert, update, or delete commands onto the database to synchronize the changes that are recorded in the client dataset.

The OleDbDataAdapter class has three command properties that are used to update the database:

InsertCommand: Represents the query or stored procedure that is used to insert new records into the data source.

SelectCommand: Represents a SQL statement used to select records in the data source.

DeleteCommand: Represents a SQL statement for deleting records from the data set.

System.Data.DataSet,system.Data.DataTable,System.Data.DataRow,and System.Data.DataColumn classes

The DataSet is a generic class provided by the .NET Framework. This class is very useful on the client side to store data in a manner that is much more functional and powerful than the ADO Recordset object. Moreover, the data in a DataSet is in XML format, and therefore is readily accessible and manageable. The XML format makes it very well suited to Web applications, and makes cross-platform access possible.

The DataSet in memory is quite similar to a full-blown, in-memory DBMS in that it has the ability to store data from multiple tables and the relationships between them. The tables are stored in DataTable objects, and DataRelation objects represent the relationship between tables. The rows and columns in a table are stored in DataRow and DataColumn objects, respectively.

related post

CREATING ASSEMBLIES WITH ASP.NET

BUILDING HANDLERS IN ASP.NET

INTERFACES AND CLASSES CREATION IN ASP.NET

CACHING IN ASP.NET

CACHING IN ASP.NET PART TWO

WIRE LESS APPLICATION WITH ASP.NET

SECURITY IN ASP.NET PART ONE

SECURITY IN ASP.NET PART TWO

LOCALIZING ASP.NET APPLICATIONS

DEPLOYING ASP.NET APPLICATIONS

ASP.NET CONFIGURATION SYSTEM

WEB SERVICES IN ASP.NET

WEB SERVICES PART TWO

WEB SERVICE INFRASTRUCTURE

WEB SERVICE INFRASTRUCTURE PART TWO

EXCHANGING MESSAGES IN ASP.NET

MICROSOFT HAIL STORM

SOAP AND DOT NET PART ONE

SOAP AND DOT NET PART TWO


ADO.NET Object Model

The .NET Framework is designed to change dramatically the developer's current style of developing applications, including the data access features. For the .NET applications, the primary data access technology to be used would be ADO.NET — the latest addition to the ADO model.

The ADO.NET Object Model is primarily divided into two levels:

Connected Layer: Consists of the classes that comprise the Managed Providers
Disconnected Layer: Is rooted in the DataSet

Managed Providers

Managed Providers are a collection of classes in the .NET Framework that provide a foundation for the ADO.NET programming model. The .NET Framework allows you to write language-neutral components, which can be called from any language, such as C++ or Visual Basic. In the .NET Framework, the OLE DB and ADO layers are merged into one layer. This results in high performance, and at the same time allows components to be called from any language. The Managed Data Providers include classes that can be used for the following:

Accessing data from SQL Server 7.0 and later

Accessing the other OLE DB providers

The Managed Provider for ADO.NET is the System.Data.OleDb namespace, which allows you to access OLE DB data sources. This namespace includes classes that are used to connect to OLE DB data sources and execute database queries to access and manipulate data.

To demonstrate how to open a connection to a SQL Server database and fill the DataSet with a database query result, consider the following code:

Dim connection As New

SqlConnection("server=localserver;uid=sa;pwd=;database=Sales")
Dim command As New SqlDataAdapter("SELECT * FROM Products Where
ProductID=@ID", connection)
Dim param1 As New SqlParameter("@ID", SqlDbType.Int)

param1.Value = 1
command.SelectCommand.Parameters.Add(param1)
Dim dataset As New DataSet()
command.Fill(dataset, "Products")
In this code:

connection is a SqlConnection class object that represents a connection to the SQL Server database.

command is a SqlDataAdapter class object that represents a set of data commands and a database connection.

param1 is a SqlParameter class object that represents the parameter to be passed in the T-SQL command.

dataset is a DataSet class object that represents the DataSet that is filled by the query results.

DataSet class

The DataSet comprises the Disconnected Layer of ADO.NET. The DataSet consists of a local buffer of tables and relations. the DataSet object model consists of Tables, Columns, Relations, Constraints, and Rows. A DataSet contains a collection of DataTables (the Tables collection). A DataTable represents one table of inmemory data. A DataTable consists of the following:



Unlike RecordSets, which are equivalent to tables in ADO, DataSets keep track of the relationships between tables if any. The DataSet is designed with a rich programming model. Th e following code creates a new DataTable with the name ProductInfo:

Dim dset As DataSet = New DataSet("ProductInfo")

Later, you can add columns to the DataTable. The columns are added to the DataTable by using the Add method on the Columns collection, and the column is assigned a name and a datatype. Finally, data is added to the table by calling the NewRow method on the DataTable and storing the column values in each DataRow.

RELATED POST

CREATING ASSEMBLIES WITH ASP.NET

BUILDING HANDLERS IN ASP.NET

INTERFACES AND CLASSES CREATION IN ASP.NET

CACHING IN ASP.NET

CACHING IN ASP.NET PART TWO

WIRE LESS APPLICATION WITH ASP.NET

SECURITY IN ASP.NET PART ONE

SECURITY IN ASP.NET PART TWO

LOCALIZING ASP.NET APPLICATIONS

DEPLOYING ASP.NET APPLICATIONS

ASP.NET CONFIGURATION SYSTEM

WEB SERVICES IN ASP.NET

WEB SERVICES PART TWO

WEB SERVICE INFRASTRUCTURE

WEB SERVICE INFRASTRUCTURE PART TWO

EXCHANGING MESSAGES IN ASP.NET

MICROSOFT HAIL STORM

SOAP AND DOT NET PART ONE

SOAP AND DOT NET PART TWO


Introduction to ADO.NET

As more and more companies are coming up with n-tier client/server and Web-based database Solutions, Microsoft with its Universal Data Access (UDA) model, offers highperformance access to diverse data and information sources on multiple platforms. Also, UDA provides an easy-to-use programming interface that works with practically any cool or language, leveraging the technical skills developers already have.

The Microsoft UDA model is a collection of Data Access Components, which are the key technologies that enable Universal Data Access. The Data Access Components include ActiveX Data Objects (ADO), Remote Data Service (RDS), formerly known as Advanced Data Connector (ADC), Object Linking and Embedding Database (OLE DB), and Open Database Connectivity (ODBC).

Microsoft is targeting many more such Data Access components that offer easy-tomaintain solutions to organizations. Such solutions are aimed at allowing organizations use their own choice of tools, applications, and data sources on the client, middle tier, or server. One of the emerging components within the UDAs collection is ADO.NET.

Microsoft ADO.NET is the latest improvement after ADO. ADO.NET provides platform interoperability and scalable data access. In the .NET Framework, data is transmitted in the Extensible Markup Language (XML) format. Therefore, any application that can read the XML format can process data. It is not necessary for the receiving component to be an ADO.NET component at all. The receiving component might be a Microsoft Visual Studio–based solution or any application running on any other platform.

Although ADO.NET preserves some of the primary concepts from previous ADO models, it has been chiefly stretched to provide access to structured data from diverse sources. ADO.NET provides access to diverse data sources by using a consistent and standardized programming model. ADO.NET is upgraded to offer several advantages over previous versions of ADO and over other data access components.

ADO.NET builds the foundation of data-aware .NET applications. ADO.NET brings together all the classes that allow data handling. Such classes represent data container objects that feature typical database capabilities — indexing, sorting, and views. While ADO.NET offers a solution for .NET database applications, it presents an overall structure that is not as database-centric as the ADO model.

The ADO model uses the concept of recordsets, which are the ADO representation of tables and views from a database. Although these recordsets are very flexible to use and allow access to data even when disconnected from data sources, they suffer from a major drawback. In the case of distributed and Web applications, data needs to be exchanged among different components at different tiers, which might be running on variety of platforms.

Of course, the format of the data being exchanged should be understood by all components. This transmission of data requires the conversion of data types of values to some data types that are recognized by the receiving components.

This conversion is called COM marshalling. Thus, the interoperability is limited when using ADO recordsets. So, the concept of ADO recordsets fails when we look at the Internet interoperability.
Like ADO, ADO.NET also allows you to access data when disconnected from actual data sources.
However, unlike ADO, ADO.NET uses XML as the data format. Because XML is a universal data format being used, ADO.NET expands the boundaries of interoperability to the Internet. In addition, instead of recordsets, ADO.NET uses the DataSet and DataReader objects to access and manipulate data. You'll learn about these objects later in the chapter. Thus, ADO.NET is designed to perform better and be more flexible than ADO. However, to support ADO objects, the corresponding equivalents exist in ADO.NET.


Data container objects are the objects that contain data to be transmitted to the receiving components. To take full advantage of ADO.NET, you should put some effort into understanding the concept itself, rather than simply figuring out the fastest way to port your code. Whatever .NET programming model you might choose — Windows Forms, Web Forms, or Web Services — ADO.NET will be there to help you with data access issues.



Interoperability

The ADO.NET model is designed to take maximum advantage of the flexibility provided by the large industry acceptance of XML. ADO.NET uses XML for transmitting datasets among components and across tiers. Any component that is capable of reading the XML format can process the data.

It is not necessary for the receiving component to be an ADO.NET component. The component that is sending or transmitting the dataset can simply transmit the dataset to its destination without bothering with how the receiving component is implemented. The component asking for the dataset, the destination component, can be implemented as a Visual Studio application or any other application.

However, the important point to be considered is that the receiving component should be capable of accepting the XML file formatted as a dataset.

Maintainability

After an application is deployed, there might be a need for changes in the application. For example, the application might need substantial architectural changes to improve its performance. As the performance load on a deployed application server grows, system resources can become inadequate, resulting in higher response times.

As a solution to these problems, the application might need to undergo architectural changes by adding tiers. Here, the problem is not the multitier application design, but rather the problem lies in increasing the number of tiers after an application is deployed. This transformation becomes easier if the original application is implemented in ADO.NET using datasets.

In ADO.NET, the communication between tiers is relatively easy, because the tiers can transmit data through XML-formatted datasets.

Programmability

The ADO.NET model uses typed programming to manipulate objects. In typed programming, the programming environment or programming language itself recognizes the types of things that are important to users. To take full advantage of typed programming, you must know the things that are of interest to programmers and to end users. Consider the following code using typed programming in ADO.NET:

If TotalQty > DataSet1.ProductInfo("Baby Food").QtyAvailable

his code is equivalent to a line using non-typed programming and is easier to read by end users. An end user who has little or no programming experience can easily grasp the meaning of the condition being tested. Also, in non-typed programming, if the developer makes a spelling mistake by chance (for example, ProductInfo is spelled as ProdcutInfo), a run-time error will get generated. On the other hand, in typed datasets,errors in the syntax caused by misspellings are detected at compile time rather than at run time.

Performance

In ADO, while transmitting data across tiers using COM marshalling in the form of disconnected RecordSets, the values must be converted to data types that are recognized by COM. This results in poor performance. On the other hand, ADO.NET is designed to use disconnected data architecture, which in turn is easier to scale because it reduces the load on database (does not require any data type conversions). Thus, in the ADO.NET model, everything is handled at the client side, which in turn improves performance.

Scalability

The Web-based, data-centric applications require multiple users to access data simultaneously. This increases the demand on data to be accessed, making scalability one of the most critical features. Applications that use resources, such as database connections and database locks, cannot support more users to access data simultaneously, because eventually the user demand for the limited resources will exceed their supply.

Because ADO.NET uses disconnected data access, applications do not retain database locks or active database connections for long durations. Hence, ADO.NET accommodates scalability by encouraging programmers to conserve limited resources, and allows more users to access data simultaneously.

RELATED POST

USING DEBUGGING TOOLS IN ASP.NET
ADO.NET OBJECT MODEL

You can go through the complete ASP.NET COURSE PART ONE AND TWO HERE.




To Do Next: Thank you for visiting PROGRAMMING BLOG. If you liked the post, please subscribe to my blog via email or RSS FEED.You can contact me here for any specific feed back .

ADO.NET introduction

Most applications require some kind of data access. Desktop applications need to integrate with central databases, Extensible Markup Language (XML) data stores, or local desktop databases. ADO.NET data access technology allows simple, powerful data access while maximizing system resource usage.

Different applications have different requirements for data access. Whether your application simply displays the contents of a table or processes and updates data to a central SQL server, ADO.NET provides the tools to implement data access easily and efficiently.

Disconnected Database Access

Previous versions of data access technologies have provided continuously connected data access by default. In such a model, an application creates a connection to a database and keeps it open for the life of the application or at least for the amount of time that data is required. As applications become more complex and databases begin to serve more and more clients, a connected data access technology becomes impractical for a variety of reasons.

Example:

Open database connections are expensive in terms of system resources. The more open connections, the less efficient system performance becomes.

Applications with connected data access are very difficult to scale up. An application that can maintain connections with two clients might do poorly with 10, and might be completely unusable with 100.

ADO.NET addresses these issues by implementing a disconnected database access model by default. In this model, data connections are established and only left open long enough to perform the requisite action. For example, if data is being requested from a database by an application, the connection is opened just long enough to load the data into the application, and then it is closed.

Likewise, if a database is being updated, the connection is opened to execute the UPDATE command, and then closed again. By keeping connections open only for the minimum required time, ADO.NET conserves system resources and allows data access to be scaled up with a minimal impact on performance.

ADO.NET Data Architecture

Data access in ADO.NET relies on two components: the DataSet, which stores data on the local machine, and the Data Provider, which mediates interaction between the program and the database.

The DataSet

The DataSet is a disconnected, in-memory representation of data. It can be thought of as a local copy of the relevant portions of the database. Data can be loaded into a DataSet from any valid data source, such as a Microsoft SQL Server database, a Microsoft Access database, or an XML file.

The DataSet is persisted in memory, and the data therein can be manipulated and updated independent of the database. When appropriate, the DataSet can then act as a template for updating the central database.

The DataSet object contains a collection of zero or more DataTable objects, each of which is an in-memory representation of a single table. The structure of a particular DataTable is defined by the DataColumns collection, which enumerates the columns in a particular table, and the Constraint Collection, which enumerates the constraints on the table.

Together, these two collections make up the schema of the table. A DataTable also contains a DataRows collection, which contains the actual data in the DataSet.

The DataSet contains a DataRelations collection. A DataRelation object allows you to create associations between rows in one table and rows in another table. The DataRelations collection enumerates a set of DataRelation objects that defines these relationships between tables in the DataSet.

For example, consider a DataSet that contains two related tables: a Customers table and an Orders table. In the Customers table, each customer is represented only once and is identified by a CustomerID field that is unique. In the Orders table, the customer who placed the order is identified by the CustomerID field, but can appear more than once if a customer has placed multiple orders. This is an example of a one-to-many relationship, and you would use a DataRelation object to define that relationship.

Additionally, a DataSet contains an ExtendedProperties collection, which is used to store custom information about the DataSet.

The Data Provider

The link to the database is created and maintained by the Data Provider. A Data Provider is not actually a single component, but a set of related components that work together to provide data in an efficient, performance-driven manner.

The Microsoft .NET Framework currently ships with two data providers: the SQL Server .NET Data Provider, which is designed specifically to work with Microsoft SQL Server 7.0 or later, and the OleDb .NET Data Provider, which connects with other types of databases. Each Data Provider consists of similar versions of the following generic component classes:

The Connection object provides the connection to the database.

The Command object is used to execute a command against a data source. It can execute either non-query commands, such as INSERT, UPDATE, or DELETE, or return a DataReader with the results of a SELECT command.

The DataReader object provides a forward-only, read-only, connected recordset.

The DataAdapter object populates a disconnected DataSet or DataTable with data and performs updates.

Data access in ADO.NET is facilitated as follows: A Connection object establishes a connection between the application and the database. This connection can be accessed directly by a Command object or by a DataAdapter object.

The Command object provides direct execution of a command to the database. If the command returns more than a single value, the Command object returns a DataReader to provide the data. This data can be directly processed by application logic. Alternatively, you can use the DataAdapter to fill a DataSet object. Updates to the database can be achieved either through the Command object or through the DataAdapter.

The generic classes that make up the Data Providers are summarized in the following sections.

The Connection Object

The Connection object represents the actual connection to the database. Microsoft Visual Studio .NET provides two types of Connection classes: the SqlConnection object, which is designed specifically to connect to Microsoft SQL Server 7.0 or later, and the OleDbConnection object, which can provide connections to a wide range of database types. The Connection object contains all of the information required to open a channel to the database in the ConnectionString property. The Connection object also provides methods that facilitate data transactions.


The Command Object

The Command object is also represented by two corresponding classes: SqlCommand and OleDbCommand. You can use the Command objects to execute commands to a database across a data connection. The Command objects can be used to execute stored procedures on the database, execute SQL commands, or return complete tables directly. Command objects provide three methods that are used to execute commands on the database:

ExecuteNonQuery.

Executes commands that have no return values, such as INSERT, UPDATE, or DELETE.

ExecuteScalar.

Returns a single value from a database query.
ExecuteReader.

Returns a result set by way of a DataReader object.

The DataReader Object

The DataReader object provides a forward-only, read-only, connected stream recordset from a database. Unlike other components of the Data Provider, DataReader objects cannot be directly instantiated. Rather, the DataReader is returned as the result of a Command object's ExecuteReader method.

The SqlCommand.ExecuteReader method returns a SqlDataReader object, and the OleDbCommand.ExecuteReader method returns an OleDbDataReader object. The DataReader can provide rows of data directly to application logic when you do not need to keep the data cached in memory. Because only one row is in memory at a time, the DataReader provides the lowest overhead in terms of system performance, but requires the exclusive use of an open Connection object for the lifetime of the DataReader.

The DataAdapter Object

The DataAdapter is the class at the core of ADO.NET's disconnected data access. It is essentially the middleman, facilitating all communication between the database and a DataSet. The DataAdapter fills a DataTable or DataSet with data from the database when the Fill method is called. After the memory-resident data has been manipulated, the DataAdapter can transmit changes to the database by calling the Update method. The DataAdapter provides four properties that represent database commands:

SelectCommand.

Contains the command text or object that selects the data from the database. This command is executed when the Fill method is called, and fills a DataTable or a DataSet.

InsertCommand.

Contains the command text or object that inserts a row into a table.

DeleteCommand.

Contains the command text or object that deletes a row from a table.

UpdateCommand.

Contains the command text or object that updates the values of a database.
When the Update method is called, changes in the DataSet are copied back to the database, and the appropriate InsertCommand, DeleteCommand, or UpdateCommand is executed.

related post

DAY 11 OOPS INTRODUCTION

DAY 12 POLYMORPHISM

DAY 13 INHERITANCE AND POLYMORPHISM

DAY 14 EBUGGING TOOLS IN DOT NET

DAY 15 DEBUG AND TRACE IN CLASSES

DAY 16 UNIT TEST PLAN

DAY 17 EXCEPTIONS IN VISUAL STUDIO

DAY 19 ADO.NET INTRODUCTION

DAY 20 DATA ACCESSING IN DOT NET

DAY 21 DATA BASE OBJECTS