Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Database Processing with ADO.NET

Similar presentations


Presentation on theme: "Introduction to Database Processing with ADO.NET"— Presentation transcript:

1 Introduction to Database Processing with ADO.NET

2 The ASP.NET / ADO.NET Conundrum
The tools and wizards are wonderful for simple applications As applications become more complex and “real” You need to write code between the controls and the data providers Nested controls and one-to-many views become complex At some point, the visual designers become useless

3 Lecture Overview Introduce ASP.NET data sources
Introduce the ASP.NET data bound controls Show a few SIMPLE examples to get us started

4 History of ADO Open Database Connectivity (ODBC) was created to provide a uniform API to access SQL database servers OLE DB evolved from ODBC and provided a COM based interface Next came the first versions of ADO, which encapsulated OLE DB ADO.NET is the current generation of ADO and uses .NET Managed Providers

5 History of ADO: While not a problem, Microsoft has stepped away from all MSAccess support The data controls are gone Necessary Jet versions are not usually installed You can make them work though So we will use SQL Server Express in this course I’ll show how to connect to SQL Server

6 .NET and SQL Server Use the Server Explorer to manage connections

7 Table Designer Use it to create tables and manage schema elements
Three views GUI editor Script Summary

8 Table Designer

9 ADO.NET -The Role of Managed Providers
Managed providers interact with a data source and return data to an application Data sources are typically (almost always) disconnected After data is returned to the application, connections are closed

10 ADO / ASP.NET NOTE The capabilities of ASP and ADO are vast
There are many ways to accomplish the same task DataReaders vs. DataAdapters DataSource controls vs. manual binding

11 ADO.NET Namespaces / Classes (Intro)
There are many of these Some you don’t need to work with directly Mastery of the topic requires that you understand what the controls do and how they interact with the underlying classes

12 ADO.NET Namespaces (1) Here’s the fabric System.Data
System.Data.SqlClient System.Data.OleDb System.Data.SqlTypes

13 ADO.NET Classes (1) Connection – creates the connection with the data source (database) Support is provided for ORACLE, SQL Server, Access, etc. Transaction – allows for transactional processing Command – typically an SQL statement executed against a database server Could be a stored procedure Parameter – a parameter passed to a command

14 ADO.NET Classes (2) DataAdapter – a database object that returns a disconnected set of records (DataSet and DataTable) DataReader – a forward-only reader to get database records

15 Introduction to Connections
Executing a database command is generally a two-step process Create a connection Prepare and execute a command The ASP.NET controls help automate much of the process There are specific connection classes for OLE DB, SQL Server and others They all work the same way

16 Connection Strings There are tools to build these for you based on the database provider Connection strings are typically stored in the web.config file They store all information needed to establish a database connection More later

17 Connection Strings (Example)
Connection strings are usually stored in web.config The following reads the connection string from web.config

18 Connection Strings (Example)
The SqlConnection object represents the connection itself. Its properties depict the connection status

19 Executing Commands (Introduction)
Use the SqlCommand object to Return one record one field (ExecuteNonQuery) Execute SQL INSERT, UPDATE, DELETE statements Return records in the form of a DataTable Return records one row at a time (DataReader)

20 SqlCommand Object (Using 1 row)
The OleDbCommand or SqlCommand objects work the same way Set the CommandText property to the SQL statement to execute Set the CommandType property to CommandType.Text Set the Connection property to an existing Connection object

21 SqlCommand Object (Using 1 row)
To return one field from one row, call ExecutScalar() on the SqlCommand object

22 SqlCommand Object (Returning Nothing)
Use the ExecuteNonQuery method to execute SQL statements that do not return values INSERT / UPDATE / DELETE As before, the CommandText property stores the SQL statement Two ways to do INSERT / UPDATE /DELETE With parameters Roll the statement by hand

23 SqlCommand Object (Returning Nothing - Manual)
Use create the CommandText, CommandType and Connection as before The INSERT / UPDATE / DELETE statements get contorted

24 SqlCommand Object (Parameters 1 )
Parameters provide the preferred way to pass data to INSERT / UPDATE / DELETE and other SQL statements Parameters begin with in the SQL statement

25 SqlCommand Object (Parameters 1 )
Then we create the parameters as part of the command object We are adding items to the parameters collection

26 Using the DataReader This is straight out of the book
There are much better ways to read data but here is the first cut It’s a forward only reader Call Read to read the first and subsequent records Create an SqlCommand object as before Call ExecuteReader to initialize Call Read to read each record Index contains the field name

27 Using the DataReader (example)

28 Reading Multiple Records
Here, we use the DataSet and DataTable objects along with the DataAdapter Steps Create SqlCommand with the SELECT statement as before Create a new SqlDataAdapter Create a DataSet Call Fill on the DataAdapter to populate the DataSet

29 Reading Multiple Records

30 Introduction to Data Sources
Data sources establish the connection between an ASP.NET application and a database These are not the same controls as the desktop controls

31 Data Source Controls (Categories)
Tabular SqlDataSource – For SQL databases ObjectDataSource – For custom business objects Hierarchical SiteMapDataSource – For menus XmlDataSource – To bind XML documents

32 Data Sources (Core Concepts)
The ASP.NET data source controls are bound to a page rather than the application They belong to System.Web rather than System.Data so they are again unique to ASP.NET Underneath the hood, there is a DataSet You can use the System.Data classes from an ASP.NET application Visual Studio does a great job configuring the data source controls

33 Data Sources (Under the Hood 1)
The Wizards configure properties to Write SELECT statements and customize them Write INSERT, UPDATE, and DELETE statements that are executed by other bound controls

34 Data Sources (Under the Hood 2)
The Properties window tells the tale


Download ppt "Introduction to Database Processing with ADO.NET"

Similar presentations


Ads by Google