江宏晖
这篇作品是本作者在校大二期间学习C#与数据库时自我训练所写的一个项目,主要是为了自学自练,当然如果能对部分同学有一定帮助的话那也是本人的荣幸。
这次数据库自编项目,使我掌握了C#的编写技巧以及SQLServer数据库和VS的链接方式,其中遇到过不少的难题,但是只要抓住思路检查之后还是能做到及时纠正,对每个部分了解是极为重要的一点。
由于时隔久远,电脑中关于数据库的部分模块被遗漏了,望各位大牛小牛们理解啦。
一、 项目诉求:
创建一个VS项目连接数据库,项目为学生成绩管理系统,其中包含登录、开课情况查询、学生成绩查询、学生信息维护、学生成绩维护以及帮助等主要内容。
分为两端:因用户权限不同,分别为普通用户端以及管理员端。
二、项目平台
PC、VS2012、SQLServer 2008R2
三、主要代码以及运行截图演示
Login.cs:
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 System.Data.SqlClient;
using System.Data.Odbc;
namespace student_test1
{
public partial class 学生成绩查询 : Form
{
public 学生成绩查询()
{
InitializeComponent();
}
private void 学生成绩查询_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“dataSet1.cjpm”中。您可以根据需要移动或删除它。
this.cjpmTableAdapter.Fill(this.dataSet1.cjpm);
}
private void chaxun_Click(object sender, EventArgs e)
{
string str = "dsn=jhh;uid=sa;pwd=sa";
string sqlstr = "select cjpm.学号,cjpm.姓名,cjpm.班级,cjpm.总分,cjpm.排名 from cjpm where cjpm.学号='" + xuehao.Text.Trim() + "'";
OdbcConnection conn = new OdbcConnection(str);
conn.Open();
OdbcDataAdapter da = new OdbcDataAdapter(sqlstr, conn);
DataSet qa = new DataSet();
da.Fill(qa, "学生成绩表");
DataTable dt = qa.Tables["学生成绩表"];
dataGridView1.DataSource = dt;
conn.Close();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
MDIparent1.cs:
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 student_test1
{
public partial class MDIParent1 : Form
{
private int childFormNumber = 0;
public MDIParent1()
{
InitializeComponent();
}
private void ShowNewForm(object sender, EventArgs e)
{
Form childForm = new Form();
childForm.MdiParent = this;
childForm.Text = "窗口 " + childFormNumber++;
childForm.Show();
}
private void OpenFile(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
string FileName = openFileDialog.FileName;
}
}
private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
string FileName = saveFileDialog.FileName;
}
}
private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Form childForm in MdiChildren)
{
childForm.Close();
}
}
private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
{
help u = new help();
u.Show();
}
private void 开课情况查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
开课情况查询 k = new 开课情况查询();
k.Show();
}
private void MDIParent1_Load(object sender, EventArgs e)
{
}
private void 学习成绩查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
学生成绩查询 q = new 学生成绩查询();
q.Show();
}
private void 学生信息维护ToolStripMenuItem_Click(object sender, EventArgs e)
{
学生信息维护 w = new 学生信息维护();
w.Show();
}
private void 学生成绩维护ToolStripMenuItem_Click(object sender, EventArgs e)
{
学生成绩维护 y = new 学生成绩维护();
y.Show();
}
}
}
MDIparent2.cs:
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 student_test1
{
public partial class MDIParent2 : Form
{
private int childFormNumber = 0;
public MDIParent2()
{
InitializeComponent();
}
private void ShowNewForm(object sender, EventArgs e)
{
Form childForm = new Form();
childForm.MdiParent = this;
childForm.Text = "窗口 " + childFormNumber++;
childForm.Show();
}
private void OpenFile(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
string FileName = openFileDialog.FileName;
}
}
private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
string FileName = saveFileDialog.FileName;
}
}
private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Form childForm in MdiChildren)
{
childForm.Close();
}
}
private void 开课情况查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
开课情况查询 k = new 开课情况查询();
k.Show();
}
private void 学生成绩查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
学生成绩查询 q = new 学生成绩查询();
q.Show();
}
private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
{
help u = new help();
u.Show();
}
private void MDIParent2_Load(object sender, EventArgs e)
{
}
}
}
开课情况查询.cs:
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 student_test1
{
public partial class 开课情况查询 : Form
{
public 开课情况查询()
{
InitializeComponent();
}
private void 开课情况查询_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“dataSet1.kc”中。您可以根据需要移动或删除它。
this.kcTableAdapter.Fill(this.dataSet1.kc);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
学生成绩查询.cs:
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 System.Data.SqlClient;
using System.Data.Odbc;
namespace student_test1
{
public partial class 学生成绩查询 : Form
{
public 学生成绩查询()
{
InitializeComponent();
}
private void 学生成绩查询_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“dataSet1.cjpm”中。您可以根据需要移动或删除它。
this.cjpmTableAdapter.Fill(this.dataSet1.cjpm);
}
private void chaxun_Click(object sender, EventArgs e)
{
string str = "dsn=jhh;uid=sa;pwd=sa";
string sqlstr = "select cjpm.学号,cjpm.姓名,cjpm.班级,cjpm.总分,cjpm.排名 from cjpm where cjpm.学号='" + xuehao.Text.Trim() + "'";
OdbcConnection conn = new OdbcConnection(str);
conn.Open();
OdbcDataAdapter da = new OdbcDataAdapter(sqlstr, conn);
DataSet qa = new DataSet();
da.Fill(qa, "学生成绩表");
DataTable dt = qa.Tables["学生成绩表"];
dataGridView1.DataSource = dt;
conn.Close();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
学生信息维护.cs:
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 System.Data.SqlClient;
using System.Data.Odbc;
namespace student_test1
{
public partial class 学生信息维护 : Form
{
public 学生信息维护()
{
InitializeComponent();
}
private void 学生信息维护_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“dataSet1.Xsxx”中。您可以根据需要移动或删除它。
this.xsxxTableAdapter.Fill(this.dataSet1.Xsxx);
}
private void tianjia_Click(object sender, EventArgs e)
{
//定义一个初始值n=0, 用于判断后期是否成功插入数据
int n = 0;
int m = 0;
string sqlsno = "select 学号 from Xsxx where (学号 like '" + xuehao.Text.Trim() + "')";
string sql = "insert into Xsxx (学号,姓名,性别,班级,年龄) values ('" + xuehao.Text + "','" + xingming.Text + "','" + xingbie.Text + "','" + banji.Text + "','" + Convert.ToString(nianling.Text) + "')";
//判断插入的数据是否为空,如果为空,则提示重新插入!
if (xuehao.Text.Trim() == "" || xingming.Text.Trim() == "" || xingbie.Text.Trim() == "" || banji.Text.Trim() == "" || nianling.Text.Trim() == "")
{
MessageBox.Show("插入数据不能为空,请按要求插入数据!");
return;
}
else if (int.TryParse(xuehao.Text, out m) == false || int.TryParse(banji.Text, out m) == false || int.TryParse(nianling.Text, out m) == false)
{
MessageBox.Show("请输入数字", "提示");
return;
}
else if (Convert.ToInt32(this.nianling.Text.Trim()) < 0)
{
MessageBox.Show("年龄输入有错,请重新输入");
return;
}
string str = Properties.Settings.Default.ConnectionString;
OdbcConnection conn = new OdbcConnection(str);
conn.Open();
OdbcCommand cmdsno = new OdbcCommand(sqlsno, conn);
OdbcDataReader sdr = cmdsno.ExecuteReader();
OdbcCommand cmd = new OdbcCommand(sql, conn);
//cmd.Parameters.AddRange(param);
n = cmd.ExecuteNonQuery();
if (n == 0)
{
MessageBox.Show("添加失败!");
return;
}
else if (n > 0)
{
MessageBox.Show("添加成功!");
}
conn.Close();
//调用refresh方法,在添加完成数据后 自动刷新 显示新数据
Refresh();
this.xsxxTableAdapter.Fill(this.dataSet1.Xsxx);
// }
conn.Close();
}
private void shanchu_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确认删除?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
//使用sql删除语句
string sqlxsxx = "delete from Xsxx where 1=1";
string sqlxscj = "delete from xscj where 1=1";
string sqlcjpm = "delete from cjpm where 1=1";
sqlxscj = sqlxscj + "and 学号=" + xuehao.Text;
sqlxsxx = sqlxsxx + "and 学号=" + xuehao.Text;
sqlcjpm = sqlcjpm + "and 学号=" + xuehao.Text;
//}
int n = 0;
string str = Properties.Settings.Default.ConnectionString;
OdbcConnection Conn = new OdbcConnection(str);
Conn.Open();
OdbcCommand cmdxsxx = new OdbcCommand(sqlxsxx, Conn);
n = cmdxsxx.ExecuteNonQuery();
OdbcCommand cmdxxxx = new OdbcCommand(sqlxscj, Conn);
cmdxxxx.ExecuteNonQuery();
OdbcCommand cmdcjpm = new OdbcCommand(sqlcjpm, Conn);
cmdcjpm.ExecuteNonQuery();
if (n == 0)
{
MessageBox.Show("不存在学号!");
return;
}
else if (n > 0)
{
MessageBox.Show("删除成功!");
}
Conn.Close();
//删除完后 刷新一下当前数据
Refresh();
this.xsxxTableAdapter.Fill(this.dataSet1.Xsxx);
}
}
private void xiugai_Click(object sender, EventArgs e)
{
int m = 0;
//在对数据进行修改之前 对文本框的内容做一下检查, 如果为空 则提示重新输入
if (xuehao.Text.Trim() == "" || nianling.Text.Trim() == "" || xingbie.Text.Trim() == "" || xingming.Text.Trim() == "" || banji.Text.Trim() == "")
{
MessageBox.Show("文本框的输入不能为空!");
return;
}
else if (int.TryParse(xuehao.Text, out m) == false || int.TryParse(nianling.Text, out m) == false || int.TryParse(banji.Text, out m) == false)
{
MessageBox.Show("请输入数字", "提示");
return;
}
else if (Convert.ToInt32(this.nianling.Text.Trim()) < 0)
{
MessageBox.Show("年龄输入有错,请重新输入");
return;
}
//使用SQL update 更新语句
//获取文本框中输入的内容, 通过SNO进行更新(SNO为当前鼠标点击行的Id)
string sqlUpdate = "update Xsxx set 学号 ='" + xuehao.Text + "',年龄 ='"
+ nianling.Text + "',性别='" + xingbie.Text + "',姓名='" + xingming.Text + "',班级='" + banji.Text + "'where 学号='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
string str = Properties.Settings.Default.ConnectionString;
OdbcConnection Conn = new OdbcConnection(str);
Conn.Open();
OdbcCommand cmdUpdate = new OdbcCommand(sqlUpdate, Conn);
int n = 0;
try
{
n = cmdUpdate.ExecuteNonQuery();
}
catch
{
//提示更新失败
MessageBox.Show("更新失败!");
return;// 并且返回
}
if (n > 0)
{
//否则更新成功
MessageBox.Show("修改成功!");
}
//执行完数据更新操作后 需要关闭数据库 节省资源
Conn.Close();
//更新完以后 调用刷新方法,将更新后的数据 显示在datagridview上面
Refresh();
this.xsxxTableAdapter.Fill(this.dataSet1.Xsxx);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridView1.SelectedRows.Count < 1)
{
return;
}
this.xuehao.Text = this.dataGridView1.CurrentRow.Cells["学号"].Value.ToString();
this.xingming.Text = this.dataGridView1.CurrentRow.Cells["姓名"].Value.ToString();
this.xingbie.Text = this.dataGridView1.CurrentRow.Cells["性别"].Value.ToString();
this.nianling.Text = this.dataGridView1.CurrentRow.Cells["年龄"].Value.ToString();
this.banji.Text = this.dataGridView1.CurrentRow.Cells["班级"].Value.ToString();
}
private void xuehao_TextChanged(object sender, EventArgs e)
{
}
}
}
学生成绩维护.cs:
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 System.Data.SqlClient;
using System.Data.Odbc;
namespace student_test1
{
public partial class 学生成绩维护 : Form
{
public 学生成绩维护()
{
InitializeComponent();
}
private void 学生成绩维护_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“学生成绩管理DataSet2.cjwh”中。您可以根据需要移动或删除它。
this.cjwhTableAdapter.Fill(this.学生成绩管理DataSet2.cjwh);
}
private void tianjia_Click(object sender, EventArgs e)
{
//定义一个初始值n=0, 用于判断后期是否成功插入数据
int n = 0;
int m = 0;
string sqlsno = "select 学号 from cjwh where (学号 like '" + xuehao.Text.Trim() + "')";
string sql = "insert into cjwh (学号,课程号,成绩) values ('" + xuehao.Text + "','" + kechenghao.Text + "','" + chengji.Text + "')";
//判断插入的数据是否为空,如果为空,则提示重新插入!
if (xuehao.Text.Trim() == "" || kechenghao.Text.Trim() == "" || chengji.Text.Trim() == "" )
{
MessageBox.Show("插入数据不能为空,请按要求插入数据!");
return;
}
else if (int.TryParse(xuehao.Text, out m) == false || int.TryParse(kechenghao.Text, out m) == false || int.TryParse(chengji.Text, out m) == false)
{
MessageBox.Show("请输入数字", "提示");
return;
}
string str = Properties.Settings.Default.ConnectionString;
OdbcConnection conn = new OdbcConnection(str);
conn.Open();
OdbcCommand cmdsno = new OdbcCommand(sqlsno, conn);
/* OdbcDataReader sdr = cmdsno.ExecuteReader();
if (sdr.Read())
{
MessageBox.Show("该学号已存在,请重新输入!");
return;
}
else*/
{
OdbcCommand cmd = new OdbcCommand(sql, conn);
//cmd.Parameters.AddRange(param);
n = cmd.ExecuteNonQuery();
if (n == 0)
{
MessageBox.Show("添加失败!");
return;
}
else if (n > 0)
{
MessageBox.Show("添加成功!");
}
conn.Close();
//调用refresh方法,在添加完成数据后 自动刷新 显示新数据
Refresh();
this.cjwhTableAdapter.Fill(this.学生成绩管理DataSet2.cjwh);
}
conn.Close();
}
private void xiugai_Click(object sender, EventArgs e)
{
int m = 0;
//在对数据进行修改之前 对文本框的内容做一下检查, 如果为空 则提示重新输入
if (xuehao.Text.Trim() == "" || kechenghao.Text.Trim() == "" || chengji.Text.Trim() == "" )
{
MessageBox.Show("文本框的输入不能为空!");
return;
}
else if (int.TryParse(xuehao.Text, out m) == false || int.TryParse(kechenghao.Text, out m) == false || int.TryParse(chengji.Text, out m) == false)
{
MessageBox.Show("请输入数字", "提示");
return;
}
//使用SQL update 更新语句
//获取文本框中输入的内容, 通过SNO进行更新(SNO为当前鼠标点击行的Id)
string sqlUpdate = "update cjwh set 学号 ='" + xuehao.Text + "',课程号 ='"
+ kechenghao.Text + "',成绩='" + chengji.Text + "'where 学号='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'and 课程号='" + dataGridView1.CurrentRow.Cells[1].Value.ToString() + "'";
string str = Properties.Settings.Default.ConnectionString;
OdbcConnection Conn = new OdbcConnection(str);
Conn.Open();
OdbcCommand cmdUpdate = new OdbcCommand(sqlUpdate, Conn);
int n = 0;
try
{
n = cmdUpdate.ExecuteNonQuery();
}
catch
{
//提示更新失败
MessageBox.Show("更新失败!");
return;// 并且返回
}
if (n > 0)
{
//否则更新成功
MessageBox.Show("修改成功!");
}
//执行完数据更新操作后 需要关闭数据库 节省资源
Conn.Close();
//更新完以后 调用刷新方法,将更新后的数据 显示在datagridview上面
Refresh();
this.cjwhTableAdapter.Fill(this.学生成绩管理DataSet2.cjwh);
}
private void shanchu_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确认删除?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
//使用sql删除语句
//string sqlxsxx = "delete from Xsxx where 1=1";
string sqlcjwh = "delete from cjwh where 1=1";
//string sqlcjpm = "delete from cjpm where 1=1";
//sqlxsxx = sqlxsxx + "and 学号=" + xuehao.Text;
sqlcjwh = sqlcjwh + "and 学号=" + xuehao.Text+"and 课程号=" + kechenghao.Text+"and 成绩=" + chengji.Text;
// sqlcjpm = sqlcjpm + "and 学号=" + xuehao.Text;
//}
int n = 0;
string str = Properties.Settings.Default.ConnectionString;
OdbcConnection Conn = new OdbcConnection(str);
Conn.Open();
OdbcCommand cmdcjwh = new OdbcCommand(sqlcjwh, Conn);
n = cmdcjwh.ExecuteNonQuery();
if (n == 0)
{
MessageBox.Show("不存在学号!");
return;
}
else if (n > 0)
{
MessageBox.Show("删除成功!");
}
Conn.Close();
//删除完后 刷新一下当前数据
Refresh();
this.cjwhTableAdapter.Fill(this.学生成绩管理DataSet2.cjwh);
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridView1.SelectedRows.Count < 1)
{
return;
}
this.xuehao.Text = this.dataGridView1.CurrentRow.Cells["学号"].Value.ToString();
this.kechenghao.Text = this.dataGridView1.CurrentRow.Cells["课程号"].Value.ToString();
this.chengji.Text = this.dataGridView1.CurrentRow.Cells["成绩"].Value.ToString();
}
}
}
Help.cs:
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 student_test1
{
public partial class help : Form
{
public help()
{
InitializeComponent();
}
private void help_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“dataSet1.cjpm”中。您可以根据需要移动或删除它。
this.cjpmTableAdapter.Fill(this.dataSet1.cjpm);
}
}
}
四、 操作说明
管理员登录账号密码: jhh(账号),123(密码);
普通用户登录账号密码:hhj(账号),456(密码);
在两个维护界面中,修改/删除部分需选中所要修改/删除学生的整行信息进行操作;