Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
85 views15 pages

Data Table: Datasets, Datatables, and Dataviews

The document provides information on ADO.NET objects used to work with data. It describes the key objects - SqlConnection for connecting to a database, SqlCommand for executing commands, SqlDataReader for reading data streams, DataSet for in-memory data representation with related tables, and SqlDataAdapter for transferring data between a database and DataSet.

Uploaded by

Wadhwa Kannu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views15 pages

Data Table: Datasets, Datatables, and Dataviews

The document provides information on ADO.NET objects used to work with data. It describes the key objects - SqlConnection for connecting to a database, SqlCommand for executing commands, SqlDataReader for reading data streams, DataSet for in-memory data representation with related tables, and SqlDataAdapter for transferring data between a database and DataSet.

Uploaded by

Wadhwa Kannu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

DATA TABLE

A DataSet is made up of a collection of tables, relationships, and constraints. In ADO.NET, DataTable objects are used to represent the tables in a DataSet. A DataTable represents one table of in-memory relational data; the data is local to the .NET-based application in which it resides, but can be populated from a data source such as Microsoft SQL Server using a DataAdapter For more information, see Populating a DataSet from a DataAdapter. The DataTable class is a member of the System.Data namespace within the .NET Framework class library. You can create and use a DataTable independently or as a member of a DataSet, and DataTable objects can also be used in conjunction with other .NET Framework objects, including the DataView. You access the collection of tables in a DataSet through the Tables property of the DataSet object. The schema, or structure of a table is represented by columns and constraints. You define the schema of a DataTable using DataColumn objects as well as ForeignKeyConstraint and UniqueConstraint objects. The columns in a table can map to columns in a data source, contain calculated values from expressions, automatically increment their values, or contain primary key values. In addition to a schema, a DataTable must also have rows to contain and order data. The DataRow class represents the actual data contained in a table. You use the DataRow and its properties and methods to retrieve, evaluate, and manipulate the data in a table. As you access and change the data within a row, the DataRow object maintains both its current and original state. You can create parent-child relationships between tables using one or more related columns in the tables. You create a relationship between DataTable objects using a DataRelation. DataRelation objects can then be used to return the related child or parent rows of a particular row. For more information, see Adding DataRelations.

DataSets, DataTables, and DataViews


The ADO.NET DataSet is a memory-resident representation of data that provides a consistent relational programming model regardless of the source of the data it contains. A DataSet represents a complete set of data including the tables that contain, order, and constrain the data, as well as the relationships between the tables. There are several ways of working with a DataSet, which can be applied independently or in DataSet, combination. You can:

Programmatically create a DataTable, DataRelation, and Constraint within a DataSet and populate DataTable, DataRelation,
the tables with data.

Populate the DataSet with tables of data from an existing relational data source using a
DataAdapter. .

Load and persist the DataSet contents using XML. For more information, see Using XML in a
DataSet. DataSet. A strongly typed DataSet can also be transported using an XML Web service. The design of the DataSet makes it ideal for transporting data using XML Web services. For an overview of XML Web

services, see XML Web Services Overview. For an example of consuming a DataSet from an XML Web Overview. service, see Consuming a DataSet from an XML Web Service. Service.

Retrieving Database Schema Information


Obtaining schema information from a database is accomplished with the process of schema discovery. Schema discovery allows applications to request that managed providers find and return information about the database schema, also known as metadata, of a given database. metadata Different database schema elements such as tables, columns, and stored-procedures are exposed through schema collections. Each schema collection contains a variety of schema information specific to the provider being used. Each of the .NET Framework managed providers implement the GetSchema method in the Connection class, and the schema information that is returned from the GetSchema method comes in the form of a DataTable. The GetSchema method is an overloaded method that provides DataTable. optional parameters for specifying the schema collection to return, and restricting the amount of information returned. The .NET Framework Data Providers for OLE DB, ODBC, Oracle, and SqlClient provide a GetSchemaTable method that returns a DataTable describing the column metadata of the DataReader. . The .NET Framework Data Provider for OLE DB also exposes schema information by using the GetOleDbSchemaTable method of the OleDbConnection object. As arguments, GetOleDbSchemaTable takes an OleDbSchemaGuid that identifies the schema information to return, and an array of restrictions on those returned columns. GetOleDbSchemaTable returns a DataTable populated with the requested schema information.

