To facilitate data access, Microsoft encapsulates a data access module, that is, the Data Access Application Block. Through this module, the encoding used to access the database is greatly reduced.CodeIt is both efficient and reduces the chance of errors. The benefits are as follows:
1. Use a general SQL statement to bind controls. The general code is as follows:
1 // Create the connection and SQL to be executed
2 String Strconntxt = " Server = (local); database = northwind; Integrated Security = true; " ;
3 String Strsql = " Select * from products where categoryid = 1 "
4
5 // Create and open the connection object
6 Sqlconnection objconn = New Sqlconnection (strconntxt );
7 Objconn. open ();
8
9 // Create the connamd object
10 Sqlcommand objcmd = New Sqlcommand (strsql, objconn );
11 Objcmd. commandtype = Commandtype. text;
12
13 // Databind the DataGrid by calling the executereader () method
14 Datagrid1.datasource = Objcmd. executereader ();
15 Datagrid1.databind ();
16
17 // Close the connection
18 Objconn. Close ();
If the data access application block encapsulated by Microsoft is used, it is mainly used as the sqlhelper class. The Code is as follows: 1 // Create the connection string and SQL to be executed
2 String Strsql = " Select * from products where categoryid = 1 " ;
3 String Strconntxt = " Server = (local); database = northwind; Integrated Security = true; " ;
4
5 Datagrid1.datasource = Sqlhelper. executereader (strconntxt, commandtype. Text, strsql );
6 Datagrid1.databind ();
2. Call the stored procedure to bind controls
The general code is as follows:
1 // Open a connection to northwind
2 Sqlconnection objconn = New Sqlconnection ( " Server = (local); database = northwind; Integrated Security = true; " );
3 Objconn. open ();
4
5 // Create the Stored Procedure command object
6 Sqlcommand objcmd = New Sqlcommand ( " Getproductscategory " , Objconn );
7 Objcmd. commandtype = Commandtype. storedprocedure;
8
9 // Create the parameter object for the Stored Procedure Parameter
10 Objcmd. Parameter. Add ( " @ Categoryid " , Sqldbtype. INT );
11 Objcmd. Parameter [ " @ Categoryid " ]. Value = 1 ;
12
13 // Create our dataadapter and dataset objects
14 Sqldataadapter objda = New Sqldataadapter (objcmd );
15 Dataset objds = New Dataset ( " Category_results " );
16
17 // Fill the dataset
18 Objda. Fill (objds );
19
20 // Databind the DataGrid
21 Datagrid1.datasource = Objds;
22 Datagrid1.databind ();
23
24 // Close connection
25 Objconn. Close ();
If the data access application block encapsulated by Microsoft is used, it is mainly used as the sqlhelper class. The Code is as follows: 1 String Strconn = " Server = (local); database = northwind; Integrated Security = true; " ;
2 Dataset objds = Sqlhelper. executedataset (strconn, commandtype. storedprocedure, " Getproductsbycategory " , New Sqlparameter ( " @ Categoryid " , 1 ));
3
4 Datagrid1.datasource = Objds;
5 Datagrid1.databind ();
Data Access Application Block, with its encapsulatedSource codeAnd help files, we can also make some changes according to the project requirements and then compile them into DLL to introduce the project, so as to facilitate project development as follows:
Http://download.microsoft.com/download/VisualStudioNET/daabref/RTM/NT5/EN-US/DataAccessApplicationBlock.msi