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