C # simple movie record management system: Development 4 [log view]

Source: Internet
Author: User

Overview

In the previous issue, we implemented the User Password modification function. In this issue, we implemented the Log Viewing function (used to record and view the user's system logon and exit history ).

 

Principle

The principle is very simple, In the login form FrmLogin. cs defines three global variables for obtaining the logon username, logon status (logon or logout), and the corresponding time; then, call the global variable in the log view form and save it to SQL Server by using the SQ insert statement. Then, when you access the log to view the form, you can use the form to automatically insert data and view data.

Define three global variables:

  1. Uid (public static string Uid;) for login success and exit -- defined when the password is modified in chapter 3
  2. Logon success and exit Time (public static DateTime Time ;)
  3. Logon Situation-two types include "Logon" and "exit" (public static string Situation ;)

The Code is as follows:

// Define a global variable Uid; // obtain the username public static string Uid after successful logon; // define a global variable Time // used to obtain the user's logon Time after successful logon public static DateTime Time; // define a logon global variable to get "Logon" or "exit" public static string Situation;

After defining the global variables for logon, we need to add some code in the code that determines that the logon is successful to obtain all logon information of the user and assign it to the global variables.

// If the entered password = password in the database if (pwd = txtPwd. text) {// obtain the user ID Uid = txtName after successful login. text; // obtain the current Logon Time = DateTime. now; // obtain the current user logon status Situation = "Logon"; // indicates that the password under this account is correct and MessageBox is successfully logged on to the system. show ("the system is successfully logged on. You are redirected to the home page... "); FrmMain main = new FrmMain (); main. show (); this. hide ();}

After the code is written, let's test the program and display the current logon user name and logon time in the main interface form.

Add a label Box name: lblCurrentUser under the form of the main interface.

 

Add the following code to the main interface form class FrmMain. cs form auto loading event FrmMain_Load:

// Automatically run private void FrmMain_Load (object sender, EventArgs e) when the form is loaded) {// assign the current logon user name and logon time to the text attribute of the label // It is displayed in the label box lblCurrentUser when loading the form of the current main interface. text = "the current login user is:" + FrmLogin. uid + "" + "Logon Time:" + FrmLogin. time ;}

In this way, after a user successfully logs on to the console, the user name and logon time (for example) of the current logon user are displayed in the form of the main interface ).

Log on to the system as admin1.

 

The current user and the logon time are displayed in the form on the main interface.

 

Now, let's design our log viewing function.

First, we need a Log data table to save the user Log data.

Set the field property as follows, and set the Id to auto-increment.

 

In this way, the database is created.

 

Database Name: Log

 

Then we need to create a log query form.

First, we need to add a "log query" button name: tsbLog on the main interface ToolStrip.

 

Code under the button

private void tsbLog_Click(object sender, EventArgs e){    FrmLog log = new FrmLog();    log.Show();    this.Hide();}

Then, design the log view form.

Log Viewing form name: FrmLog. cs DataGridView name: dgvLog

Go back to the main interface and click "name: btnBack" to exit. Click "name: btnExit ".

 

Column attribute settings in the DataGridView

Number Column

Name: ID DataPropertyName: Id HeaderText: No.

 

User Column

Name: User_Name DataPropertyName: User_Name HeaderText: User

 

Condition column

Name: Situation DataPropertyName: Situation HeaderText: Condition

 

Time column

Name: Time DataPropertyName: Time HeaderText: Time

 

