) Asp.net class used to obtain the IP address information of the access user

Source: Internet
Author: User

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 ();
}
}

Related Article

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.