ado

21
ADO ActiveX Data Object

Upload: abhay-singh

Post on 10-May-2015

193 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Ado

ADO

ActiveX Data Object

Page 2: Ado

ActiveX Data Object• ADO is ActiveX Data Object that allows you to use data

from databases without knowing the implementation of such a database.

• To access the data, programmer even don't have to know SQL although there is a possibility of using SQL commands through ADO as well.

• One of the main objects in ADO is a Recordset.• The general idea of using ADO is the following:

– firstly you have to create and open a connection object that allows you to connect to the database.

– Then you create a Recordset and retrieve data from the database to it.

– Afterwards you have the possibility to save potential changes to the database.

Page 3: Ado

ActiveX Data Object• ADO.NET is  ActiveX Data Object for .NET and

it's a successor to ADO.NET although it was changed completely.

• Its functionalities are similar to the ones from ADO.

• It allows you to connect to data sources and perform some operations on them.

• The main object in ADO.NET is Dataset, it's a bit similar to Recordset but it provides more possibilities for developers.

• ADO.NET makes possible disconnected access to data by taking advantage of XML files.

Page 4: Ado

ActiveX Data Object

• ADO.NET provides also frameworks that make using databases even simpler.

• ADO.NET Entity Framework for object-relational mapping.

• It presents data from databases as objects and thus makes it much easier to use it by rising level of abstraction.

• Another framework that is very useful is LINQ (Language Integrated Query), which allow you to query data in a unified and clear way.

Page 5: Ado

ADO and ADO.NET • The ADO Recordset is now called the ADO.NET DataSet.• ADO.NET uses disconnected models based on messages

whereas ADO is rather connection oriented.• ADO.NET supports XML in an easy and clear way,

moreover it uses XML for passing data. On the other hand standard ADO uses binary representation for passing data.

• ADO is based on COM (Component Object Model) and ADO.NET uses CLR (Common Language Runtime).

• Recordset in ADO and DataSet in ADO.NET.• ADO.NET uses XML to communicate DataSets from point

to point.• DataSets are a disconnected form of data access.

Page 6: Ado

DataSet and Recordset • Recordset represents data in memory, usually it's a

table or result of a query such as joining two tables or using where clause.

• At one time there can be only one current record in Recordset, if you want to perform operations on other record you have to use move method.

• On the other hand DataSet can contain one or more tables from database, these tables are stored in object called DataTable.

• It is also very easy to retrieve data from it, you can use simple foreach loop to do so.

• With DataSet you can also maintain relationships between data tables such as for instance foreign key. Generally DataSet can include more rich and complex data and organize it in a clear manner.

Page 7: Ado

DataSet and Recordset

• As I mentioned before ADO Recordset requires constant connection to database to perform some operations on it while ADO.NET DataSet allows you to make changes and submit all of them to the database later.

• ADO.NET uses also data adapter which communicates with OLE DB provider while in ADO you communicate with it directly.

• Data adapters are significant advantage because it's very easy to set up and manage this connection.

Page 8: Ado

DataSet and Recordset

• performance of DataSets is better because of its architecture, it's faster and more portable than Recordset.

• DataSet stores data in XML so it's easier to transfer it to other applications and they are more firewall-prove than Recordsets that are transferred using COM marshalling.

• DataSet is better and more robust tool to manage data from databases and it provides more functionalities.

Page 9: Ado

DataSetstring source = “server=(local)” +“integrated

security=SSPI;” +“database=Northwind”;SqlConnection conn = new SqlConnection(source);conn.Open();string query = "SELECT id, name FROM employees";   SqlDataAdapter adapter = new SqlDataAdapter(query, conn); 

DataSet company = new DataSet();   adapter.Fill(company, "employees"); 

foreach (DataRow employee in company.Tables["employees"].Rows)   { Console.WriteLine(employee["id"] + " " + employee["name"]);   }  

Page 10: Ado

Namespaces

• System.Data—All generic data access classes.• System.Data.Common—Classes shared (or

overridden) by individual data providers.• System.Data.Odbc—ODBC provider classes.• System.Data.OleDb—OLE DB provider classes.• System.Data.Oracle—Oracle provider classes.• System.Data.SqlClient—SQLServer provider

classes.• System.Data.SqlTypes—SQLServer data types.

