I would like to share with you a data operation class for SQLServer, mainly related to database operations. Make appropriate changes
I would like to share with you an SQL Server data operation class, mainly related to database operations. Make appropriate changes
The Code is as follows:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Data. SqlClient;
///
/// Summary of dataOperate
///
Public class dataOperate
{
Public dataOperate ()
{
//
// TODO: add the constructor logic here
//
}
///
/// Create a database connection method
///
/// Returns the SqlConnection object.
Public static SqlConnection createCon ()
{
SqlConnection con = new SqlConnection ("server =.; database = db_message; uid = sa; pwd = ;");
Return con;
}
///
/// SQL statement execution methods include Delete, insert, and update
///
/// SQL statement to be executed
/// Returns a Boolean value indicating whether execution is successful.
Public static bool execSql (string SQL)
{
// Create a database connection
SqlConnection con = createCon ();
// Open the database connection
Con. Open ();
// Create a SqlCommand object
SqlCommand com = new SqlCommand (SQL, con );
// Determine whether the SQL statement is successfully executed
If (com. ExecuteNonQuery ()> 0)
{
Return true;
}
Else
{
Return false;
}
}
///
/// Query the data method and return a DataSet object
///
/// Executed SQL statement
/// Returned DataSet object
Public static DataSet getRows (string SQL)
{
// Create a DataSet object
DataSet ds;
// Create a database connection
SqlConnection con = createCon ();
// Open the database connection
Con. Open ();
// Create a SqlDataAdapter object
SqlDataAdapter sda = new SqlDataAdapter (SQL, con );
// Instance DataSet object
Ds = new DataSet ();
// Fill in the DataSet object
Sda. Fill (ds );
// Close the database connection
Con. Close ();
Return ds;
}
///
/// Method for querying whether data exists
///
/// SQL statement to be executed
/// Returns a Boolean value. If the data exists, True is returned. Otherwise, False is returned.
Public static bool isName (string SQL)
{
// Create a database connection
SqlConnection con = createCon ();
// Open the database connection
Con. Open ();
// Create a SqlCommand object
SqlCommand com = new SqlCommand (SQL, con );
// Determine whether the data exists and return the corresponding Boolean Value
If (Convert. ToInt32 (com. ExecuteScalar ()> 0)
{
Return true;
}
Else
{
Return false;
}
}
///
/// Return the result that requires statistics
///
/// SQL statement to be queried
/// Returns an integer variable, indicating the statistical result.
Public static int countData (string SQL)
{
// Create a database connection
SqlConnection con = createCon ();
// Open the database connection
Con. Open ();
// Create a SqlCommand object
SqlCommand com = new SqlCommand (SQL, con );
// Return the query result
Return Convert. ToInt32 (com. ExecuteScalar ());
}
///
/// Implement the user logon method, which can prevent SQL injection attacks
///
/// SQL statement used for execution
/// User Login Name
/// User Password
/// Returns a Boolean value indicating whether the logon is successful.
Public static bool enter (string SQL, string name, string pass)
{
// Create a database connection
SqlConnection con = createCon ();
// Open the database connection
Con. Open ();
// Create a SqlCommand object
SqlCommand com = new SqlCommand (SQL, con );
// Set the parameter type
Com. Parameters. Add (new SqlParameter ("@ name", SqlDbType. VarChar, 20 ));
// Set the parameter value
Com. Parameters ["@ name"]. Value = name;
Com. Parameters. Add (new SqlParameter ("@ pass", SqlDbType. VarChar, 20 ));
Com. Parameters ["@ pass"]. Value = pass;
// Determine whether execution is successful
If (Convert. ToInt32 (com. ExecuteScalar ()> 0)
{
Return true;
}
Else
{
Return false;
}
}
///
/// Data query method, which returns a SqlDataReader object
///
/// SQL statement execution Method
/// Returns a SqlDataReader object.
Public static SqlDataReader getRow (string SQL)
{
// Create a database connection
SqlConnection con = createCon ();
// Open the database connection
Con. Open ();
// Create a SqlCommand object
SqlCommand com = new SqlCommand (SQL, con );
// Obtain the SqlDataReader object returned by ExecuteReader
SqlDataReader sdr = com. ExecuteReader ();
Return sdr;
}
///
/// Set the display style of the time
///
/// Time to be displayed
/// Returns the modified time style.
Public static string strDate (DateTime str)
{
// Set the display style of the time
Return str. ToLongDateString () + str. Hour + "Hour" + str. Minute + "Minute" + str. Second + "Second ";
}
///
/// Character filtering method
///
/// String to be filtered
/// Returns the filtered string.
Public static string filtrateHtml (string str)
{
Str = str. Trim ();
Str = str. Replace ("'",""");
Str = str. Replace ("<", "<");
Str = str. Replace (">", "> ");
Str = str. Replace ("","");
Str = str. Replace ("\ n ","
");
Return str;
}
///
/// Restore the string
///
/// String to be restored
/// Returns the restored string.
Public static string resumeHtml (string str)
{
Str = str. Trim ();
Str = str. Replace (""","'");
Str = str. Replace ("<", "<");
Str = str. Replace (">", "> ");
Str = str. Replace ("","");
Str = str. Replace ("
"," \ N ");
Return str;
}
}