Professional Documents
Culture Documents
Data Sources
Relational Database
XML Data
ASP.Net Apps
Native APIs
Directory Data
PHP Apps
Direct raw data access
Spreadsheet Data
DBMS (SQL)
Data access APIs: Standard: OLEDB, ODBC, JDBC; Native API
Databases
Web Applications
Databases
ASP.Net Applications
Databases
ODBC is an uniform interface that allows applications to access data from a variety of relational Database Management Systems (DBMS).
OLE DB is a comprehensive set of COM interfaces for accessing a diverse range of data in a variety of data stores. Designed as a higher-level replacement for ODBC
http://msdn.microsoft.com/en-us/library/ee730343.aspx
5
ADO.Net
ADO.NET is a set of classes that expose data access services for .NET applications. ADO.NET provides consistent access to data sources such as SQL Server, Oracle, XML, and to data sources exposed through OLEDB and ODBC.
ADO.NET Architecture
ADO.Net Architecture
http://msdn.microsoft.com/en-us/library/27y4ybxw(v=VS.90).aspx
7
A .NET Framework data provider is used for connecting to a database, executing commands, and processing results. .NET Framework data providers include:
Data Provider for SQL Server Data Provider for OLE DB Data Provider for ODBC Data Provider for Oracle EntityClient Provider
http://msdn.microsoft.com/en-us/library/a6cd7c08(v=VS.90).aspx
The .NET Framework Data Provider for SQL Server (System.Data.SqlClient) uses its own protocol to communicate with SQL Server.
It is optimized to access a SQL Server directly without adding an OLE DB or ODBC layer.
ASP.Net Applications
OLEDB
Windows Applications
Databases
Databases
10
Step
Connecting Establishes a connection to a to database Connection specific data source. Executes a command against a data source. Exposes Parameters and can Preparing execute in the scope of a commands Command Transaction from a Connection. Processing Reads a forward-only, read-only results DataReader stream of data from a data source.
11
Step 2: Build a new command with simple and static SQL statement. Associate the command with the connection.
Step 3: Open the connection and execute the command. Use a DataReader to process the result.
con.Close();
Connection
http://msdn.microsoft.com/en-us/library/ms254978(v=VS.90).aspx
http://www.connectionstrings.com/
13
Command
int number = 10; SqlCommand cmd = new SqlCommand("select top "+number+"* from books"); cmd.Connection = con; Use variables for any part of SQL statements: fields,
where clause, comparison criteria, sorting type, etc.
14
Command Execution
Depending on the command type and return value, four methods are provided for a command object.
Methods
ExecuteReader()
Return Value
Returns a DataReader object.
ExecuteScalar()
ExecuteNonQuery()
Command Parameters
Parameters are restricted to type, size, and other constraints defined for the column value. Its a way to validate user input.
Use @ to define a parameter where a value is supplied.
Example
SqlCommand cmd = new SqlCommand(); cmd.CommandText="select top 10 * from books where ListPrice<@price; cmd.Connection = con; SqlParameter sqlpara = new SqlParameter("@price", price); cmd.Parameters.Add(sqlpara);
Dont forget to add the parameter to the command.
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx
16
DataReader
Use Getter methods: GetInt32(), GetString(), etc. Use collections: reader[1], reader["BookTitle"]
Use ExecuteScalar if only
SqlDataReader data = cmd.ExecuteReader(); one value is returned. while (data.Read()) The Read() method will retrieve one row at a time. { Response.Write("<p>"+data[1]+", $"+ data["ListPrice"]+"</p>"); } Use either column index number or column con.Close(); name to get a value for the current row.
More examples
http://msdn.microsoft.com/en-us/library/haa3afyz(v=VS.90).aspx
17
Database Modification
Use ExecuteNonQuery() method for SQL UPDATE, INSERT INTO, and DELETE
FROM statements
Example
SqlCommand cmd = new SqlCommand(); cmd.CommandText = "update books set listprice = @price where bookid = 1"; cmd.Connection = con; SqlParameter sqlpara = new SqlParameter("@price", Double.Parse(this.txtNewPrice.Text)); cmd.Parameters.Add(sqlpara); con.Open(); int result = cmd.ExecuteNonQuery(); con.Close();
ExecuteNonQuery() retuens an integer indicating number of results affected. If it is 0, very likely the execution failed.
18
DataSet
The DataSet object is a disconnected, in-memory representation of a consistent relational data model.
http://msdn.microsoft.com/en-us/library/ss7fbaez(v=VS.90).aspx
19
SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds, "Books"); The fill method will automatically create
http://msdn.microsoft.com/en-us/library/bh8kx08z(v=VS.90).aspx
20
for (int i = 0; i < dv.Count; i++ ) Response.Write("<p>" + dv[i][2] + ", $" + dv[i]["ListPrice"] + "</p>");
Use a 2-D array like format to refer to the data. The first index is the row number and the second one is the column number or name.
http://msdn.microsoft.com/en-us/library/tzwewss0(v=VS.90).aspx http://msdn.microsoft.com/en-us/library/fdcwwhez(v=VS.90).aspx
21
DataReader or DataSet
Cache data locally in your application so that you can manipulate it. If you only need to read the results of a query, the DataReader is the better choice. Interact with data dynamically such as binding to a web server control or combining and relating data from multiple sources. Remote data between tiers or from an XML Web service. Perform extensive processing on data without requiring an open connection to the data source, which frees the connection to be used by other clients.
22
Summary
Key Concepts
Database access API Standard database access APIs: OLEDB, ODBC, JDBC ADO.Net .Net data provider Connection, Command, DataReader, DataSet, DataAdapter
Key skills
Use SQL Server data provider to access SQL Server databases. Send SQL queries (SELECT, UPDATE, INSERT INTO) and process results. Manipulate DataReader and DataSet objects.
23
ADO.Net
http://msdn.microsoft.com/en-us/library/e80y5yhx(v=VS.90).aspx
http://msdn.microsoft.com/en-us/library/ms254937(v=VS.90).aspx
http://msdn.microsoft.com/en-us/library/ss7fbaez(v=VS.90).aspx
http://msdn.microsoft.com/en-us/library/ee730343.aspx
http://msdn.microsoft.com/en-us/library/ms810810.aspx
24