Page 11: Ado

classes are contained in the System.Data

• DataSet—This object is designed for disconnected use and can contain a set of DataTables and include relationships between these tables.

• DataTable—A container of data that consists of one or more DataColumns and, when populated, will have one or more DataRows containing data.

• DataRow—A number of values, akin to a row from a database table, or a row from a spreadsheet.

• DataColumn—This object contains the definition of a column, such as the name and data type.

Page 12: Ado

Database-Specific Classes• SqlCommand, OleDbCommand, OracleCommand, and

ODBCComm and—Used as wrappers(binds) for SQL statements or stored procedure calls.

• SqlConnection, OleDbConnection, OracleConnection, ODBCConnection—Used to connect to the database. Similar to an ADO Connection.

• SqlDataAdapter, OleDbDataAdapter, OracleDataAdapter, ODBCDataAdapter—Used to hold select, insert, update, and delete commands, which are then used to populate a DataSet and update the Database.

• SqlDataReader, OleDbDataReader, OracleDataReader, ODBCDataReader —Used as a forward only, connected data reader.

• dr = cmd.ExecuteReader(); while (dr.Read())

Page 13: Ado

Executing Commands• ExecuteNonQuery()—Executes the command

but does not return any output.– SqlCommand cmd = new SqlCommand(select, conn);– int rowsReturned = cmd.ExecuteNonQuery();– Console.WriteLine(“{0} rows returned.”, rowsReturned);

• ExecuteReader()—Executes the command and returns a typed DataReader.– dr = cmd.ExecuteReader();– while (dr.Read())– { textBox1.Text = dr[0].ToString();

• ExecuteScalar()—Executes the command and returns a single value.

– string select = “SELECT COUNT(*) FROM Customers”;– object o = cmd.ExecuteScalar();– Console.WriteLine ( o ) ;

Page 14: Ado
Page 15: Ado

Connecting to a User Instance

• The UserInstance and AttachDBFilenameConnectionString keywords allow a SqlConnection to connect to a user instance. User instances are also supported by the SqlConnectionStringBuilderUserInstance and AttachDBFilename properties.

• Note the following about the sample connection string shown below:

• The Data Source keyword refers to the parent instance of SQL Server Express that is generating the user instance. The default instance is .\sqlexpress.

Page 16: Ado

• Integrated Security is set to true. To connect to a user instance, Windows Authentication is required; SQL Server logins are not supported.

• The User Instance is set to true, which invokes a user instance.

Page 17: Ado

• The AttachDbFileName connection string keyword is used to attach the primary database file (.mdf), which must include the full path name. AttachDbFileName also corresponds to the "extended properties" and "initial file name" keys within a SqlConnection connection string.

• The |DataDirectory| substitution string enclosed in the pipe symbols refers to the data directory of the application opening the connection and provides a relative path indicating the location of the .mdf and .ldf database and log files. If you want to locate these files elsewhere, you must provide the full path to the files.

Page 18: Ado

• To connect to the database server is recommended to use Windows Authentication, commonly known as integrated security. To specify the Windows authentication, you can use any of the following two key-value pairs with the data provider. NET Framework for SQL Server:

• Integrated Security = true;• Integrated Security = SSPI;• However, only the second works with the data

provider. NET Framework OleDb. If you set Integrated Security = true for ConnectionString an exception is thrown.

The Security Support Provider Interface

Page 19: Ado

• To specify the Windows authentication in the data provider. NET Framework for ODBC, you should use the following key-value pair.

• Trusted_Connection = yes;• Let me start with Integrated Security = false• false User ID and Password are specified

in the connection string.true Windows account credentials are used for authentication.

Page 20: Ado

• Recognized values are true, false, yes, no, and SSPI.

• If User ID and Password are specified and Integrated Security is set to true, then User ID andPassword will be ignored and Integrated Security will be used

Page 21: Ado

SQL Server Express User Instances

• Microsoft SQL Server Express Edition (SQL Server Express) supports the user instance feature, which is only available when using the .NET Framework Data Provider for SQL Server (SqlClient). A user instance is a separate instance of the SQL Server Express Database Engine that is generated by a parent instance. User instances allow users who are not administrators on their local computers to attach and connect to SQL Server Express databases. Each instance runs under the security context of the individual user, on a one-instance-per-user basis.