Creating a DataTable
A DataTable, which represents one table of in-memory relational data, can be created and used independently, or can be used by other .NET Framework objects, most commonly as a member of a DataSet. You can create a DataTable object by using the appropriate DataTable constructor. You can add it to the DataSet by using the Add method to add it to the DataTable object's Tables collection

Handling DataTable Events


The DataTable object provides a series of events that can be processed by an application. The following table describes DataTable events.

EVENT
INITIALIZED

DESCRIPTION
OCCURS AFTER THE ENDINIT METHOD OF A DATATABLE IS CALLED. THIS EVENT IS INTENDED PRIMARILY TO
SUPPORT DESIGN-TIME SCENARIOS.

COLUMNCHANGED

OCCURS AFTER A VALUE HAS BEEN SUCCESSFULLY CHANGED IN A DATACOLUMN.

COLUMNCHANGING ROWCHANGED

OCCURS WHEN A VALUE HAS BEEN SUBMITTED FOR A DATACOLUMN. OCCURS AFTER A DATACOLUMN VALUE OR THE ROWSTATE OF A DATAROW IN THE DATATABLE HAS BEEN
CHANGED SUCCESSFULLY.

ROWCHANGING

OCCURS WHEN A CHANGE HAS BEEN SUBMITTED FOR A DATACOLUMN VALUE OR THE ROWSTATE OF A DATAROW IN THE DATATABLE.

ROWDELETED ROWDELETING TABLECLEARED

OCCURS AFTER A DATAROW IN THE DATATABLE HAS BEEN MARKED AS DELETED. OCCURS BEFORE A DATAROW IN THE DATATABLE IS MARKED AS DELETED. OCCURS AFTER A CALL TO THE CLEAR METHOD OF THE DATATABLE HAS SUCCESSFULLY CLEARED EVERY DATAROW.

TABLECLEARING TABLENEWROW DISPOSED

OCCURS AFTER THE CLEAR METHOD IS CALLED BUT BEFORE THE CLEAR OPERATION BEGINS. OCCURS AFTER A NEW DATAROW IS CREATED BY A CALL TO THE NEWROW METHOD OF THE DATATABLE. OCCURS WHEN THE DATATABLE IS DISPOSED. INHERITED FROM MARSHALBYVALUECOMPONENT.

SEQUENCE

OF

OPERATIONS

Here is the sequence of operations that occur when a DataRow is added, modified, or deleted: 1. CREATE THE PROPOSED RECORD AND APPLY ANY CHANGES. 2. CHECK CONSTRAINTS FOR NON-EXPRESSION COLUMNS.

3.

RAISE THE ROWCHANGING OR ROWDELETING EVENTS AS APPLICABLE.

4. SET THE PROPOSED RECORD TO BE THE CURRENT RECORD. 5. UPDATE ANY ASSOCIATED INDEXES.

6.

RAISE LISTCHANGED EVENTS FOR ASSOCIATED DATAVIEW OBJECTS AND PROPERTYCHANGED EVENTS FOR ASSOCIATED DATAROWVIEW OBJECTS.

7. EVALUATE ALL EXPRESSION COLUMNS, BUT DELAY CHECKING ANY CONSTRAINTS ON THESE COLUMNS.

8. 9.

RAISE LISTCHANGED EVENTS FOR ASSOCIATED DATAVIEW OBJECTS AND PROPERTYCHANGED EVENTS FOR ASSOCIATED DATAROWVIEW OBJECTS AFFECTED BY THE EXPRESSION COLUMN EVALUATIONS. RAISE ROWCHANGED OR ROWDELETED EVENTS AS APPLICABLE.

10. CHECK CONSTRAINTS ON EXPRESSION COLUMNS.

ADO.NET OBJECTS
ADO.NET includes many objects you can use to work with data. This section introduces some of the primary objects you will use. Over the course of this tutorial, you'll be exposed to many more

