ASP. NET database connection class ClassConn and oledb database connection method

Source: Internet
Author: User

ASP. NET database connection class: ClassConn

 
 
  1. UsingSystem;
  2. UsingSystem. Collections;
  3. UsingSystem. ComponentModel;
  4. UsingSystem. Data;
  5. UsingSystem. Data. SqlClient;
  6. UsingSystem. Drawing;
  7. UsingSystem. Web;
  8. UsingSystem. Web. SessionState;
  9. UsingSystem. Web. UI;
  10. UsingSystem. Web. UI. HtmlControls;
  11. UsingSystem. Web. UI. WebControls;
  12.  
  13. NamespaceZz
  14. {
  15. /// <Summary> 
  16. /// Summary of ClassConn. 
  17. /// </Summary> 
  18. Public ClassConn// ASP. NET database connection class 
  19. {
  20. PublicConn ()
  21. {
  22. // 
  23. // TODO: add the constructor logic here 
  24. // 
  25. }
  26. PublicSqlConnection connstr;// Connection string 
  27. Public StringGetconnstr ()// Obtain the connection string 
  28. {
  29. StringConstr;
  30. Constr = System. Configuration. ConfigurationSettings. receivettings ["Connstring"];
  31. ReturnConstr;
  32. }
  33. Public VoidOpen ()// Open the database 
  34. {
  35. StringConstr;
  36. Constr = getconnstr ();
  37. Connstr =NewSqlConnection (constr );
  38. Connstr. Open ();
  39. }
  40. Public VoidClose ()// Close the database 
  41. {
  42. Connstr. Dispose ();
  43. Connstr. Close ();
  44. }
  45. Public VoidExecsql (StringSQL)// Execute the SQL statement 
  46. {
  47. Open ();
  48. SqlCommand cmd =NewSqlCommand (SQL, connstr );
  49. Cmd. ExecuteNonQuery ();
  50. Close ();
  51. }
  52. PublicDataSet dataset (StringSQL)// Return the DataSet object 
  53. {
  54. Open ();
  55. SqlDataAdapter rs =NewSqlDataAdapter (SQL, connstr );
  56. DataSet ds =NewDataSet ();
  57. Rs. Fill (ds );
  58. ReturnDs;
  59. }
  60. PublicDataView dataview (StringSQL)// Return the DataView object 
  61. {
  62. DataSet ds =NewDataSet ();
  63. Ds = dataset (SQL );
  64. DataView dv =NewDataView (ds. Tables [0]);
  65. ReturnDv;
  66. }
  67. PublicSqlDataReader datareader (StringSQL)// Return the DataReader object 
  68. {
  69. Open ();
  70. SqlCommand cmd =NewSqlCommand (SQL, connstr );
  71. SqlDataReader dr = cmd. ExecuteReader ();
  72. ReturnDr;
  73. }
  74. }
  75. }

2. Use the ASP. NET database connection class to connect to the oledb Database

 
 
  1. UsingSystem;
  2. UsingSystem. Collections;
  3. UsingSystem. ComponentModel;
  4. UsingSystem. Data;
  5. UsingSystem. Data. OleDb;
  6. UsingSystem. Drawing;
  7. UsingSystem. Web;
  8. UsingSystem. Web. SessionState;
  9. UsingSystem. Web. UI;
  10. UsingSystem. Web. UI. WebControls;
  11. UsingSystem. Web. UI. HtmlControls;
  12.  
  13. NamespaceXyl
  14. {
  15. /// <Summary> 
  16. /// Summary of ClassConn. 
  17. /// </Summary> 
  18. Public ClassClassConn
  19. {
  20. PublicClassConn ()
  21. {
  22. // 
  23. // TODO: add the constructor logic here 
  24. // 
  25. }
  26. PublicOleDbConnection connstr;
  27. Public StringGetconnstr ()
  28. {
  29. StringConstr;
  30. Constr ="Provider = Microsoft. Jet. OleDb.4.0; Data Source ="+ HttpRuntime. AppDomainAppPath + System. Configuration. ConfigurationSettings. deleettings ["Connstring"];
  31. ReturnConstr;
  32. }
  33. Public VoidOpenDB ()
  34. {
  35. StringConstr;
  36. Constr = getconnstr ();
  37. Connstr =NewOleDbConnection (constr );
  38. Connstr. Open ();
  39. }
  40. Public VoidCloseDB ()
  41. {
  42. Connstr. Dispose ();
  43. Connstr. Close ();
  44. }
  45. Public VoidExecsql (StringSQL)
  46. {
  47. OpenDB ();
  48. OleDbCommand cmd =NewOleDbCommand (SQL, connstr );
  49. Cmd. ExecuteNonQuery ();
  50. CloseDB ();
  51. }
  52. PublicDataSet DataSets (StringSQL)
  53. {
  54. OpenDB ();
  55. OleDbDataAdapter rs =NewOleDbDataAdapter (SQL, connstr );
  56. DataSet ds =NewDataSet ();
  57. Rs. Fill (ds );
  58. ReturnDs;
  59. }
  60. PublicDataView DataViews (StringSQL)
  61. {
  62. DataSet ds =NewDataSet ();
  63. Ds = DataSets (SQL );
  64. DataView dv =NewDataView (ds. Tables [0]);
  65. ReturnDv;
  66. }
  67. PublicOleDbDataReader MyDataReader (StringSQL)
  68. {
  69. OpenDB ();
  70. OleDbCommand myCom =NewOleDbCommand (SQL, connstr );
  71. OleDbDataReader myreader = myCom. ExecuteReader ();
  72. ReturnMyreader;
  73. }
  74. }
  75. }

The above introduces the ASP. NET database connection class ClassConn and the method for connecting to the oledb database.

  1. Implementation of ASP. NET database Driver Class: DBHelper
  2. ASP. NET database operation class written in C #
  3. ASP. NET database image storage to Sql2000
  4. Add an ASP. NET custom error handling page
  5. Application of session storage mode in ASP. NET

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.