Query the log list by condition

Source: Internet
Author: User

Input conditions: 1. error level; 2. Start time; 3. End Time.

Paging style: 1. Current page number; 2. Number of rows on one page.

Step 1: Obtain the SQL statement conditions:

A-> "log table" is connected to "User table" on the user _ id field on the left:

From sys_log SL left join mem_user mu on SL. user_id = mu. ID

B-> condition:
Where SL. ID is not null

1. error level;

SQL. append ("and SL. log_level = @ log_level ");
Sqlparameterlist. Add (New sqlparameter ("@ log_level", condition. loglevel ));

2. Start time;

SQL. append ("and SL. record_time> = '" + condition. begindate. value. tostring ("yyyy-mm-dd") + "00:00:00 '");

3. End Time

SQL. append ("and SL. record_time <= '" + condition. enddate. value. tostring ("yyyy-mm-dd") + "23:59:59 '");

Step 2: obtain the total number of records under this condition: (list <sqlparameter> sqlparameterlist)

String sqlcount = "select count (*)" + SQL. tostring ();

Step 3: obtain the additional paging conditions under this condition (list <sqlparameter> sqlparameterlist must be backed up and cloned at this time, otherwise the "Object occupied" error will be run)

A-> obtain the spliced field: Select SL. *, Mu. Name as user_name

B-> set the row number field: row_number () over (order by SL. record_time) as rowindex (from sys_log SL left join mem_user mu on SL. user_id = mu. ID)

C-> row number range condition in paging condition: Where temptb. rowindex between "+ Page. beginpageindex +" and "+ Page. endpageindex

Step 4: store qualified records into datatable -- "list

Datatable ------- "(datarow) ------------" (logdetail) --------------- "list

Datatable dt = query result;

List <model. log. logdetail> loglist = new list <model. log. logdetail> ();
Foreach (datarow DR in DT. Rows)
{
Model. log. logdetail log = new model. log. logdetail ();
Log. Level = (model. log. loglevel) convert. toint32 (Dr ["log_level"]);
Log. Source = convert. tostring (Dr ["Source"]);
Log. type = (model. log. logtype) convert. toint32 (Dr ["log_level"]);
Log. content = convert. tostring (Dr ["content"]);
Log. recordtime = convert. todatetime (Dr ["record_time"]);
Log. userid = convert. toint32 (Dr ["user_id"]);
Log. Username = convert. tostring (Dr ["user_name"]);
Loglist. Add (log );
}

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.