ADO.NET objects from the perspective of how they are used in a particular lesson. The objects below are the ones you must know. Learning about them will give you an idea of the types of things you can do with data when using ADO.NET. THE SQLCONNECTION OBJECT To interact with a database, you must have a connection to it. The connection helps identify the database server, the database name, user name, password, and other parameters that are required for connecting to the data base. A connection object is used by command objects so they will know which database to execute the command on. THE SQLCOMMAND OBJECT The process of interacting with a database means that you must specify the actions you want to occur. This is done with a command object. You use a command object to send SQL statements to the database. A command object uses a connection object to figure out which database to communicate with. You can use a command object alone, to execute a command directly, or assign a reference to a command object to an SqlDataAdapter, which holds a set of commands that work on a group of data as described below. THE SQLDATAREADER OBJECT Many data operations require that you only get a stream of data for reading. The data reader object allows you to obtain the results of a SELECT statement from a command object. For performance reasons, the data returned from a data reader is a fast forward-only stream of data. This means that you can only pull the data from the stream in a sequential manner This is good for speed, but if you need to manipulate data, then a DataSet is a better object to work with. THE DATASET OBJECT DataSet objects are in-memory representations of data. They contain multiple Datatable objects, which contain columns and rows, just like normal database tables. You can even define relations between tables to create parent-child relationships. The DataSet is specifically designed to help manage data in memory and to support disconnected operations on data, when such a scenario make sense. The DataSet is an object that is used by all of the Data Providers, which is why it does not have a Data Provider specific prefix. THE SQLDATAADAPTER OBJECT Sometimes the data you work with is primarily read-only and you rarely need to make changes to the underlying data source Some situations also call for caching data in memory to minimize the number of database calls for data that does not change. The data adapter makes it easy for you to accomplish these things by helping to manage data in a disconnected mode. The data adapter fills a DataSet object when reading the data and writes in a single batch when persisting changes back to the database. A data adapter contains a reference to the connection object and opens and closes the connection automatically when reading from or writing to the database. Additionally, the data adapter contains command object references for SELECT, INSERT, UPDATE, and DELETE operations on the data. You will have a data adapter defined for each table in a DataSet and it will take care of all communication with the database for you. All you need to do is tell the data adapter when to load from or write to the database.

SUMMARY
ADO.NET is the .NET technology for interacting with data sources. You have several Data Providers, which allow communication with different data sources, depending on the protocols they use or what

the database is. Regardless, of which Data Provider used, you'll use a similar set of objects to interact with a data source. The SqlConnection object lets you manage a connection to a data source. SqlCommand objects allow you to talk to a data source and send commands to it. To have fast forward-only read access to data, use the SqlDataReader.

Simple Binding
Data Binding Data Binding is binding controls to data from the database. With data binding we can bind a control to a particular column in a table from the database or we can bind the whole table to the data grid. Data binding provides simple, convenient, and powerful way to create a read/write link between the controls on a form and the data in their application. Windows Forms supports binding data to ADO .NET DataSet, Array, ArrayList, etc. A control can be bound to any collection that supports indexed access to the elements in that collection. Simple Data Binding Simple binding allows us to display one data element from a table in a control. Simple binding is managed by use of the Bindings collection on each control. Simple bound controls show only one data element at a time. We have to create our own navigation controls (buttons) to see other data elements. These navigation controls allow us to move from record to record by clicking buttons and the data in the bound controls will be updated automatically. To bind any property of a control, you need to click the ellipse button of the Advanced property under Data Bindings property in the properties window. Clicking on the ellipse opens up Advanced Data Binding Dialog in which we can bind any property of a control to a data source. Working with Example To understand Simple Binding we will create a simple data entry form and work with it. We will create our own table in Access and access data from that table with a Form. To start, open a blank database in MS Access, name it as Emp and save it in the C: drive of your machine. Create a table, Table1 with the following columns, EmpId, EmpName, EmpLocation, EmpSalary and EmpDesignation. The columns and their data types should look like the image below.

Once you finish creating the table with required columns, enter some values and close it. Get back to Visual Studio, open a new Windows Form and from the toolbox add five TextBoxes, five Labels and eight Buttons. Here, we will bind data from the table we created in Access to TextBoxes. TextBox1 will display EmpId, TextBox2 will display EmpName, TextBox3 will display EmpLocation, TextBox4 will display EmpSalary and TextBox5 will

display

EmpDesignation.

The

image

below

displays

the

form

in

design

view.

