Você está na página 1de 17

ADO.NET is the new database technology of the .

NET (Dot Net) platform, and it builds on Microsoft ActiveX Data Objects (ADO). ADO.NET is an integral part of the .NET Compact Framework, providing access to relational data, XML documents, and application data.

You can use ADO.NET to access data by using the new .NET Framework data providers which are:
Data Provider for SQL Server (System.Data.SqlClient). Data Provider for OLEDB (System.Data.OleDb). Data Provider for ODBC (System.Data.Odbc). Data Provider for Oracle (System.Data.OracleClient).

There are two central components of ADO.NET classes: 1. DataSet 2. .NET Framework Data Provider.

Data Provider is a set of components including:


the Connection object (SqlConnection, OleDbConnection, OdbcConnection, OracleConnection) the Command object (SqlCommand, OleDbCommand, OdbcCommand, OracleCommand) the DataReader object (SqlDataReader, OleDbDataReader, OdbcDataReader, OracleDataReader) and the DataAdapter object (SqlDataAdapter, OleDbDataAdapter, OdbcDataAdapter,OracleDataAdapter).

DataSet object represents a disconnected cache of data which is made up of DataTables and DataRelations that represent the result of the command.

Right click on Website name, select Add New Item. Select SqlServerDatabase Object Give Proper name to Your database. Done

1. Add .NET Data Provider namespace. using System.Data.SqlClient; 2. Making Connection String
public string ConString= "Integrated Security=SSPI;" + "Initial Catalog=Northwind;" + "Data Source=SONY\\MYSQLSERVER;" ; 3. Complete Code

protected void Page_Load(object sender, EventArgs e) { SqlConnection con = null; string constr ="Integrated Security=SSPI;" + "Initial Catalog=Northwind;" + "Data Source=CBP\\Employee;"; try { // setup the database connection con = new SqlConnection(constr); con.Open(); Response.Write("connection established successfully"); } catch (Exception ex) { Response.Write("Error in connection : " + ex.Message); //MessageBox.Show("Error in connection : " + ex.Message); } finally { // dispose of open objects if (con != null) con.Close(); } //finally }

The DataSet is similar to an array of disconnected Recordset objects. It supports disconnected data access and operations, allowing greater scalability because you no longer have to be connected to the database all the time. DataSet is a copy of an extracted data being downloaded and cached in the client system.

The DataSet object is made up of two objects: DataTableCollection object containing null or multiple DataTable objects (Columns, Rows, Constraints). DataRelationCollection object containing null or multiple DataRelation objects which establish a parent/child

relation between two DataTable objects.

Create dataSet

DataSet dset = new DataSet();

A DataSet is a container; therefore, you have to fill it with data.

string strSql = "select * from tbl_emp"; SqlDataAdapter dadapter = new SqlDataAdapter(); dadapter.SelectCommand = new SqlCommand(strSql, con); //Fill Data Set dadapter.Fill(dset);

//Create a DataSet DataSet dset = new DataSet(); //Create Data Table DataTable dt = new DataTable(); //Fill Data Set dadapter.Fill(dset); //Fill DataTable via DataSet dt = dset.Tables[0]; con.Close(); DropDownList1.DataSource = dt; DropDownList1.DataTextField = dt.Columns[1].ToString(); DropDownList1.DataBind();

DataAdapter object is like a bridge that links the database and a Connection object with the ADO.NET-managedDataSet object through its SELECT and action query Commands. It specifies what data to move into and out of theDataSet. Often, this takes the form of references to SQL statements or stored procedures that are invoked to read or write to a database. The DataAdapter provides four properties that allow us to control how updates are made to the server: SelectCommand UpdateCommand InsertCommand DeleteCommand The four properties are set to Command objects that are used when data is manipulated.

The DataAdapter includes three main methods:

Fill (populates a DataSet with data). FillSchema (queries the database for schema information that is necessary to update). Update (to change the database, DataAdapter calls the DeleteCommand, the InsertCommand and the UpdateCommand properties).

CONTINUE IN PART 2-

Você também pode gostar