The connection string contains all the necessary location and authenticated information to connect to SQL Server. Also the connection string contains the name of the server, name of the database, SQL Server login, and password as shown below: -
You will use similar code to create a connection in a Microsoft Access database by changing the namespaces and classes. In Example 2, a database connection is created and opened for a Microsoft Access databases named employees.
The output is shown below.
As you are creating a connection for Microsoft Access, you must import the System.Data.OleDb namespace rather than the System.Data.SqlClient namespace.
Next, create an instance of the OleDbConnection class and initialize it with a connection string appropriate for Microsoft Access. Finally, calling the open () method of the OleDbConnection class actually opens the database connection.
In Example18, the name of the OLEDB provider for Microsoft Access (Microsoft.Jet.OLEDB.4.0) and the path to the Access database on the server is seen. If you want to connect to another type of database, you have to specify a different provider.
By default when you call the Open () method with either the SqlConnection or OleDbConnection class, the connection takes 15 seconds to open before timing out. You can overwrite this default behavior by supplying a Connect Timeout attribute in the connection string. For example, to allow a connection to open in a SQL Server up to 90 seconds you have to initialize the connection like this:
MyConnection=NewSqlConnection("Server=localhost;UID=sa;PWD=secret; Connect _ -Timeout=90" )
Finally Database has limited number of connections. Thus, whenever the task is completed it is advisable to close the connection quickly so that it is free to be used by other pages. To close either a SqlConnection or OleDbConnection, use the following statement:
myConnection. Close ()
Retrieving Records from a Table
The SQL statement that will be used most often in ASP.NET pages is Select. The Select statement enables you to retrieve records that match a certain condition from a database table. The syntax for a basic Select statement is as follows:
SELECT column1, column2...
FROM tablename1, tablename2...
WHERE search condition
For example, If you want to retrieve the empno and ename columns, from the employee’s table where the empno column has the value 1, you would use the following Select statement:
Select empno, ename
FROM employees
WHERE empno = 1
If you simply want to retrieve all the columns and all the rows from the Authors table, you would use the following Select statement:
Select * FROM employees
The asterisk (*) is a wildcard character that represents all the columns. If you use a WHERE clause, all the rows from the Authors table are automatically returned.
(For more information learn SqlServer and MS Access.)
To execute a Select statement in an ASP.NET page follow these four steps:
1. Create and open a database connection.
2. Create a database command that represents the SQL Select statement to execute.
3. Execute the command with the ExecuteReader () method returning a DataReader.
4. Loop through the DataReader displaying the results of the query.
When a query is executed using ADO.NET, the results are returned in a DataReader. Precisely the results of that query are represented by either a SqlDataReader or OleDbDataReader, depending on the database from which you are retrieving the records.
A DataReader represents a forward-only stream of database records; i.e. the DataReader represents only a single record at a time. To get the next record in the stream, and display them you must call the Read () method repeatedly until you the end of the stream is reached. Once the record is passed, there's no going back.
The following example, displays all the records from a SQL Server database named Northwind and table named employees as shown below in example.