首页 > 技术文章 > C#_会员管理系统:开发四(日志查看)

start-from-scratch 2016-04-24 03:04 原文

新建一个日志查看窗体:

日志需要的登录时间和登录状态信息由用户刚登录程序时就提供,所以在登录窗体(VIPLogin.cs)中添加代码:

1         //定义一个全局变量 Uid;
2         //用于获取登录成功后的用户名
3         public static string uid;
4         //定义一个全局变量 time;
5         //用于获取用户登录时的时间
6         public static DateTime time;
7         //定义一个全局变量situation
8         //用于获取用户的登录状态
9         public static string situation;    
 1                             //如果 文本框中输入的密码 ==数据库中的密码
 2                             if (pwd == txtPwd.Text)
 3                             {
 4                                 uid = txtName.Text;
 5                                 time = DateTime.Now;
 6                                 situation = "登录";
 7                                 //说明在该账户下 密码正确, 系统登录成功
 8                                 MessageBox.Show("登录成功,正在进入主界面......");
 9                                 //***************新增代码***************
10                                 //创建新的会员资料管理界面窗体并显示,同时把登录界面隐藏
11                                 //VIPManager vm=new VIPManager();
12                                 VIPMain vmain = new VIPMain();
13                                 vmain.Show();
14                                 this.Hide();
15                                 //***************新增代码***************
16                             }

登录窗体(VIPLogin.cs)全部详细代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Configuration;
  5 using System.Data;
  6 using System.Drawing;
  7 using System.Linq;
  8 using System.Text;
  9 using System.Threading.Tasks;
 10 using System.Windows.Forms;
 11 using System.Data.SqlClient;
 12 
 13 namespace 会员管理系统
 14 {
 15     public partial class VIPLogin : Form
 16     {
 17         public VIPLogin()
 18         {
 19             InitializeComponent();
 20         }
 21         //用于连接配置文件App.config
 22         string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
 23         //定义一个全局变量 Uid;
 24         //用于获取登录成功后的用户名
 25         public static string uid;
 26         //定义一个全局变量 time;
 27         //用于获取用户登录时的时间
 28         public static DateTime time;
 29         //定义一个全局变量situation
 30         //用于获取用户的登录状态
 31         public static string situation;
 32 
 33         //登录按钮
 34         private void btnLogin_Click(object sender, EventArgs e)
 35         {
 36             //连接数据库语句
 37             using(SqlConnection con=new SqlConnection(connStr))
 38             {
 39                 //操作数据库语句
 40                 string sql = "select vuserpwd from vipaccount where vUserName='" + txtName.Text + "'";
 41                 using(SqlCommand cmd=new SqlCommand(sql,con))
 42                 {
 43                     //打开数据库
 44                     con.Open();
 45                     //使用 SqlDataReader 来 读取数据库
 46                     using (SqlDataReader sdr = cmd.ExecuteReader())
 47                     {
 48                         //SqlDataReader 在数据库中为 从第1条数据开始 一条一条往下读
 49                         if (sdr.Read()) //如果读取账户成功(文本框中的用户名在数据库中存在)
 50                         {
 51                             //则将第1条 密码 赋给 字符串pwd  ,并且依次往后读取 所有的密码
 52                             //Trim()方法为移除字符串前后的空白
 53                             string pwd = sdr.GetString(0).Trim();
 54                             //如果 文本框中输入的密码 ==数据库中的密码
 55                             if (pwd == txtPwd.Text)
 56                             {
 57                                 uid = txtName.Text;
 58                                 time = DateTime.Now;
 59                                 situation = "登录";
 60                                 //说明在该账户下 密码正确, 系统登录成功
 61                                 MessageBox.Show("登录成功,正在进入主界面......");
 62                                 //***************新增代码***************
 63                                 VIPLog vl = new VIPLog();
 64                                 //添加当前的用户信息到日志中
 65                                 vl.AddMsg();
 66                                 //退出程序
 67                                 //创建新的会员资料管理界面窗体并显示,同时把登录界面隐藏
 68                                 //VIPManager vm=new VIPManager();
 69                                 VIPMain vmain = new VIPMain();
 70                                 vmain.Show();
 71                                 this.Hide();
 72                                 //***************新增代码***************
 73                             }
 74                             else
 75                             {
 76                                 //密码错误
 77                                 MessageBox.Show("密码错误,请重新输入");
 78                                 txtPwd.Text = "";
 79                             }
 80                         }
 81                         else
 82                         {
 83                             //用户名错误
 84                             MessageBox.Show("用户名错误,请重新输入!");
 85                             txtName.Text = "";
 86                         }
 87                     }
 88                 }
 89             }
 90         }
 91         
 92         //设置快捷键
 93         private void VIPLogin_KeyDown(object sender, KeyEventArgs e)
 94         {
 95             //如果按下ESC键
 96             if (e.KeyCode == Keys.Escape)
 97             {
 98                 //关闭窗体
 99                 this.Close();
100             }
101             //如果按下F5键
102             else if (e.KeyCode == Keys.F5)
103             {
104                 //调用登录按钮单击事件
105                 this.btnLogin_Click(null,null);
106             }
107         }
108 
109         //关闭
110         private void btnClose_Click(object sender, EventArgs e)
111         {
112             //彻底的退出
113             System.Environment.Exit(0);
114         }
115 
116         //当登录窗体为活动窗体时
117         private void VIPLogin_Activated(object sender, EventArgs e)
118         {
119             //设置文本框txtName获得焦点
120             txtName.Focus();
121         }
122 
123         //获取文本框txtName的快捷键
124         private void txtName_KeyUp(object sender, KeyEventArgs e)
125         {
126             if (e.KeyCode == Keys.Enter)
127             {
128                 txtPwd.Focus();
129                 txtPwd.SelectAll();
130             }
131         }
132 
133         //获取文本框txtPwd的快捷键
134         private void txtPwd_KeyUp(object sender, KeyEventArgs e)
135         {
136             if (e.KeyCode == Keys.Enter)
137             {
138                 btnLogin_Click(null, null);
139             }
140         }
141     }
142 }

 

