C#开发环境是Visual Studio 2019,数据库环境是Microsoft SQL Server Management Studio 18.
参考书籍是胡学钢主编的《C#应用开发与实践》 本人为C#初学者
一共创建4个Form,如下图
分别是登陆界面Form1,注册界面Form2,查询界面Form3,教师界面Form4.每个界面都要添加引用using System.Data.SqlClient;
数据库一共建立了6张表(没有规范设置主码),如下图
下面详细说一下每个界面所使用的控件和所实现的功能以及代码.
登录界面Form1
控件:两个label(label1.Text=“账号”,label2.Text=“密码”).两个textBox分别用来输入账号和密码.两个radioButton用来选择是学生登陆还是教师登陆.两个button分别控制登陆和重输.linkLabel可用于跳转到注册界面.toolStripStatusLabel用来显示一下时间.
功能:用户可登陆信息系统以及注册账号
与这个界面相关联的表是表Student,表Tusers,两表的元组内容如下(其实只用到了每张表的前两列)
登陆界面的代码如下
"登陆"按钮:
private void Button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == false && radioButton2.Checked == false)//如果用户没有选择是登陆学生端还是教师端则让用户先做选择
{
MessageBox.Show("请先选择登陆人员类别!");
}
if (radioButton1.Checked)//如果选中学生端
{
string userid = textBox1.Text.Trim();//将用户输入的账号赋值给string类型的变量uesrid
string password = textBox2.Text.Trim();//如上
if (userid.Equals("") || password.Equals(""))//用户名或密码为空
{
MessageBox.Show("用户名或密码不能为空");
}
else//用户名或密码不为空
{
SqlConnection con = new SqlConnection("Data Source=.;DataBase=Test;User ID=sa;Pwd=zks123"); //数据库连接
con.Open(); //打开数据库
string sql = "select * from Student where Sno='" + userid + "'and PWD='" + password + "'";//Sql语句查询
SqlCommand cmd = new SqlCommand(sql,con);//Command命令执行对象
//cmd.CommandType = CommandType.Text;//
SqlDataReader sdr = cmd.ExecuteReader();//定义一种读取的方式
if (sdr.Read()) //从数据库中找到
{
MessageBox.Show("学生系统登录成功");
this.Hide();//Hide()方法用于隐藏该界面
查询界面Form3 form3 = new 查询界面Form3();//创建一个Form3的对象form3
form3.Show();//Show()用于显示界面,这样就跳转到学生信息界面了.
}
else
{
MessageBox.Show("用户名不存在或密码错误");
return;
}
con.Close();
}
}
if (radioButton2.Checked)//如果选中教师端
{
string userid = textBox1.Text.Trim();
string password = textBox2.Text;
if (userid.Equals("") || password.Equals(""))//用户名或密码为空
{
MessageBox.Show("用户名或密码不能为空");
}
else//用户名或密码不为空
{
SqlConnection con = new SqlConnection("Data Source=.;DataBase=Test;User ID=sa;Pwd=zks123"); //数据库连接
con.Open(); //打开数据库
string sql = "select * from TUsers where TID='" + userid + "'and TPWD='" + password + "'";
SqlCommand cmd = new SqlCommand(sql,con);执行对象
//cmd.CommandType = CommandType.Text;//
SqlDataReader sdr = cmd.ExecuteReader();//定义一种读取的方式
if (sdr.Read()) //从数据库中找到
{
MessageBox.Show("教师系统登录成功");
this.Hide();
教师界面Form4 form4 = new 教师界面Form4();
form4.Show();
}
else
{
MessageBox.Show("用户名不存在或密码错误");
return;
}
con.Close();
}
}
}
"重输"按钮:
private void Button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
}
"注册"按钮:
private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Hide();
注册界面Form2 form2 = new 注册界面Form2();
form2.Show();
}
toolStripStatusLabel1:
private void Form1_Load(object sender, EventArgs e)
{
toolStripStatusLabel1.Text ="您的开始使用时间为:"+DateTime.Now.ToLongDateString() + DateTime.Now.ToShortTimeString();
}
额外需要添加的代码,为后续功能的实现做准备:
public partial class Form1 : Form//只显示了要添加的代码
{
查询界面Form3 form3 = new 查询界面Form3();
教师界面Form4 form4 = new 教师界面Form4();
public class class1//建立一个公有类class1
{
public static int i;//该类中有一个公有静态变量i
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
class1.i = Convert.ToInt32(textBox1.Text);//把textBox1.Text的内容转化为Int32赋值给class1的i
}
}
注册界面Form2
登陆界面点击注册可跳转到注册界面
控件:button和label和textBox
功能:可实现注册功能并将信息保存到数据库中
"注册"按钮:
private void Button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
MessageBox.Show("账号不能为空!");
if (textBox2.Text == "")
MessageBox.Show("密码不能为空!");
if (textBox2.Text != textBox3.Text)
MessageBox.Show("两次输入密码不一致!");
else
{
string newid = textBox1.Text;
string newpwd = textBox2.Text;
SqlConnection con = new SqlConnection("Data Source=.;DataBase=Test;User ID=sa;Pwd=zks123"); //数据库连接
con.Open();
string sql = "Select * from Student where Sno='" + newid + "'";
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read()) //查询数据库中是否已有该账号
{
MessageBox.Show("该账号已存在,请重新注册");
}
else
{
SqlConnection conn= new SqlConnection("Data Source=LAPTOP-27QJOFOC;DataBase=Test;User ID=sa;Pwd=zks123"); //数据库连接
conn.Open();
string strsql = "insert into Student(Sno,PWD,Name,Sex,Age) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')";//实现插入功能的Sql语句
SqlCommand comm = new SqlCommand(strsql,conn);
if(Convert.ToInt32(comm.ExecuteNonQuery())>0)//判断ExecuteNonQuery方法返回的参数是否大于0,大于0表示成功
MessageBox.Show("注册成功!请登录!");
this.Hide();
Form1 form = new Form1();
form.Show();//返回登陆界面进行登陆
}
conn.Close();
}
}
}
查询界面Form3:
控件:5个groupBox显示不同的模块,深灰色为dataGridView,选择列表用的是树控件TreeView
功能:可实现点击相应信息进行查询
与该界面相关的表为表Course,表Grade,表Honor
SCS为奖学金金额,SHS为是否为三好学生
具体代码:
private void 查询界面Form3_Load(object sender, EventArgs e)
{
for(int i = 0; i < 5; i++)//为课程表的dataGridView1添加5行数据
{
dataGridView1.Rows.Add();
}
//修改每一行的头数据块为课程节数
dataGridView1.Rows[0].HeaderCell.Value = "1-2";
dataGridView1.Rows[1].HeaderCell.Value = "3-4";
dataGridView1.Rows[2].HeaderCell.Value = "-";
dataGridView1.Rows[3].HeaderCell.Value = "5-6";
dataGridView1.Rows[4].HeaderCell.Value = "7-8";
dataGridView1.Rows[0].Height =70;//设置高度
dataGridView1.Rows[1].Height = 70;
dataGridView1.Rows[3].Height = 70;
dataGridView1.Rows[4].Height = 70;
dataGridView1.Rows[1].Cells[0].Value ="数据结构";//根据数据库手动安排课程(所以Course表只是个摆设,并没有在窗体中对其操作)
dataGridView1.Rows[0].Cells[1].Value = "操作系统";
dataGridView1.Rows[3].Cells[2].Value = "C#程序开发";
dataGridView1.Rows[4].Cells[3].Value = "图形学";
dataGridView1.Rows[3].Cells[4].Value = "编译原理";
//treeView1.ContextMenuStrip = contextMenuStrip1;
TreeNode TopNode = treeView1.Nodes.Add("学生信息");//添加树控件的第一个父节点
TreeNode ParentNode1 = new TreeNode("基本信息");//创建一个子节点
TopNode.Nodes.Add(ParentNode1);//将子节点加入到父节点中
groupBox1.Visible = false;//设置每一个groupBox开始是不可见,以下都是
TreeNode TopNode1 = treeView1.Nodes.Add("荣誉管理");//父节点
TreeNode ParentNode2 = new TreeNode("奖学金管理");//子节点
TreeNode ParentNode3 = new TreeNode("三好学生管理");//子节点
TopNode1.Nodes.Add(ParentNode2);//添加
TopNode1.Nodes.Add(ParentNode3);//添加
groupBox2.Visible = false;
groupBox3.Visible = false;
TreeNode TopNode2 = treeView1.Nodes.Add("课程管理");//父
TreeNode ParentNode4 = new TreeNode("课程安排");//子
TreeNode ParentNode5= new TreeNode("课程成绩");//子
TopNode2.Nodes.Add(ParentNode4);//添加
TopNode2.Nodes.Add(ParentNode5);//添加
groupBox4.Visible = false;
groupBox5.Visible = false;
label5.Text = Form1.class1.i.ToString(); //这一步比较重要,是连接两个窗体的枢纽,将登陆界面用户所输入的账号传给label5,即显示学号
SqlConnection con = new SqlConnection("Data Source=.;DataBase=Test;User ID=sa;Pwd=zks123;");
con.Open();
string sql = "Select * from Student where Sno='" + label5.Text + "'";//根据label5去查找所有信息
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
//reader.GetOrdinal("id")是得到ID所在列的index,
//reader.GetInt32(int n)这是将第n列的数据以Int32的格式返回
//reader.GetString(int n)这是将第n列的数据以string 格式返回
label6.Text = sdr.GetString(sdr.GetOrdinal("Name"));
label7.Text = sdr.GetString(sdr.GetOrdinal("Sex"));
label8.Text = sdr.GetString(sdr.GetOrdinal("Age"));
}
con.Close();
//以下代码用于读取数据库的照片
SqlConnection conn = new SqlConnection("Data Source=.;DataBase=Test;User ID=sa;Pwd=zks123;");
conn.Open();
string sql1 = "select Picture from Student where Sno='" + label5.Text + "'";
SqlCommand cmd1 = new SqlCommand(sql, conn);
SqlDataReader reader = cmd1.ExecuteReader();
while (reader.Read())//以下是从事软件行业12年的大佬指点的代码,实现从string到byte[]的转换
{
var buffer = reader.GetValue(reader.GetOrdinal("Picture")) as byte[];
if (buffer == null) continue;//此语句不可省略
using (var ms = new MemoryStream(buffer))
{
Image img = Image.FromStream(ms);
pictureBox1.Image = img;
}
}
//读取完成
//以下为comboBox添加可选择的项
comboBox1.Items.Add("2018");
comboBox1.Items.Add("2019");
comboBox2.Items.Add("2018");
comboBox2.Items.Add("2019");
comboBox3.Items.Add("2018");
comboBox3.Items.Add("2019");
label18.Text = label5.Text;//label18为课程成绩的学号
/*string[] str = new string[] { "2018", "2019" };
comboBox2.DataSource = str;*/
}
点击运行,登录后可得到如下界面
通过树控件选择实现查询各个模块,代码如下
private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (treeView1.SelectedNode.Text == "基本信息")
{
groupBox2.Visible = false;//目的是只显示你要查询的界面
groupBox3.Visible = false;
groupBox4.Visible = false;
groupBox5.Visible = false;
groupBox1.Visible = true;
}
if(treeView1.SelectedNode.Text == "奖学金管理")
{
groupBox2.Visible = true;
groupBox1.Visible = false;
groupBox3.Visible = false;
groupBox4.Visible = false;
groupBox5.Visible = false;
}
if (treeView1.SelectedNode.Text == "三好学生管理")
{
groupBox3.Visible = true;
groupBox1.Visible = false;
groupBox2.Visible = false;
groupBox4.Visible = false;
groupBox5.Visible = false;
}
if (treeView1.SelectedNode.Text == "课程安排")
{
groupBox1.Visible = false;
groupBox2.Visible = false;
groupBox3.Visible = false;
groupBox5.Visible = false;
groupBox4.Visible = true;
}
if (treeView1.SelectedNode.Text == "课程成绩")
{
groupBox1.Visible = false;
groupBox2.Visible = false;
groupBox3.Visible = false;
groupBox4.Visible = false;
groupBox5.Visible = true;
}
}
点击基本信息,可直接根据学号显示信息
"选择照片"按钮,可实现从文件中选择照片 注意这里必须要在前面添加引用using System.IO;
private void Button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.jpg,*jpeg,*.bmp,*.ico,*.png,*.tif,*.wmf|*.jpg;*jpeg;*.bmp;*.ico;*.png;*.tif;*.wmf";
openFileDialog1.Title = "选择头像";
//判断是否选择了头像
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
}
"保存信息"代码,实际上只保存了照片
private void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;DataBase=Test;User ID=sa;Pwd=zks123");
con.Open();
FileStream fs = new FileStream(this.openFileDialog1.FileName.ToString(), FileMode.Open, FileAccess.Read);
BinaryReader BReader = new BinaryReader(fs);//创建二进制流对象
byte[] byteImage = BReader.ReadBytes((int)fs.Length);
//创建命令对象
SqlCommand sqlcmd = new SqlCommand("update Student set Picture =@Picture where Sno='"+label5.Text+"'", con);//注意要用update而不是insert
//添加参数并赋值
sqlcmd.Parameters.Add("@Picture", SqlDbType.Image).Value = byteImage;
sqlcmd.ExecuteNonQuery();//执行SQL语句
MessageBox.Show("保存成功!");
con.Close();//关闭数据库连接
}
点击奖学金管理,可查看每年的金额和类别
代码如下(comboBox1)
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox1.Text.Equals("2018" ))
{
SqlConnection con = new SqlConnection("Data Source=.;DataBase=Test;User ID=sa;Pwd=zks123;");
con.Open();
string sql1 = "Select * from Honor where Sno='" + label5.Text + "' and Year='"+2018+"'";
SqlCommand cmd = new SqlCommand(sql1,con);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
if (sdr["SCS"]==System.DBNull.Value)//判断NULL等号右边不能是NULL
{
label11.Text = "不存在相应信息!";
}
else if (sdr["SCS"]!=System.DBNull.Value)
{
label11.Text = "2018年金额为"+sdr.GetString(sdr.GetOrdinal("SCS"));
int tmp=Convert.ToInt32(sdr.GetString(sdr.GetOrdinal("SCS")));
if (tmp==600)
{
label16.Text = "三等奖学金";
}
else if (tmp == 1000)
{
label16.Text = "二等奖学金";
}
else if (tmp == 2000)
{
label16.Text = "一等奖学金";
}
else if (tmp == 5000)
{
label16.Text = "国家奖学金";
}
}
}
}
if (comboBox1.Text.Equals("2019"))
{
SqlConnection con = new SqlConnection("Data Source=.;DataBase=Test;User ID=sa;Pwd=zks123;");
con.Open();
string sql2 = "Select * from Honor where Sno='" + label5.Text + "' and Year='" + 2019 + "'";
SqlCommand cmd = new SqlCommand(sql2, con);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
if (sdr["SCS"] == System.DBNull.Value)
{
label11.Text = "不存在相应信息!";
}
else if (sdr["SCS"] != System.DBNull.Value)//不能使用sdr["SCS"]!=null
{
label11.Text = "2019年金额为" + sdr.GetString(sdr.GetOrdinal("SCS"));
int tmp = Convert.ToInt32(sdr.GetString(sdr.GetOrdinal("SCS")));
if (tmp == 600)
{
label16.Text = "三等奖学金";
}
else if (tmp == 1000)
{
label16.Text = "二等奖学金";
}
else if (tmp == 2000)
{
label16.Text = "一等奖学金";
}
else if (tmp == 5000)
{
label16.Text = "国家奖学金";
}
}
}
}
/*else if(comboBox1.Text!="2018"&& comboBox1.Text != "2019")
{
MessageBox.Show("请选择有效学年!");
label11.Text="请选择年份查看";
}
*/
}
点击三好学生,可查看每年的情况
代码如下(comboBox2)
private void ComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.Text.Equals("2018"))
{
SqlConnection con = new SqlConnection("Data Source=.;DataBase=Test;User ID=sa;Pwd=zks123;");
con.Open();
string sql3 = "Select * from Honor where Sno='" + label5.Text + "' and Year='" + 2018 + "'";
SqlCommand cmd = new SqlCommand(sql3, con);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
label14.Text = sdr.GetString(sdr.GetOrdinal("SHS"));
}
}
if (comboBox2.Text.Equals("2019"))
{
SqlConnection con = new SqlConnection("Data Source=.;DataBase=Test;User ID=sa;Pwd=zks123;");
con.Open();
string sql4 = "Select * from Honor where Sno='" + label5.Text + "' and Year='" + 2019 + "'";
SqlCommand cmd = new SqlCommand(sql4,con);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
label14.Text = sdr.GetString(sdr.GetOrdinal("SHS"));
}
}
}
点击课程安排
代码已经在Load中实现
点击课程成绩,可查询每学年每门课的成绩
代码如下(comboBox3)
private void ComboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox3.Text.Equals("2018"))
{
//这里是绑定数据源,可实现直接读取数据库的数据
SqlConnection con = new SqlConnection("Data Source=.;DataBase=Test;User ID=sa;Pwd=zks123;");
con.Open();
SqlDataAdapter sqldataAdapter = new SqlDataAdapter("Select Cname,Grade from Grade where Sno='" + label5.Text + "' and Year='" + 2018 + "'", con);
DataTable dt = new DataTable();
sqldataAdapter.Fill(dt);
dataGridView2.DataSource = dt;
con.Close();
}
if (comboBox3.Text.Equals("2019"))
{
SqlConnection con = new SqlConnection("Data Source=.;DataBase=Test;User ID=sa;Pwd=zks123;");
con.Open();
SqlDataAdapter sqldataAdapter = new SqlDataAdapter("Select Cname,Grade from Grade where Sno='" + label5.Text + "' and Year='" + 2019 + "'", con);
DataTable dt = new DataTable();
sqldataAdapter.Fill(dt);
dataGridView2.DataSource = dt;
con.Close();
}
}
教师界面Form4
教师的基本信息和学生一样的代码所以这里不再设置,只有荣誉管理和工资管理两项内容
控件:tabPage可用于分页查询,一个是荣誉管理,另一个是工资管理,listBox用于显示获得的荣誉列表,label9下面的是一个pictureBox,用于显示荣誉图片,此外还要添加一个imageList用于储存图片.imageList直接添加图片就可以了,如下图
数据库涉及的表的内容如下,表Salary,表Tusers
点击运行并登陆教师端
工资管理代码如下
private void 教师界面Form4_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=.;DataBase=Test;User ID=sa;Pwd=zks123;");
conn.Open();
SqlDataAdapter sqldataAdapter = new SqlDataAdapter("Select 基本工资,全勤提成,职称提成,科研提成,学术提成 from Salary where 教师号='" + label2.Text + "'", conn);
DataTable dt = new DataTable();
sqldataAdapter.Fill(dt);
dataGridView1.DataSource = dt;
conn.Close();
int a= Convert.ToInt32(dataGridView1.Rows[0].Cells[0].Value.ToString());
int b = Convert.ToInt32(dataGridView1.Rows[0].Cells[1].Value.ToString());
int c = Convert.ToInt32(dataGridView1.Rows[0].Cells[2].Value.ToString());
int d = Convert.ToInt32(dataGridView1.Rows[0].Cells[3].Value.ToString());
int m = Convert.ToInt32(dataGridView1.Rows[0].Cells[4].Value.ToString());
int n = a + b + c + d +m ;
label8.Text = n.ToString();//实现求和
}
荣誉管理界面
荣誉管理代码
private void 教师界面Form4_Load(object sender, EventArgs e)
{
label9.Text = "请点击相关荣誉查看";
label2.Text = Form1.class1.i.ToString();
tabPage1.Text = "荣誉管理";
tabPage2.Text = "工资管理";
SqlConnection con = new SqlConnection("Data Source=.;DataBase=Test;User ID=sa;Pwd=zks123;");
con.Open();
string sql = "Select * from Tusers where TID='" + label2.Text + "'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())//这里要用while而不能用if
{
label4.Text = sdr.GetString(sdr.GetOrdinal("Title"));
listBox1.Text = sdr.GetString(sdr.GetOrdinal("THonor"));
listBox1.Items.Add(sdr["THonor"].ToString());
}
con.Close();
}
private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem == null) return;
label9.Text = listBox1.SelectedItem.ToString();
} //让label9显示listBox选择的荣誉
//"点击切换"按钮代码,实现照片浏览
private void Button1_Click_1(object sender, EventArgs e)
{
if (j == 5)//记的在最前面定义一个j
j= 0;
pictureBox1.Image= imageList1.Images[j];
j++;
}