ASP. NET allows you to connect to a database flexibly. A simple method is to use data source controls that encapsulate data access to controls that can be configured using connection and query information. Alternatively, you can use the ADO. Net class to write and execute data access by yourself.Code.
Use the data source control in the ASP. NET webpage
To connect to the database using the data source control, perform the following operations:
Determine the type of the data source control. For example, the objectdatasource control uses a middle-layer business object to retrieve and modify data, while the sqldatasource control allows you to provide a connection between data sources and SQL statements to retrieve and modify data.
UseSqldatasourceDetermine the requiredProgram. The provider is a class that communicates with a specific type of database. The default value is the system. Data. sqlclient provider, which connects to the Microsoft SQL Server database.
Add the data source control to the page and set its Data Access properties. For example,ObjectdatasourceThe control requires the Business Object Type of the middle layer and one or more methods for querying or modifying data.SqldatasourceThe control requires a connection string containing the information required by the provider to open a specific database, and one or more SQL commands used to query or modify data.
You can specify the provider and connection informationSqldatasourceYou can also define the provider and connection string information in the web. config file of the Web application. After storing the connection information in the web. config file, you can reuse the information to process multiple data control instances. In addition, it helps you use the access control list (ACL) and encryption to protect the connection string information. For more information, see "connection string" after this topic ".
Providers
The ADO. net provider can communicate with a specific type of database or data storage area. For example, one provider may be able to communicate with the SQL Server database, while the other provider may be able to communicate with the Oracle database .. Net Framework contains the following providers:
System. Data. sqlclientThe. NET Framework data provider used in the namespace for SQL Server. This provider isSqldatasourceControl default provider; if you are usingSqldatasourceControl to connect to the SQL Server database, you do not need to explicitly specify the provider.
The. NET Framework data provider used in the system. Data. oledb namespace for ole db.
The. NET framework data provider used for odbc in the system. Data. ODBC namespace.
The. NET Framework data provider for Oracle in the system. Data. oracleclient namespace.
You can specify the provider in the web. config file as part of the connection string or as the property of a single data source control on the page.
Connection string
The connection string provides relevant information, which is required when the provider communicates with a specific database. You can store the connection string in the web. config file and reference the configuration items in the data source control.
The connection string usually provides the location of the server or database server, the specific database to be used, and the authentication information, depending on the provider. Similar to the provider, you can specify the connection string in the web. config file or the attribute of a single data source control on the page.
Connect to string Storage
The best practice is to place the connection string in the web. config file. In the <configuration> element, you can create<Connectionstrings>And place the connection string in it, as shown in the following example:
Copy
<Connectionstrings> <Add name = "northindconnectionstring" connectionstring = "Server = mydataserver; Integrated Security = sspi; database = northwind;" providername = "system. data. sqlclient "/> </connectionstrings>
In this example, both the name and the provider are provided. Any data source control on any page of the application can reference this connection string. The advantage of storing the connection string information in the web. config file is that you can easily change the server name, database, or authentication information without editing individual webpages. In addition, you can use encryption to protect the connection string. For more information, see How to: ensure the security of the connection string when using the data source control.
Data Source Control
Data source controls provide data services (including data retrieval and modification) that can be used by other data-bound web server controls (such as the gridview, formview, and detailsview controls. Data source controls (suchSqldatasourceWill encapsulate all the elements required to connect to the database to retrieve or manipulate data (providers, connection strings, and queries ). For exampleSqldatasourceControl is configured to connect to the database and read all records from the MERs table:
Copy
<Asp: sqldatasource id = "sqldatasource1" runat = "server" selectcommand = "select * from MERs" connectionstring = "<% $ connectionstrings: northwindconnectionstring %>"/>
In this example, the program name and connection string are stored in the web. config file, and the SQL query configuration is the properties of the data source control.
Use code to execute Data Access
If the data source control is not suitable for applications, you can use the ADO. Net class to encode the data access. If the data source control cannot meet your complex requirements, or if you want to create a separate component for data access outside the webpage, You can encode the data access by yourself.
See Task
How to: Use the sqldatasource control to connect to the Access Database
How to: Use the sqldatasource control to connect to the ODBC database
How to: bind data to a templated Control
Concept
ASP. NET data access Overview