Introduction to ASP. NET data access layer

Source: Internet
Author: User

If you do not use the ASP. NET data access layer, there will be a lot of SqlConnection, SqlCommand, SqlDataReader, Open, Close ...... These classes and methods, and the large amount of code, leave you bored, and the code writing is actually all physical, no technical content.

ASP is required for each project. NET data access layer, I do not do anything, but I made the data access layer into an independent project, there is no big purpose, the data access layer, only for data access, does not contain any logic.

The following is my improved ASP. NET data access layer. I hope more people can make better suggestions. Thank you.

 
 
  1. UsingSystem;
  2. UsingSystem. Data;
  3. UsingSystem. Data. SqlClient;
  4. UsingSystem. Configuration;
  5. UsingSystem. Web;
  6. UsingSystem. Web. Security;
  7. UsingSystem. Web. UI;
  8. UsingSystem. Web. UI. WebControls;
  9. UsingSystem. Web. UI. WebControls. WebParts;
  10. UsingSystem. Web. UI. HtmlControls;
  11. UsingSystem. Text;
  12. ///<Summary> 
  13. /// Data access layer, database operation class
  14. /// Summary> 
  15. PublicclassDatabase
  16. {
  17. PublicstaticstringConnectionStrings=ConfigurationManager.
    ConnectionStrings ["ConnectionStrings"]. ConnectionString;
  18. ///<Summary> 
  19. /// Execute an SQL statement with SqlParameter. If the SQL statement is successful, true is returned. If the SQL statement fails, false is returned.
  20. /// Summary> 
  21. ///<ParamnameParamname="SQL">SQL statement Param> 
  22. ///<ParamnameParamname="Param">SqlParameter [] parameter list Param> 
  23. ///<Returns> Returns> 
  24. PublicstaticintExecuteNonQuery (stringsql, paramsSqlParameter [] Param)
  25. // Execute an SQL statement with SqlParameter and return the number of affected rows
  26. {
  27. // HttpContext. Current. Response. Write (SQL +"<Br>");
  28. Using (SqlConnectionconn=NewSqlConnection(ConnectionStrings ))
  29. {
  30. If (conn. State! = ConnectionState. Open)
  31. Conn. Open ();
  32. SqlTransactionTran=Conn. BeginTransaction (); // start the transaction
  33. SqlCommandcmd=NewSqlCommand(SQL, conn, Tran );
  34. If (Param! = Null)
  35. Cmd. Parameters. AddRange (Param );
  36. Try
  37. {
  38. IntResult=Cmd. ExecuteNonQuery ();
  39. Tran. Commit ();
  40. Cmd. Parameters. Clear ();
  41. ReturnResult;
  42. }
  43. Catch (Exceptionerr)
  44. {
  45. HttpContext. Current. Response. Write (err. Message +"<Br>"+ Err. StackTrace );
  46. Tran. Rollback (); // roll back the transaction
  47. Return0;
  48. }
  49. }
  50. }
  51.  
  52. ///<Summary> 
  53. /// Execute the SQL statement with SqlParameter and return the DataReader
  54. /// Summary> 
  55. ///<ParamnameParamname="SQL">SQL statement Param> 
  56. ///<ParamnameParamname="Param">SqlParameter [] parameter list Param> 
  57. ///<Returns>Return SqlDataReader Returns> 
  58. PublicstaticSqlDataReaderExecuteReader (stringsql, paramsSqlParameter [] Param)
  59. // Execute the SQL statement with SqlParameter and return the DataReader
  60. {
  61. SqlConnectionconn=NewSqlConnection(ConnectionStrings );
  62. SqlCommandcmd=NewSqlCommand(SQL, conn );
  63. If (Param! = Null)
  64. Cmd. Parameters. AddRange (Param );
  65. If (conn. State! = ConnectionState. Open)
  66. Conn. Open ();
  67.  
  68. Try
  69. {
  70. SqlDataReaderdr=Cmd. ExecuteReader (CommandBehavior. CloseConnection );
  71. Cmd. Parameters. Clear ();
  72. Returndr;
  73. }
  74. Catch (Exceptionerr)
  75. {
  76. Conn. Close ();
  77. HttpContext. Current. Response. Write (err. Message +"<Br>"+ Err. StackTrace );
  78. Returnnull;
  79. }
  80. }
  81.  
  82. ///<Summary> 
  83. /// Execute the SQL statement with SqlParameter and return the DataTable
  84. /// Summary> 
  85. ///<ParamnameParamname="SQL">SQL statement Param> 
  86. ///<ParamnameParamname="Param">SqlParameter [] parameter list Param> 
  87. PublicstaticDataTableGetDataTable (stringsql, paramsSqlParameter [] Param)
  88. // Execute the SQL statement with SqlParameter and return the DataTable
  89. {
  90. SqlConnectionconn=NewSqlConnection(ConnectionStrings );
  91. DataTabledt=NewDataTable();
  92. SqlDataAdapterda=NewSqlDataAdapter(SQL, conn );
  93. If (Param! = Null)
  94. Da. SelectCommand. Parameters. AddRange (Param );
  95. SqlCommandBuildercb=NewSqlCommandBuilder(Da );
  96. Da. Fill (dt );
  97. Conn. Close ();
  98. // HttpContext. Current. Response. Write (SQL +"<Br>");
  99. Returndt;
  100. }
  101.  
  102. ///<Summary> 
  103. /// Execute an SQL statement with SqlParameter and return the data in the first column of the first row of the query result.
  104. /// Summary> 
  105. ///<ParamnameParamname="SQL">SQL statement Param> 
  106. ///<ParamnameParamname="Param">SqlParameter [] parameter list Param> 
  107. ///<Returns> Returns> 
  108. PublicstaticObjectExecuteScalar (stringsql, paramsSqlParameter [] Param)
  109. // Execute an SQL statement with SqlParameter and return the data in the first column of the first row of the query result.
  110. {
  111. // HttpContext. Current. Response. Write (SQL +"<Br>");
  112. Using (SqlConnectionconn=NewSqlConnection(ConnectionStrings ))
  113. {
  114. If (conn. State! = ConnectionState. Open)
  115. Conn. Open ();
  116. SqlTransactionTran=Conn. BeginTransaction ();
  117. SqlCommandcmd=NewSqlCommand(SQL, conn, Tran );
  118. If (Param! = Null)
  119. Cmd. Parameters. AddRange (Param );
  120. Try
  121. {
  122. ObjectResult=Cmd. ExecuteScalar ();
  123. Cmd. Parameters. Clear ();
  124. Tran. Commit ();
  125. ReturnResult;
  126. }
  127. Catch (Exceptionerr)
  128. {
  129. HttpContext. Current. Response. Write (err. Message +"<Br>"+ Err. StackTrace );
  130. Tran. Rollback ();
  131. Returnnull;
  132. }
  133. }
  134. }
  135.  
  136. ///<Summary> 
  137. /// Generate the SqlParameter Parameter
  138. /// Summary> 
  139. ///<ParamnameParamname="ParamName">Parameter Name Param> 
  140. ///<ParamnameParamname="ParamType">Parameter type Param> 
  141. ///<ParamnameParamname="ParamValue">Parameter Value Param> 
  142. PublicstaticSqlParameterGetParameter
    (StringParamName, SqlDbTypeparamType, ObjectParamValue)
  143. // Generate the SqlParameter Parameter
  144. {
  145. SqlParameterparam=NewSqlParameter(ParamName, paramType );
  146. Param. Value=ParamValue;
  147. Returnparam;
  148. }
  149. }
  1. C # Introduction to local types
  2. C # fixed pointer
  3. Analysis of C # FTP WebRequest objects
  4. C # application scenarios of the Division Method
  5. Brief Introduction to VB. NET and C #

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.