Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Data;
Using System.Data.SqlClient;
Using System.Configuration;
namespace Mybookshop.dal {public static class DBHelper {//sql database connection, note: Mybookshop is a connection string that needs to be changed according to the actual situation of the project
private static SqlConnection connection; public static SqlConnection Connection {get {string connectionString = configurationmanager.connectionstrings["Mybookshop"].
ConnectionString;
if (connection = = null) {connection = new SqlConnection (connectionString); Connection.
Open (); else if (connection. state = = System.Data.ConnectionState.Closed) {connection.
Open (); else if (connection. state = = System.Data.ConnectionState.Broken) {connection.
Close (); Connection.
Open ();
return connection;
}///execute the increment, delete, and modify operation without parameter data records, and return the result value after execution public static int ExecuteCommand (string safesql) {
SqlCommand cmd = new SqlCommand (Safesql, Connection); int result = cmd.
ExecuteNonQuery ();
return result; }//execute the increment, delete, and change operation of the data record with parameter, return the result value after execution public static int ExecuteCommand (String sql, params sqlparameter[] Value
s) {SqlCommand cmd = new SqlCommand (sql, Connection); Cmd.
Parameters.addrange (values); return CMD.
ExecuteNonQuery ();
The first row of the result set after executing without a parameter SQL statement is returned, but the result must be the int public static int getscalar (string safesql) {
SqlCommand cmd = new SqlCommand (Safesql, Connection); int result = Convert.ToInt32 (cmd.
ExecuteScalar ());
return result; //Returns the first row of the result set after executing with a parameter SQL statement, but the result must be int public static int getscalar (String sql, params sqlparameter[] values) {SqlCommand cmd = new SqlCommand (SQL,
Connection); Cmd.
Parameters.addrange (values); int result = Convert.ToInt32 (cmd.
ExecuteScalar ());
return result; //Returns the reader public static SqlDataReader Getreader (String safesql) {sqlcom After executing the SQL statement with no parameters
Mand cmd = new SqlCommand (Safesql, Connection); SqlDataReader reader = cmd.
ExecuteReader ();
return reader;
//Returns the reader public static SqlDataReader getreader (String sql, params sqlparameter[] values) after executing the SQL statement with parameters
{SqlCommand cmd = new SqlCommand (sql, Connection); Cmd.
Parameters.addrange (values); SqlDataReader reader = cmd.
ExecuteReader ();
return reader; //Returns the result data table after executing the SQL statement without parameters public static DataTable GetDataSet (String safesql) {DataSet ds = new DAtaset ();
SqlCommand cmd = new SqlCommand (Safesql, Connection);
SqlDataAdapter da = new SqlDataAdapter (cmd); Da.
Fill (DS); Return DS.
Tables[0];
//Returns the result data table after execution with a parameter SQL statement public static DataTable GetDataSet (String sql, params sqlparameter[] values)
{DataSet ds = new DataSet ();
SqlCommand cmd = new SqlCommand (sql, Connection); Cmd.
Parameters.addrange (values);
SqlDataAdapter da = new SqlDataAdapter (cmd); Da.
Fill (DS); Return DS.
Tables[0];
}
}
}