本篇内容主要讲解“基于C#如何实现宿舍管理系统”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“基于C#如何实现宿舍管理系统”吧!
首先通过创建C#的Windows窗体应用程序,名字可以自行设置,框架可以选用默认的。
这是我的项目主页面,主要包括4个Label类,3个Button类,2个radioButton1,1个pictureBox1。主要的设计界面就如图所示。命名和图片大家可以自行的设置,通过对组件的Text属性进行设置,radioButton具有一个Checked属性,可以控制默认的多选框。(例如我的在用户)
主页面代码主要包括验证登录信息,通过与SQL查询来验证用户信息,以及打开对象的对话框。
首先我们写一个Login的登录方法用来判断登录,随后会跳转到别的窗口(会在下一次的教程中编写)。代码如下。
public void Login() {
//用户
if (radioButton1.Checked == true) {
DataBase DB = new DataBase();
string sql = $"select * from [User] where id='{textBox1.Text}' and password='{textBox2.Text}'" ;
IDataReader dc = DB.read(sql);
if (dc.Read())
{
Data.UID = dc["id"].ToString();
Data.UName = dc["name"].ToString();
MessageBox.Show("登录成功");
User1 user = new User1();
this.Hide();
user.ShowDialog();
this.Show();
}
else
{
MessageBox.Show("登陆失败");
}
DB.Close();
}
//管理员
if (radioButton2.Checked == true) {
DataBase DB = new DataBase();
string sql = $"select * from [Admin] where id='{textBox1.Text}' and password='{textBox2.Text}'";
IDataReader dc = DB.read(sql);
if (dc.Read())
{
MessageBox.Show("登录成功");
Admin1 admin = new Admin1();
this.Hide();
admin.ShowDialog();
this.Show();
}
else
{
MessageBox.Show("登陆失败");
}
DB.Close();
}
}
随后双击登录button,输入以下代码,用以判断空值。
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "")
{
Login();
}
else
{
MessageBox.Show("输入有空,请重新输入");
}
}
这个按钮就没啥难点直接上代码了,双击退出Button。
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
用以跳转的按钮,也是双击注册Button,同样的跳转的窗口将在下次教程中讲解。
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
随后我们的代码里还要新建一个DataBase.cs用以创建SQL连接。代码如下。
using System.Data.SqlClient;
namespace HomeWork
{
class DataBase
{
SqlConnection sc;
public SqlConnection connect() {
string str= @"Data Source=.;Initial Catalog=DormitoryDB;integrated security=true"; //位置(这个地方根据自己的需要修改)
sc = new SqlConnection(str); //连接
sc.Open(); //打开
return sc; //返回对象
}
public SqlCommand command(string sql) {
SqlCommand cmd = new SqlCommand(sql, connect());
return cmd;
}
public int Execute(string sql) //更新
{
return command(sql).ExecuteNonQuery();
}
public SqlDataReader read(string sql) //读取
{
return command(sql).ExecuteReader();
}
public void Close() {
sc.Close();
}
}
}
(1)SQL打开
代码都有了但是SQL配置还未完成。首先你得安装SQL数据库打开
(2)SQL登录
我用的是默认Windows身份验证,也可以用管理员登录。
(3)新建数据库
登录后新建一个数据库,如下。
(3)新建数据表
之后根据自己的需要新建表格。我将我的数据表展示一下。(可能不太严谨因为我的水平也有限)
还需要一个Data类用以保存用户的ID等信息,便于后面的开发。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HomeWork
{
class Data
{
public static string UID = "", UName = ""; //用户名和ID
}
}
using System;
using System.Data;
using System.Windows.Forms;
namespace HomeWork
{
public partial class Index : Form
{
public Index()
{
InitializeComponent();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "")
{
Login();
}
else
{
MessageBox.Show("输入有空,请重新输入");
}
}
//登录方法
public void Login() {
//用户
if (radioButton1.Checked == true) {
DataBase DB = new DataBase();
string sql = $"select * from [User] where id='{textBox1.Text}' and password='{textBox2.Text}'" ;
IDataReader dc = DB.read(sql);
if (dc.Read())
{
Data.UID = dc["id"].ToString();
Data.UName = dc["name"].ToString();
MessageBox.Show("登录成功");
User1 user = new User1();
this.Hide();
user.ShowDialog();
this.Show();
}
else
{
MessageBox.Show("登陆失败");
}
DB.Close();
}
//管理员
if (radioButton2.Checked == true) {
DataBase DB = new DataBase();
string sql = $"select * from [Admin] where id='{textBox1.Text}' and password='{textBox2.Text}'";
IDataReader dc = DB.read(sql);
if (dc.Read())
{
MessageBox.Show("登录成功");
Admin1 admin = new Admin1();
this.Hide();
admin.ShowDialog();
this.Show();
}
else
{
MessageBox.Show("登陆失败");
}
DB.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
register register = new register();
register.ShowDialog();
}
}
}
到此,相信大家对“基于C#如何实现宿舍管理系统”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。