asp: share a tool class that operates a SQL Server database

Source: Internet
Author: User

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Collections;6 usingSystem.Data.SqlClient;7 8  Public classDatabasehelper9 {Ten     Private stringConnectionString =Constants.connectionurl; One      A     //Hidden constructors; -     PrivateDatabasehelper () -     { the     } -  -     PrivateDatabasehelper (stringconnectionString) -     { +          This. connectionString =connectionString; -     } +  A     //default parameter connection database; at      PublicSqlConnection getconnection () -     { -SqlConnection connection =NewSqlConnection ( This. connectionString); -         returnconnection; -     } -  in     //Specifies the connection string to connect to the database; -      PublicSqlConnection getconnection (stringconnectionString) to     { +SqlConnection connection =NewSqlConnection (connectionString); -         returnconnection; the     } *  $     //executes the SQL SELECT query statement, returning the query results in JSON format;Panax Notoginseng      Public stringQuery (SqlConnection connection,stringSQLStatement) { -         stringstr ="["; the  + connection. Open (); ASqlCommand Command =connection. CreateCommand (); theCommand.commandtext =SQLStatement; +SqlDataReader DataReader =command. ExecuteReader (); -         inti =0; $ArrayList ArrayList =NewArrayList (); $          for(i =0; i < Datareader.fieldcount; i++) -         { - Arraylist.add (Datareader.getname (i)); the         } -          while(Datareader.read ())Wuyi         {  thestr + ="{"; -              for(i =0; i < Datareader.fieldcount; i++) Wu             { -str + = Arraylist[i]. ToString () +":"+Datareader[i]. ToString (); About                 if(! (i = = Datareader.fieldcount-1)) $                 { -str + =","; -                 } -             } Astr + ="},"; +         } the datareader.close (); - connection. Close (); $         intindex = str. LastIndexOf (","); thestr =Str. Remove (index); thestr + ="]"; the         returnstr; the     } -  in     //Execute SQL INSERT statement; the      Public voidInsert (SqlConnection connection,stringSQLStatement) the     { About connection. Open (); theSqlCommand Command =connection. CreateCommand (); theCommand.commandtext =SQLStatement; the command. ExecuteNonQuery (); + connection. Close (); -     } the Bayi     //Execute SQL UPDATE statement; the      Public voidUpdate (SqlConnection connection,stringSQLStatement) the     { - connection. Open (); -SqlCommand Command =connection. CreateCommand (); theCommand.commandtext =SQLStatement; the command. ExecuteNonQuery (); the connection. Close (); the     } -  the     //executes the SQL DELETE statement; the      Public voidDelete (SqlConnection connection,stringSQLStatement) the     {94 connection. Open (); theSqlCommand Command =connection. CreateCommand (); theCommand.commandtext =SQLStatement; the command. ExecuteNonQuery ();98 connection. Close (); About     } - 101     //returns the instance object of the default argument;102      Public Staticdatabasehelper getinstance ()103     {104         return NewDatabasehelper (); the     }106 107     //returns an instance object of the specified argument;108      Public StaticDatabasehelper getinstance (stringconnectionString)109     { the         return NewDatabasehelper (connectionString);111     } the}

asp: share a tool class that operates a SQL Server database

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.