S1总复习(6)北大青鸟管理系统
数据库操作
添加几条数据
主窗口
界面
代码
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;
namespace db11071202北大青鸟资源管理系统
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 占用资源ToolStripMenuItem_Click(object sender, EventArgs e)
{
FrmTakeUp frmTakeUp = new FrmTakeUp();
frmTakeUp.MdiParent = this;
frmTakeUp.Show();
}
private void 资源查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
FrmCheck frmCheck = new FrmCheck();
frmCheck.MdiParent = this;
frmCheck.Show();
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
添加窗口
界面
代码
using System;
using System.Windows.Forms;
namespace db11071202北大青鸟资源管理系统
{
public partial class FrmTakeUp : Form
{
public FrmTakeUp()
{
InitializeComponent();
}
private void FrmTakeUp_Load(object sender, EventArgs e)
{
cbRescameLoad();
}
private void cbRescameLoad()
{
//组合框
//添加数据
cbRescame.Items.Add("机房一");
cbRescame.Items.Add("机房二");
cbRescame.Items.Add("机房三");
cbRescame.Items.Add("机房四");
cbRescame.Items.Add("教室一");
cbRescame.Items.Add("教室二");
cbRescame.Items.Add("教室三");
cbRescame.Items.Add("教室四");
//设置默认下标
cbRescame.SelectedIndex = 0;
}
private void btnAdd_Click(object sender, EventArgs e)
{
//调用帮手
DbHelper helper = new DbHelper();
//获取值
//文本框
String className = txtClassName.Text;
String courseName = txtCourseName.Text;
String instructorName = txtInstructorName.Text;
String teacherName = txtTeacherName.Text;
//组合框
String cbRescameText = cbRescame.Text;
//时间控件
DateTime dtTime = dtpTakeDate.Value;
String dateString = dtTime.ToShortDateString();
//单选框
String tagTime = rbTime(rbAM, rbAM);
//非空验证
bool b = checkTxt(className, courseName, instructorName, teacherName);
if (!b)
{
return;
}
//主逻辑 添加数据
//获取其他数据,使用的教室,日期,时间
//新建 sql 语句
String sql = String.Format("insert into ResourceInfo VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
cbRescameText, dateString, tagTime, className, courseName, teacherName, instructorName);
//让帮手干活,的返回受影响的行数
int update = helper.update(sql);
//判断受影响行数
if (update > 0 )
{
MessageBox.Show("占用成功!");
this.Close();
}
else
{
MessageBox.Show("占用失败!");
}
}
private string rbTime(RadioButton rbAm, RadioButton rbPm)
{
String temp;
if (rbAm.Checked)
{
temp = rbAm.Tag.ToString();
}
else
{
temp = rbPm.Tag.ToString();
}
return temp;
}
private bool checkTxt(string className, string courseName, string instructorName, string teacherName)
{
bool b = true;
if (className.Trim().Length == 0 || courseName.Trim().Length == 0 || instructorName.Trim().Length == 0 ||
teacherName.Trim().Length == 0)
{
MessageBox.Show("信息必须完整!");
b = false;
}
return b;
}
private void btnEmpty_Click(object sender, EventArgs e)
{
txtClassName.Text = "";
txtCourseName.Text = "";
txtInstructorName.Text = "";
txtTeacherName.Text = "";
}
}
}
查询窗口
界面
代码
using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace db11071202北大青鸟资源管理系统
{
public partial class FrmCheck : Form
{
public FrmCheck()
{
InitializeComponent();
}
private void FrmCheck_Load(object sender, EventArgs e)
{
String sql = "select * from ResourceInfo";
//窗体加载
lvLoad(sql);
}
private void lvLoad(string sql)
{
//调用帮手
DbHelper helper = new DbHelper();
//清空数据
lvData.Items.Clear();
//帮手干活
SqlDataReader reader = helper.getReader(sql);
while (reader.Read())
{
//获取数据
String rescame = reader["rescame"].ToString();
String takeDate = reader["takeDate"].ToString();
String takeTime = reader["takeTime"].ToString();
String className = reader["ClassName"].ToString();
String courseName = reader["courseName"].ToString();
String teacherName = reader["teacherName"].ToString();
String instructorName = reader["instructorName"].ToString();
//新建项
ListViewItem item = new ListViewItem();
//添加项
item.Text = rescame;
//添加子项
item.SubItems.Add(takeDate);
item.SubItems.Add(takeTime);
item.SubItems.Add(className);
item.SubItems.Add(courseName);
item.SubItems.Add(teacherName);
item.SubItems.Add(instructorName);
//让lv 的Itmes集合 添加进去
lvData.Items.Add(item);
}
}
private void btnQuery_Click(object sender, EventArgs e)
{
//获取文本框的值
String txtDateText = txtDate.Text;
//新建 sql 语句
String sql = String.Format("select * from ResourceInfo where takeDate = '{0}'",txtDateText);
if (txtDateText.Length == 0)
{
sql = "select * from ResourceInfo";
}
lvLoad(sql);
}
}
}