Reposted to favorites. The author is unknown.
Using system;
Using system. Data;
Using system. Data. sqlclient;
Using system. text;
///
/// Class for retrieving IP data
///
Public class ipcontrol
{
// Constant is used to represent the variable name used in the T-SQL statement
Private const string parm_ip_address = "@ IPaddress ";
Private const string parm_ip_src = "@ ipsrc ";
Private const string parm_ip_datetime = "@ ipdatetime ";
// T-SQL statement
Private const string SQL _insert_ipstat = "insert into ipstat values (@ IPaddress, @ ipsrc, @ ipdatetime )";
Private const string SQL _delete_ipstat = "delete from ipstat where datediff (D, ip_datetime, getdate ()> 30"; // only keep data for one month
Private const string SQL _select_total = "select count (*) from ipstat ";
Private const string SQL _select_today = "select count (*) from ipstat where datediff (D, ip_datetime, getdate () = 0 ";
Private const string SQL _select_yesterday = "select count (*) from ipstat where datediff (D, ip_datetime, getdate () = 1 ";
Private const string SQL _select_month = "select count (*) from ipstat where datediff (D, ip_datetime, getdate () <30 and datediff (mm, ip_datetime, getdate ()) = 0 ";
Public ipcontrol ()
{
}
///
/// Save IP address data to the database
///
IP address
///
IP Source
///
IP Access time
Public void addip (string IPaddress, string ipsrc, datetime ipdatetime)
{
// Construct the connection statement string
Stringbuilder strsql = new stringbuilder ();
// Create a parameter indicating the QQ number
Sqlparameter [] parms = new sqlparameter [] {New sqlparameter (parm_ip_address, sqldbtype. nvarchar, 20 ),
New sqlparameter (parm_ip_src, sqldbtype. nvarchar, 80 ),
New sqlparameter (parm_ip_datetime, sqldbtype. datetime )};
Sqlcommand cmd = new sqlcommand ();
// Assign values to parameters in sequence and add them to execution statements.
Parms [0]. value = IPaddress;
Parms [1]. value = ipsrc;
Parms [2]. value = ipdatetime;
Foreach (sqlparameter parm in parms)
Cmd. Parameters. Add (parm );
// Defines the storage range of object Resources. Once the using range ends, the resources occupied by the other party are released.
Using (sqlconnection conn = new sqlconnection (sqlhelper. connectionstringlocaltransaction ))
{
// Load the insert statement in the execution string
Strsql. append (SQL _insert_ipstat );
Conn. open ();
// Set sqlcommand attributes
Cmd. Connection = conn;
Cmd. commandtype = commandtype. text;
Cmd. commandtext = strsql. tostring ();
// Execute the sqlcommand command
Cmd. executenonquery ();
Cmd. Parameters. Clear ();
// If the execution is successful, true is returned; otherwise, false is returned.
}
}
Public String gettotal ()
{
// Call sqlhelper to access the component and return the value in the first column of the First row
Object COUNT = sqlhelper. executescalar (sqlhelper. connectionstringlocaltransaction, commandtype. Text, SQL _select_total, null );
// Return the statistical result
Return count. tostring ();
}
Public String gettoday ()
{
// Call sqlhelper to access the component and return the value in the first column of the First row
Object COUNT = sqlhelper. executescalar (sqlhelper. connectionstringlocaltransaction, commandtype. Text, SQL _select_today, null );
// Return the statistical result
Return count. tostring ();
}
Public String getyesterday ()
{
// Call sqlhelper to access the component and return the value in the first column of the First row
Object COUNT = sqlhelper. executescalar (sqlhelper. connectionstringlocaltransaction, commandtype. Text, SQL _select_yesterday, null );
// Return the statistical result
Return count. tostring ();
}
Public String getmonth ()
{
// Call sqlhelper to access the component and return the value in the first column of the First row
Object COUNT = sqlhelper. executescalar (sqlhelper. connectionstringlocaltransaction, commandtype. Text, SQL _select_month, null );
// Return the statistical result
Return count. tostring ();
}
}