然后为日志查看窗体(VIPLog)添加代码,详细代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.Data.SqlClient;
11 using System.Configuration;
12 
13 namespace 会员管理系统
14 {
15     public partial class VIPLog : Form
16     {
17         public VIPLog()
18         {
19             InitializeComponent();
20         }
21         //连接数据库字符串多次调用,放在外面省代码量
22         string connstr = ConfigurationManager.ConnectionStrings["str"].ToString();
23 
24         private void btnClose_Click(object sender, EventArgs e)
25         {
26             //获取当前时间
27             GetExitTime();
28             //添加当前的用户信息到日志中
29             AddMsg();
30             //退出程序
31             System.Environment.Exit(0);
32         }
33 
34         private void btnBack_Click(object sender, EventArgs e)
35         {
36             VIPMain vm = new VIPMain();
37             vm.Show();
38             this.Hide();
39         }
40 
41         private void VIPLog_Load(object sender, EventArgs e)
42         {
43             AddMsg();
44             ShowMsg();
45         }
46         //写一个窗体加载自动从数据库获取数据并显示在datagridview的方法
47         public void ShowMsg()
48         {
49             //连接数据库对象
50             SqlConnection conn = new SqlConnection(connstr);
51             string sql = "select [id] 编号,vName 名字,situation 状态,[time] 登录或退出时间,vtype 权限 from [log]";
52             //操作数据库对象
53             SqlCommand cmd = new SqlCommand(sql, conn);
54             //创建表对象
55             DataTable dt = new DataTable();
56             //创建数据库填充操作对象(语句)
57             SqlDataAdapter sda = new SqlDataAdapter(cmd);
58             //把数据填充进dt表中
59             sda.Fill(dt);
60             //指定dgvManager控件的数据源:dt
61             dgvLog.DataSource = dt;
62         }
63         //写一个窗体加载(或者退出)自动添加当前用户登录信息进数据库的方法
64         public void AddMsg()
65         {
66             //连接数据库对象
67             SqlConnection conn = new SqlConnection(connstr);
68 
69             string sql = string.Format("insert into [Log](vName,situation,time) values('{0}','{1}','{2}')",VIPLogin.uid,VIPLogin.situation,VIPLogin.time);
70             //操作数据库对象
71             SqlCommand cmd = new SqlCommand(sql,conn);
72             //打开数据库连接
73             conn.Open();
74             //执行操作数据库对象并返回受影响条数
75             int n = cmd.ExecuteNonQuery();
76             //关闭数据库连接
77             conn.Close();
78         }
79         //写一个获取当前系统时间的方法(退出时间)
80         //退出时顺手修改登录状态
81         public void GetExitTime()
82         {
83             VIPLogin.time = DateTime.Now;
84             VIPLogin.situation = "退出";
85         }
86 
87     }
88 }

为了保证每个退出操作都能记录到数据库的日志表中,为每个退出按钮添加如下代码:

1             VIPLog vipl = new VIPLog();
2             vipl.GetExitTime();
3             vipl.AddMsg();    

运行效果:

推荐阅读