After the interface is set, let's write the code of the FrmLog form.

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. configuration; using System. data. sqlClient; /*************************************** ******************* Author: soFlash-blog Park http://www.cnblogs.com/longwu ** Description: FrmLog. cs is used to view logon logs ********************************* * ******************/Namespace movie record management system {public partial class FrmLog: Form {public FrmLog () {InitializeComponent () ;}// obtain the connection path static string connStr = ConfigurationManager in the configuration file using the connection string. connectionStrings ["str"]. connectionString; // private void FrmLog_Load (object sender, EventArgs e) public void FrmLog_Load (object sender, EventArgs e) {// method call // Add the current user's logon information to the DataGridView AddLog (); // display the data to DataG ShowMsg ();} public void AddLog () {string SQL = "insert into Log (User_Name, Situation, Time) values (@ User_Name, @ Situation, @ Time )"; // check whether the inserted data is null. if it is null, a prompt is displayed. // generally, this prompt is not required (for exception prompt) if (FrmLogin. uid = "" | FrmLogin. situation = "" | FrmLogin. time. toString () = "") {MessageBox. show ("data cannot be blank! "); Return;} // Insert the SqlParameter [] param = {new SqlParameter (" @ User_Name ", FrmLogin. uid), new SqlParameter ("@ Situation", FrmLogin. situation), new SqlParameter ("@ Time", FrmLogin. time)}; // use the SQL connection command to obtain the connection string SqlConnection conn = new SqlConnection (connStr) of connStr ); // use the cmd command to load the SQL query statement and the conn connection command SqlCommand cmd = new SqlCommand (SQL, conn); // open the database conn. open (); // Add all parameters to cmd. parameters. addRange (param); // assign the integer obtained by executing the cmd query to n int n = cmd. executeNonQuery (); // closes the database conn. close () ;}// write a form to load automatically obtain data from the database and display the public void ShowMsg () {string showSql = "select Id, User_Name, Situation, time from Log "; // use conn to connect the bridge to obtain the connection string SqlConnection conn = new SqlConnection (connStr ); // use the cmd command to load the SQL query statement and conn data to connect SqlCommand cmd = new SqlCommand (showSql, conn); // a new datatable object DataTable dt = new DataTable (); // use the SQL data adapter to load the cmd command SqlDataAdapter sda = new SqlDataAdapter (cmd); // put the content obtained by the data adapter into the data table sda. fill (dt); // assign the data table content to the dgvLog of the dview control. dataSource = dt;} // write a method to obtain the current system time. public void GetExitTime () {// obtain the current system exit time FrmLogin. time = DateTime. now; // assign the current "exit" string to the global variable Situation FrmLogin In the logon form. situation = "quit";} private void btnExit_Click (object sender, EventArgs e) {// call the GetExitTime () method to obtain the current exit time (); // Add the login information of the current user to the DataGridView AddLog (); // directly exit the system Application. exit ();} private void btnBack_Click (object sender, EventArgs e) {FrmMain main = new FrmMain (); main. show (); this. close ();}}}

In this way, the FrmLog form is designed, but there are many places in the system to exit the function.

Therefore, you need to obtain the current exit time (including the exit status of the exit time) in the log form and add the current exit log (username, condition, and time) to the exit button.

// Call the FrmLog form to obtain the method for exiting the current time. FrmLog = new FrmLog (); log. getExitTime (); // method call // Add the login information of the current user to the log in the DataGridView. addLog ();

The exit button for calling the method is as follows:

1. "Log on again" button on the main interface

 

2. The "Exit System" button on the main interface

 

3. The "Exit System" button on the Management Interface

Let's test the system running effect.

First, we use the account admin1 to log on to the system.

 

Go to the log view form to view the logon status of the logged-on user admin1.

 

Go back to the main interface and use the "Exit System" button on the main interface to test the function.

 

After logging out of the system, we log on again using the account admin1. The following results are displayed on the log view page (two new records are added: logout and logon again ).

 

In this way, our log viewing function is designed! In the next issue, we will learn about the user registration function of the system.

 

Add source code

MovieRecordManagementSystem04.zip

Related Recommendations [click here to view the Directory]
  • C # simple movie record management system: developer 1 [user logon]
  • C # simple movie record management system: Development 2 [add, delete, modify, and query]
  • C # simple movie record management system: Development 3 [Password modification]
  • C # simple movie record management system: developer 5 [User Registration]
  • C # simple movie record management system: Development 6 [data search]
  • C # simple movie record management system: Development 7 [User Classification]
  • C # simple movie record management system: Development 8 [data backup]
  • C # simple movie record management system: Development 9 [data recovery]
  • C # simple movie record management system: Conclusion [permission Assignment]

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.