Microsoft Published data Access Application Block's use code _ Practical Tips
Source: Internet
Author: User
To facilitate access to data, Microsoft itself encapsulates a data access module, Application Block. Through it, the amount of coding we use to access the database has been greatly reduced. Such code is both efficient and reduces the likelihood of errors, and its benefits are visible. Here are two examples to compare
1. Use general SQL statements for control binding, general code as follows:
1//create the connection and SQL to be executed
2string strconntxt = "server= (local);D atabase=northwind;integrated security=true;";
3string strSQL = "SELECT * FROM Products WHERE CategoryID = 1"
4
5//create and open the Connection object
6SqlConnection objconn = new SqlConnection (Strconntxt);
7objconn.open ();
8
9//create the Connamd Object
10SqlCommand objcmd = new SqlCommand (strSQL, objconn);
11objcmd.commandtype = CommandType.Text;
12
13//databind the DataGrid by calling the ExecuteReader () method
14datagrid1.datasource = Objcmd.executereader ();
15datagrid1.databind ();
16
17//close the connection
18objconn.close (); If you use Microsoft's encapsulated data Access Application Block, the main SqlHelper class, the code is as follows:
1//create the connection string and SQL to is executed
2string strSQL = "SELECT * from the products where CategoryID = 1";
3string strconntxt = "server= (local);D atabase=northwind;integrated security=true;";
4
5datagrid1.datasource = Sqlhelper.executereader (Strconntxt, CommandType.Text, strSQL);
6datagrid1.databind ();
2. Invoke stored procedures for control binding
The general code is as follows:
1//open a connection to Northwind
2SqlConnection objconn = new SqlConnection ("server= (local);D atabase=northwind;integrated security=true;");
3objconn.open ();
4
5//create the stored procedure Command object
6SqlCommand objcmd = new SqlCommand ("Getproductscategory", objconn);
7objcmd.commandtype = CommandType.StoredProcedure;
8
9//create the Parameter object for the stored procedure parameter
10objcmd.parameter.add ("@CategoryID", SqlDbType.Int);
11objcmd.parameter["@CategoryID"]. Value = 1;
12
13//create our DataAdapter and DataSet objects
14SqlDataAdapter Objda = new SqlDataAdapter (objcmd);
15DataSet objDS = new DataSet ("Category_results");
16
17//fill the DataSet
18objda.fill (OBJDS);
19
20//databind the DataGrid
21datagrid1.datasource = objDS;
22datagrid1.databind ();
23
24//close Connection
25objconn.close (); If you use Microsoft's encapsulated data Access Application Block, the main SqlHelper class, the code is as follows:
1string strconn = "server= (local);D atabase=northwind;integrated security=true;";
2DataSet objDS = SqlHelper.ExecuteDataset (strconn, CommandType.StoredProcedure, "getProductsByCategory", new SqlParameter ("@CategoryID", 1));
3
4datagrid1.datasource = objDS;
5datagrid1.databind ();
Data Access Application Block, with its encapsulated source code and Help files, we can also make changes based on the requirements of the project and compile into a DLL to introduce the project to facilitate the development of the project. The download address is as follows:
Http://download.microsoft.com/download/VisualStudioNET/daabref/RTM/NT5/EN-US/DataAccessApplicationBlock.msi
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.