1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingSystem.Data.SqlClient;7 usingSystem.Data;8 9 namespaceMyschool.dalTen { One Public classDBHelper A { - //Private CONST String conn = "Data source=.;i Nitial Catalog=myschool; User Id=gaofei; password=111111 "; - Private Const stringconn ="Data Source=.;i Nitial Catalog=myschool; User Id=sa; Password=sa"; the Private StaticSqlConnection connection =NewSqlConnection (conn); - - - /// <summary> + ///reading Data - /// </summary> + /// <returns></returns> A Public StaticSqlDataReader Reader (stringsql) at { - Try - { - connection. Open (); -SqlCommand Comm =NewSqlCommand (SQL, connection); - returnComm. ExecuteReader (commandbehavior.closeconnection); in } - Catch(Exception ex) to { + Throw; - } the } * $ /// <summary>Panax Notoginseng ///Delete and change data - /// </summary> the /// <param name= "SQL" ></param> + /// <returns></returns> A Public Static intExecuteNonQuery (stringsql) the { + Try - { $Connection. Open ();//Open a database connection $SqlCommand Comm =NewSqlCommand (SQL, connection); - returnComm. ExecuteNonQuery (); - } the Catch(Exception ex) - {Wuyi Throw; the } - finally Wu { - connection. Close (); About } $ } - /// <summary> - ///returns a single value - /// </summary> A /// <param name= "SQL" ></param> + /// <returns></returns> the Public Static ObjectExecuteScalar (stringsql) - { $ Try the { theConnection. Open ();//Open a database connection theSqlCommand Comm =NewSqlCommand (SQL, connection); the returnComm. ExecuteScalar (); - in } the Catch(Exception) the { About Throw; the } the finally the { + connection. Close (); - } the }Bayi /// <summary> the ///Return Data Set the /// </summary> - /// <param name= "SQL" ></param> - /// <param name= "TableName" ></param> the /// <returns></returns> the Public StaticDataSet Fill (stringSqlstringtableName) the { the Try - { theConnection. Open ();//Open Connection the //To create a data adapter object theSqlDataAdapter SDA =NewSqlDataAdapter (SQL, connection);94 //Create a data set theDataSet ds =NewDataSet (); theSda. Fill (ds, TableName);//Populating data sets the returnds;98 } About Catch(Exception ex) - {101 Throw;102 //throws an exception that appears103 //throw new Exception (e.message);104 } the finally106 {107 connection. Close ();108 }109 } the }111}
C#_JDBC Connection Database