您的位置:首页 > 编程语言 > C#

C#_会员管理系统:开发四(日志查看)

2016-04-24 03:04 537 查看
新建一个日志查看窗体:



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

//定义一个全局变量 Uid;
//用于获取登录成功后的用户名
public static string uid;
//定义一个全局变量 time;
//用于获取用户登录时的时间
public static DateTime time;
//定义一个全局变量situation
//用于获取用户的登录状态
public static string situation;


//如果 文本框中输入的密码 ==数据库中的密码
if (pwd == txtPwd.Text)
{
uid = txtName.Text;
time = DateTime.Now;
situation = "登录";
//说明在该账户下 密码正确, 系统登录成功
MessageBox.Show("登录成功,正在进入主界面......");
//***************新增代码***************
//创建新的会员资料管理界面窗体并显示,同时把登录界面隐藏
//VIPManager vm=new VIPManager();
VIPMain vmain = new VIPMain();
vmain.Show();
this.Hide();
//***************新增代码***************
}


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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 会员管理系统
{
public partial class VIPLogin : Form
{
public VIPLogin()
{
InitializeComponent();
}
//用于连接配置文件App.config
string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
//定义一个全局变量 Uid;
//用于获取登录成功后的用户名
public static string uid;
//定义一个全局变量 time;
//用于获取用户登录时的时间
public static DateTime time;
//定义一个全局变量situation
//用于获取用户的登录状态
public static string situation;

//登录按钮
private void btnLogin_Click(object sender, EventArgs e)
{
//连接数据库语句
using(SqlConnection con=new SqlConnection(connStr))
{
//操作数据库语句
string sql = "select vuserpwd from vipaccount where vUserName='" + txtName.Text + "'";
using(SqlCommand cmd=new SqlCommand(sql,con))
{
//打开数据库
con.Open();
//使用 SqlDataReader 来 读取数据库
using (SqlDataReader sdr = cmd.ExecuteReader())
{
//SqlDataReader 在数据库中为 从第1条数据开始 一条一条往下读
if (sdr.Read()) //如果读取账户成功(文本框中的用户名在数据库中存在)
{
//则将第1条 密码 赋给 字符串pwd  ,并且依次往后读取 所有的密码
//Trim()方法为移除字符串前后的空白
string pwd = sdr.GetString(0).Trim();
//如果 文本框中输入的密码 ==数据库中的密码
if (pwd == txtPwd.Text)
{
uid = txtName.Text;
time = DateTime.Now;
situation = "登录";
//说明在该账户下 密码正确, 系统登录成功
MessageBox.Show("登录成功,正在进入主界面......");
//***************新增代码***************
VIPLog vl = new VIPLog();
//添加当前的用户信息到日志中
vl.AddMsg();
//退出程序
//创建新的会员资料管理界面窗体并显示,同时把登录界面隐藏
//VIPManager vm=new VIPManager();
VIPMain vmain = new VIPMain();
vmain.Show();
this.Hide();
//***************新增代码***************
}
else
{
//密码错误
MessageBox.Show("密码错误,请重新输入");
txtPwd.Text = "";
}
}
else
{
//用户名错误
MessageBox.Show("用户名错误,请重新输入!");
txtName.Text = "";
}
}
}
}
}

//设置快捷键
private void VIPLogin_KeyDown(object sender, KeyEventArgs e)
{
//如果按下ESC键
if (e.KeyCode == Keys.Escape)
{
//关闭窗体
this.Close();
}
//如果按下F5键
else if (e.KeyCode == Keys.F5)
{
//调用登录按钮单击事件
this.btnLogin_Click(null,null);
}
}

//关闭
private void btnClose_Click(object sender, EventArgs e)
{
//彻底的退出
System.Environment.Exit(0);
}

//当登录窗体为活动窗体时
private void VIPLogin_Activated(object sender, EventArgs e)
{
//设置文本框txtName获得焦点
txtName.Focus();
}

//获取文本框txtName的快捷键
private void txtName_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
txtPwd.Focus();
txtPwd.SelectAll();
}
}

//获取文本框txtPwd的快捷键
private void txtPwd_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnLogin_Click(null, null);
}
}
}
}


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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace 会员管理系统
{
public partial class VIPLog : Form
{
public VIPLog()
{
InitializeComponent();
}
//连接数据库字符串多次调用,放在外面省代码量
string connstr = ConfigurationManager.ConnectionStrings["str"].ToString();

private void btnClose_Click(object sender, EventArgs e)
{
//获取当前时间
GetExitTime();
//添加当前的用户信息到日志中
AddMsg();
//退出程序
System.Environment.Exit(0);
}

private void btnBack_Click(object sender, EventArgs e)
{
VIPMain vm = new VIPMain();
vm.Show();
this.Hide();
}

private void VIPLog_Load(object sender, EventArgs e)
{
AddMsg();
ShowMsg();
}
//写一个窗体加载自动从数据库获取数据并显示在datagridview的方法
public void ShowMsg()
{
//连接数据库对象
SqlConnection conn = new SqlConnection(connstr);
string sql = "select [id] 编号,vName 名字,situation 状态,[time] 登录或退出时间,vtype 权限 from [log]";
//操作数据库对象
SqlCommand cmd = new SqlCommand(sql, conn);
//创建表对象
DataTable dt = new DataTable();
//创建数据库填充操作对象(语句)
SqlDataAdapter sda = new SqlDataAdapter(cmd);
//把数据填充进dt表中
sda.Fill(dt);
//指定dgvManager控件的数据源:dt
dgvLog.DataSource = dt;
}
//写一个窗体加载(或者退出)自动添加当前用户登录信息进数据库的方法
public void AddMsg()
{
//连接数据库对象
SqlConnection conn = new SqlConnection(connstr);

string sql = string.Format("insert into [Log](vName,situation,time) values('{0}','{1}','{2}')",VIPLogin.uid,VIPLogin.situation,VIPLogin.time);
//操作数据库对象
SqlCommand cmd = new SqlCommand(sql,conn);
//打开数据库连接
conn.Open();
//执行操作数据库对象并返回受影响条数
int n = cmd.ExecuteNonQuery();
//关闭数据库连接
conn.Close();
}
//写一个获取当前系统时间的方法(退出时间)
//退出时顺手修改登录状态
public void GetExitTime()
{
VIPLogin.time = DateTime.Now;
VIPLogin.situation = "退出";
}

}
}


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

VIPLog vipl = new VIPLog();
vipl.GetExitTime();
vipl.AddMsg();


运行效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: