There are many ways to accomplish any given task in ASP.NET. For example, on Day 10, you learned how to use the OleDbCommand and OleDbDataAdapter objects to retrieve data from a database using SQL statements. Today you learned how to parameterize those statements and retrieve the same data. On Day 11, you learned how to use the XmlDocument object to manipulate XML files. The XPathNavigator object used today can accomplish the same tasks, as well as a few others. The more of these methods you learn, the stronger a programmer you'll become. However, even if you learn every available method, generally it's a good idea to select a particular method and stick with it. This makes it easier for you to understand your code later on, as well as for other developers to read your code.
Parameterized queries allow you to pass in variable data to SQL statements, both for input and output. You use parameters by substituting the variable part of your queries with variables prefixed by the @ symbol. For example:
SELECT FirstName FROM tblUsers WHERE UserID = @ID
Parameters can be created in ASP.NET with the OleDbParameter object. You can specify the parameter name, value, type, and direction, such as input or output. For example:
dim objParam as OleDbParameter objParam = objCmd.Parameters.Add("@ID", OleDbType.Integer) objParam.Direction = ParameterDirection.Input objParam.Value = 1
Stored procedures are compiled SQL statements that allow database applications to execute queries much more efficiently than regular statements. There are a number of benefits to using stored procedures, including increased performance and more modularity in your applications.
You create stored procedures using regular SQL statements within a database application, such as MS SQL Server 2000 or Access. You can then execute these procedures from ASP.NET with the OleDbCommand object by setting the CommandType property to StoredProcedure. You can also create parameters for stored procedures, just as you would with normal parameterized queries. Returned values can be placed in output parameters. They can also return value parameters or a data control such as a DataReader.
Transactions allow databases to undo changes. Any commands executed within a transaction are logged. The database can then revert to a state before the commands were executed to undo the changes. In effect, transactions allow you to build an all-or-none execution. You start a transaction by calling BeginTransaction on the connection object, and then all subsequent statements are protected they can be rolled back. The Commit method makes changes final, and the RollBack method undoes any changes.
XPathNavigator allows cursor-style access to an XML document, dynamically loading nodes as necessary. This object is similar to the XmlDocument object and supports many of the same methods. In addition, XPathNavigator supports XPath queries and XSL transforms.
XPath queries are statements that gather specific parts of an XML file, much as SQL statements gather parts of a data store. You execute XPath queries with the XPathNavigator.Select method.
XSL transforms allow you to convert one XML file into another structured document, such as an HTML page. The XmlTransform class uses XSL stylesheets to determine the format of the converted document. It supports two methods: Load and Transform. The former loads the XSL stylesheet, while the latter transforms an XML document, placing the result in either XmlReader or XmlTextWriter.
Tomorrow you're going to look at another important part of data access: file I/O. ASP.NET uses many different files to execute your applications, and you'll examine them tomorrow. You'll also learn how to read and write files, as well as examine and modify their properties.