Ado

342 views
278 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
342
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ado

  1. 1. ADO ActiveX Data Object
  2. 2. 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.
  3. 3. 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.
  4. 4. 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.
  5. 5. 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.
  6. 6. 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.
  7. 7. 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.
  8. 8. 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.
  9. 9. DataSet string 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"]); }
  10. 10. 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.
  11. 11. 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.
  12. 12. 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())
  13. 13. 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 ) ;
  14. 14. Connecting to a User Instance • The UserInstance and AttachDBFilenameConnectionS tring keywords allow a SqlConnection to connect to a user instance. User instances are also supported by the SqlConnectionStringBuilderUserInstance and Attac hDBFilename 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.
  15. 15. • 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.
  16. 16. • 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.
  17. 17. The Security Support Provider Interface • 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.
  18. 18. • To specify the Windows authentication in the data provider. NET Framework for ODBC, you should use the following keyvalue 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.
  19. 19. • 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
  20. 20. 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-

×