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.Data.Sql;
using System.Data.SqlClient;
using System.IO;
using System.Data.OleDb;
namespace login
{
public partial class login : Form
{
string connS = "Data Source=.;Initial Catalog=Student;User ID=sa;Password=123456789";//连接数据库
public login()
{
InitializeComponent();//窗体设计
}
public bool User()//判断用户名和密码是否匹配
{
using (SqlConnection conn = new SqlConnection(connS))//实例连接对象
{
conn.Open();//打开数据库连接
using (SqlCommand cmd = conn.CreateCommand())//通过连接对象创建数据库命令对象
{
cmd.CommandText = "select * from Suser where Uname='" + textBoxU.Text + "' and Upassw='" + textBoxP.Text + "'";//查询数据库的SQL语句
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
return true;
}
else
{
return false;
}
}
}
}
}
private void Form1_Load(object sender, EventArgs e)//加载
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)//时间组件
{
labeltime.Text = System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo);//获取当前时间
}
private void buttonlogin_Click(object sender, EventArgs e)//登录按钮
{
try
{
if (textBoxU.Text != "" && textBoxP.Text != "")
{
if (User())
{
//this.Hide();
Smassage fm1 = new Smassage();
fm1.GetStudentMessageEvent += new Smassage.GetStudentMessage(getSMessage);
fm1.ShowDialog();
}
else
{
MessageBox.Show("账号或密码错误!");
}
}
else
{
MessageBox.Show("用户名和密码不能为空!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public string getSMessage()
{
return textBoxU.Text + "#" + labeltime.Text;//返回用户名和时间
}
private void buttonregiste_Click(object sender, EventArgs e)//注册按钮
{
registe re1 = new registe();
re1.ShowDialog();
}
}
}
c#登录界面
最新推荐文章于 2024-06-19 14:53:18 发布