Set the text and name property of all the buttons as shown above in the properties window. Now, in the toolbox, click the Data tab and drag an OleDbConnection object onto the form. The image below displays items from the Data tab.

Once the connection object is added to the component tray, open it's properties window to set the connection string. Select ConnectionString property in the properties window, click on the drop-down arrow and select <New Connection...> item. That looks like the image below.

When you select <New Connection...>, it opens the Data Link Properties dialog box. Click on Provider tab in this box and select "Microsoft Jet 4.0 OLE DB Provider". By default it selects provider for SQL Server. After selecting MS Jet, click Next. The image below displays the Data Link properties dialog.

Clicking next takes you to Connection tab. Here, browse for Emp.mdb database in the selection area by clicking the ellipse button and click "Test Connection" button. If connection succeeds, it displays a message box stating that the connection succeeded. The image below displays that.

Once you are done with it, click OK. Until this point we created a Connection object that knows how to connect to the database. We still need other objects that will make use of this connection. Get back to the Data tab in toolbox and drag an OleDbDataAdapter tool onto the Form. This adds a new data adapter to the form and automatically starts the Data Adapter Configuration Wizard. You can view configuration of data adapter here. After you finish configuring the data adapter we need to create a Dataset.

Simple data binding

The ability of a control to bind to a single data element, such as a value in a column in a dataset table. This is the type of binding typical for controls such as a TextBox control or Label control, which are controls that typically only displays a single value. In fact, any property on a control can be bound to a field in a database. There is extensive support for this feature in Visual Studio.

Complex data binding

The ability of a control to bind to more than one data element, typically more than one record in a database. Complex binding is also called list-based binding. Examples of controls that support complex binding are the DATAGRIDVIEW, LISTBOX, and COMBOBOX

controls. For an example of complex data binding, see HOW TO: BIND A WINDOWS FORMS COMBOBOX OR LISTBOX CONTROL TO DATA.

TO

BIND A

COMBOBOX

OR

LISTBOX

CONTROL

1.

2.

Set theDataSource property to a data source object. Possible data sources include a BindingSource bound to data, a data table, a data view, a dataset, a data view manager, an array, or any class that implements the IList interface. For more information, see Data Sources Supported by Windows Forms. If you are binding to a table, set theDisplayMember property to the name of a column in the data source.

private void BindComboBox() { comboBox1.DataSource = dataSet1.Tables["Suppliers"]; ]; dataSet1.Tables[ comboBox1.DisplayMember = "ProductName"; ;

DATA GRID
INTRODUCTION
This small application for DataGrid allows users to:

DATAGRID. SAVE OR UPDATE A ROW OF THE DATAGRID. DELETE AN EXISTING ROW FROM THE DATAGRID. READ THE DATA FROM AN XML FILE.
ADD A NEW ROW IN THE

The DataGrid control in the "DataGrid Application" binds to a single DataSet object. The DataSet object of the "DataGrid Application" is initially populated from a database using an OleDbDataAdapter object.

WHAT

IS A

DATAGRID?

The Windows Forms DataGrid control provides a user interface to ADO.NET datasets, displays ADO.NET tabular data in a scrollable grid, and allows for updates to the data source. In cases where the DataGrid is bound to a data source with a single table containing no relationships, the data appears in simple rows and columns, as in a spreadsheet. The DataGrid control is one of the most useful and flexible controls in Windows Forms. As soon as the DataGrid control is set to a valid data source, the control is automatically populated, by creating columns and rows based on the structure of the data. The DataGrid control can be used to display either a single table or the hierarchical relationships between a set of tables. For example: if you bind the DataGrid to data with multiple related tables, and if you enable navigation on the DataGrid, the DataGrid displays so called 'expanders' in each row.

With an expander, you can navigate from a parent table to a child table. If you click a node in the DataGrid, it displays the child table. If you click the Back button, it displays the original parent table. In this fashion, the grid displays the hierarchical relationships between tables.

Bear in mind that only one table can be shown in the DataGrid at a time. Valid data sources for the DataGrid include:

DATATABLE DATAVIEW DATASET DATAVIEWMANAGER A SINGLE DIMENSION ARRAY ANY COMPONENT THAT IMPLEMENTS THE ILISTSOURCE INTERFACE.

You might also like