窗体(Form)是一个窗口或对话框,是存放各种控件(包括标签、文本框、命令按钮等)的容器,可用来向客户显示信息
一个Windows程序可以包含多个窗体,
登录页面代码
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 Model;
using BLL;
using System.IO;
namespace UI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//非空判断
if (txtname.Text == "" || txtpwd.Text == "")
{
MessageBox.Show("用户信息不完整");
}
else
{
//判断是否登录(传递参数)
Model.student s = new Model.student();
s.account = txtname.Text;
s.password = txtpwd.Text;
//调用bll完成功能
stubll sbll = new stubll();
if ((int)sbll.sellog(s) == 1)
{
MessageBox.Show("登录成功");
//跳转显示信息
Class1.uaccount = txtname.Text; //记录当前登录账号
Class1.upass = txtpwd.Text;
showstudent s1 = new showstudent();
s1.Show();
}
else
{
MessageBox.Show("用户名或密码出错");
}
}
}
catch(Exception y)
{
//写入文件
StreamWriter w = new StreamWriter("log.txt", true, Encoding.UTF8);
w.WriteLine(DateTime.Now + "Message:" + y.Message);
w.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
label4.Text = DateTime.Now.ToString("yyyy年MM月dd日");
}
}
}
显示信息代码
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 BLL;
using Model;
namespace UI
{
public partial class showstudent : Form
{
public showstudent()
{
InitializeComponent();
}
private void showstudent_Load(object sender, EventArgs e)
{
//显示日期
label13.Text = "登录日期:" + DateTime.Now.ToString("yyyy年MM月dd");
//查询
Model.student s = new Model.student();
s.account = Class1.uaccount;
s.password = Class1.upass;
stubll sbll=new stubll();
label8.Text = sbll.selstu (s).Rows [0]["name"].ToString();
label9 .Text =sbll.selstu (s).Rows [0]["sex"].ToString();
label10 .Text =sbll.selstu (s).Rows [0]["hiredate"].ToString();
label11 .Text =sbll.selstu (s).Rows [0]["integral"].ToString();
label12 .Text =sbll.selstu (s).Rows [0]["content"].ToString();
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
}
}
DAl里面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data;
using System.IO;
using System.Threading;
namespace DAL
{
public class studal
{
/// <summary>
/// 判断是否登录
/// </summary>
///
public Object sellog(Model.student s )
{
try
{
string str = string.Format ("select COUNT(*) from student where account='{0}' and password='{1}'",s.account ,s.password );
return DBhelper.Scalar(str);
}
catch(Exception t )
{
throw t;
yshow(t);
}
}
private static void yshow(Exception t)
{
//写入文件
StreamWriter w = new StreamWriter("log.txt", true, Encoding.UTF8);
w.WriteLine(DateTime.Now + "Message:" + t.Message);
w.Close();
}
/// <summary>
/// 查询信息
/// </summary>
///
public DataTable selstu(Model.student s)
{
string str =string.Format ( "select * from student where account='{0}' and password='{1}'",s.account,s.password );
return DBhelper.GetTable(str);
}
}
}