C # (. NET) Encapsulation of operations such as data access connection, query, and insertion

Source: Internet
Author: User

Using system;
Using system. Data;
Using system. Data. sqlclient;
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;

/// <Summary>
/// Summary of public
/// </Summary>
Public class publicclass
{// Define a Public Member
Public sqlconnection conn;

Public publicclass ()
{
//
// Todo: add the constructor logic here
//
}
# Region create a database connection
Public void openconn ()
{
String strconn = system. configuration. configurationmanager. etettings ["sqlconn"]. tostring ();
Conn = new sqlconnection (strconn );
If (conn. state. tostring (). tolower () = "open ")
{
// When the connection is enabled
}
Else
{
// When the connection is closed
Conn. open ();
}
}
# Endregion
# Region close and release the connection
Public void closeconn ()
{
If (conn. state. tostring (). tolower () = "open ")
{
// When the connection is enabled
Conn. Close ();
Conn. Dispose ();
}
}
# Endregion
# Region returns datareader for reading data
Public sqldatareader dataread (string SQL)
{
Openconn ();
Sqlcommand cmd = new sqlcommand (SQL, Conn );
Sqldatareader DR = cmd. executereader ();
Return Dr;
}
# Endregion
# Region returns a dataset
Public dataset mysqldataset (string SQL, string tablename)
{
Openconn ();
Sqldataadapter da;
Dataset DS = new dataset ();
DA = new sqldataadapter (SQL, Conn );
Da. Fill (DS, tablename );
Closeconn ();
Return Ds;
}
# Endregion
// Return a dataset
Public dataview mysqldatasource (string SQL)
{
Openconn ();
Sqldataadapter da;
Dataset DS = new dataset ();
DA = new sqldataadapter (SQL, Conn );
Da. Fill (DS, "Temp ");
Closeconn ();
Return Ds. Tables [0]. defaultview;
}
# Region executes an SQL operation: add, delete, and update

// Execute an SQL operation: add, delete, and update
Public void mysqlexcute (string SQL)
{
Openconn ();
Sqlcommand cmd;
Cmd = new sqlcommand (SQL, Conn );
Cmd. executenonquery ();
Cmd. Dispose ();
Closeconn ();
}
# Endregion
# Region executes an SQL operation: add, delete, and update operations. The affected rows are returned.
// Execute an SQL operation: add, delete, and update operations. The affected rows are returned.
Public int mysqlexecutenonquery (string SQL)
{
Openconn ();
Sqlcommand cmd;
Cmd = new sqlcommand (SQL, Conn );
Int flag = cmd. executenonquery ();
Return flag;
}
# Endregion

Public object mysqlexecutescalar (string SQL)
{
Openconn ();
Sqlcommand cmd;
Cmd = new sqlcommand (SQL, Conn );
Object OBJ = cmd. executescalar ();
Cmd. Dispose ();
Closeconn ();
Return OBJ;
}

/// <Summary>
/// Return the datatable object
/// </Summary>
/// <Param name = "SQL"> SQL statement </param>
/// <Returns> </returns>
Public datatable mysqldatatable (string SQL)
{
Openconn ();
Dataset DS = new dataset ();
Sqldataadapter da = new sqldataadapter (SQL, Conn );
Da. Fill (DS, "table ");
Closeconn ();
Return Ds. Tables ["table"];
}

/// <Summary>
/// Number of records returned for a dataset
/// </Summary>
/// <Param name = "SQL"> the passed SQL statement must be a statistical query. </param>
/// <Returns> </returns>
Public int mysqlrecordcount (string SQL)
{
// Note: the SQL statement must be a statistical query.
Openconn ();
Sqlcommand cmd = new sqlcommand ();
Cmd. commandtext = SQL;
Cmd. Connection = conn;
Sqldatareader Dr;
Dr = cmd. executereader ();
Int recordcount =-1;
While (dr. Read ())
{
Recordcount = int. parse (Dr [0]. tostring ());
}
Closeconn ();
Return recordcount;
}

/// <Summary>
/// Custom function Warning
/// </Summary>
/// <Param name = "str"> content in the pop-up information box </param>
Public void setalert (string Str)
{
Httpcontext. current. response. write ("<script language = 'javascript 'Type = 'text/JavaScript '> alert ('" + STR + "'); </SCRIPT> ");

}
// Return to the previous page
Public void adderro (string message)
{
Httpcontext. Current. response. Write ("<SCRIPT> alert ('" + message + "'); history. Back (-1); </SCRIPT> ");
}

// Close the window
Public void setclosewindow ()
{
Httpcontext. Current. response. Write ("<script language = 'javascript 'Type = 'text/JavaScript '> window. Close (); </SCRIPT> ");
}

/// <Summary>
/// Address jump
/// </Summary>
/// <Param name = "str"> jump address </param>
Public void setlocation (string Str)
{
Httpcontext. current. response. write ("<script language = 'javascript 'Type = 'text/JavaScript '> location ='" + STR + "'; </SCRIPT> ");
}

Public String ajaxsetalert (string Str)
{
Return "<script language = 'javascript 'Type = 'text/JavaScript '> alert ('" + STR + "'); </SCRIPT> ";
}

// Filter invalid characters
Public String filterstr (string Str)
{
STR = Str. Trim ();
STR = Str. Replace ("*","");
STR = Str. Replace ("= ","");
STR = Str. Replace ("/","");
STR = Str. Replace ("$ ","");
STR = Str. Replace ("#","");
STR = Str. Replace ("@","");
STR = Str. Replace ("&","");
Return STR;
}

// MD5 EncryptionAlgorithm
Public String MD5 (string Str)
{
Return System. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (STR, "MD5"). tolower (). substring (0, 12 );
}
Public String rndnum (INT vcodenum)
{
String vchar = ", A, B, C, D, E, F, G, H, I, J, K, L, M, n, P, Q, R, S, T, U, w, x ";
String [] vcarray = vchar. Split (New char [] {','}); // generates an array of strings
String vnum = "";
Int temp =-1;

Random Rand = new random ();

For (INT I = 1; I <vcodenum + 1; I ++)
{
If (temp! =-1)
{
Rand = new random (I * temp * unchecked (INT) datetime. Now. ticks ));
}

Int T = Rand. Next (31); // The array is generally read from 0, so 31 * RND is used here.
If (temp! =-1 & temp = T)
{
Return rndnum (vcodenum );
}
Temp = T;
Vnum + = vcarray [T];
}
Return vnum;
}
}

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.