C#開(kāi)發(fā)Winform實(shí)現(xiàn)學(xué)生管理系統(tǒng)
一、案例功能的實(shí)現(xiàn)
數(shù)據(jù):
--專業(yè)
create table ProfessionInfo
(
ProfessionID int primary key identity(1,1), --專業(yè)編號(hào)
professionName varchar(50) not null unique --專業(yè)名稱
)
--學(xué)生
create table StudentInfo
(
StuID varchar(20) primary key, --學(xué)生學(xué)號(hào)
StuName varchar(50) not null, --學(xué)生姓名
StuAge int not null check(StuAge > 0 and StuAge < 130), --學(xué)生年齡
StuSex char(2) not null check(StuSex in('男','女')), --學(xué)生性別
StuHobby nvarchar(100), --愛(ài)好
ProfessionID int not null references ProfessionInfo(ProfessionID), --所屬專業(yè)編號(hào)
)
--添加專業(yè)信息
insert into ProfessionInfo(professionName) values('電子競(jìng)技')
insert into ProfessionInfo(professionName) values('軟件開(kāi)發(fā)')
insert into ProfessionInfo(professionName) values('醫(yī)療護(hù)理')
--插入學(xué)生信息
insert into StudentInfo(StuID,StuName,StuAge,StuSex,StuHobby,ProfessionID)
values('001','劉備',18,'男','',1)
insert into StudentInfo(StuID,StuName,StuAge,StuSex,StuHobby,ProfessionID)
values('002','關(guān)羽',20,'男','',2)
insert into StudentInfo(StuID,StuName,StuAge,StuSex,StuHobby,ProfessionID)
values('003','張飛',19,'男','',2)
insert into StudentInfo(StuID,StuName,StuAge,StuSex,StuHobby,ProfessionID)
values('004','孫尚香',17,'女','',3)業(yè)務(wù)需求:

(1)專業(yè)下拉框綁定專業(yè)表數(shù)據(jù),網(wǎng)格控件綁定學(xué)生數(shù)據(jù),并且點(diǎn)擊"搜索"按鈕可以多條件組合查詢。
(2)選中某一行,右鍵可以彈出"刪除"菜單,點(diǎn)擊"刪除"菜單可以刪除學(xué)生數(shù)據(jù)。
(3)點(diǎn)擊"新增"按鈕,彈出新增窗體,在此窗體中完成學(xué)生的新增操作。

(4)選中某一行,點(diǎn)擊"編輯"按鈕,彈出編輯窗體,在此窗體中完成數(shù)據(jù)的修改。

備注:其中性別的單選框,以及愛(ài)好的多選框分別用兩個(gè)Pannel容器包含。
實(shí)現(xiàn)代碼:
(1)查詢窗體綁定專業(yè)信息、綁定學(xué)生信息以及搜索功能代碼:
#region 綁定專業(yè)信息到下拉框
private void BindProfession()
{
DataTable dt = new DataTable();
DBHelper.PrepareSql("select * from ProfessionInfo");
dt = DBHelper.ExecQuery();
DataRow dr = dt.NewRow();
dr["ProfessionID"] = 0;
dr["professionName"] = "--請(qǐng)選擇--";
dt.Rows.InsertAt(dr, 0);
this.cmbPro.DataSource = dt;
this.cmbPro.DisplayMember = "professionName";
this.cmbPro.ValueMember = "ProfessionID";
}
#endregion
#region 綁定學(xué)生數(shù)據(jù)
private void BindData()
{
string sql = "select * from StudentInfo inner join ProfessionInfo on StudentInfo.ProfessionID=ProfessionInfo.ProfessionID where 1 = 1 ";
if(!this.cmbPro.SelectedValue.ToString().Equals("0"))
sql += " and StudentInfo.ProfessionID = " + this.cmbPro.SelectedValue.ToString();
if(!this.txtName.Text.Equals(""))
sql += " and StuName like '%" + this.txtName.Text + "%'";
this.dataGridView1.AutoGenerateColumns = false;
DBHelper.PrepareSql(sql);
this.dataGridView1.DataSource = DBHelper.ExecQuery();
}
#endregion
private void Form1_Load(object sender, EventArgs e)
{
BindProfession();
BindData();
}
private void btSearch_Click(object sender, EventArgs e)
{
BindData();
}(2)刪除菜單代碼:
private void 刪除ToolStripMenuItem_Click(object sender, EventArgs e)
{
//添加是否確定刪除的對(duì)話框
DialogResult result = MessageBox.Show("確定要?jiǎng)h除數(shù)據(jù)嗎,刪除之后無(wú)法恢復(fù)!", "提示框",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (result == DialogResult.Cancel)
return;
string stuid = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string sql = "delete from StudentInfo where StuID = @StuID";
DBHelper.PrepareSql(sql);
DBHelper.SetParameter("StuID", stuid);
int rowCount = DBHelper.ExecNonQuery();
if (rowCount == 1)
MessageBox.Show("刪除成功!");
else
MessageBox.Show("刪除失敗!");
BindData();
}(3)添加學(xué)生信息窗體代碼:
#region 綁定專業(yè)信息到下拉框
private void BindProfession()
{
DataTable dt = new DataTable();
DBHelper.PrepareSql("select * from ProfessionInfo");
dt = DBHelper.ExecQuery();
DataRow dr = dt.NewRow();
dr["ProfessionID"] = 0;
dr["professionName"] = "--請(qǐng)選擇--";
dt.Rows.InsertAt(dr, 0);
this.cmbPro.DataSource = dt;
this.cmbPro.DisplayMember = "professionName";
this.cmbPro.ValueMember = "ProfessionID";
}
#endregion
private void FrmAdd_Load(object sender, EventArgs e)
{
BindProfession();
}
private void btAdd_Click(object sender, EventArgs e)
{
string sql = "insert into StudentInfo(StuID,StuName,StuAge,StuSex,StuHobby,ProfessionID) values(@StuID,@StuName,@StuAge,@StuSex,@StuHobby,@ProfessionID)";
DBHelper.PrepareSql(sql);
DBHelper.SetParameter("StuID", this.txtId.Text);
DBHelper.SetParameter("StuName",this.txtName.Text);
DBHelper.SetParameter("StuAge",this.txtAge.Text);
//性別處理
string sex = "";
if (this.rbBoy.Checked == true) sex = this.rbBoy.Text;
if (this.rbGirl.Checked == true) sex = this.rbGirl.Text;
DBHelper.SetParameter("StuSex", sex);
//愛(ài)好處理
string hobby = "";
foreach (CheckBox ck in this.panel2.Controls)
{
if (ck.Checked == true)
{
if (!hobby.Equals(""))
hobby += ",";
hobby += ck.Text;
}
}
DBHelper.SetParameter("StuHobby", hobby);
DBHelper.SetParameter("ProfessionID",this.cmbPro.SelectedValue.ToString());
int rowCount = DBHelper.ExecNonQuery();
if (rowCount == 1)
{
MessageBox.Show("新增成功!");
this.Close();
}
else
{
MessageBox.Show("新增失敗!");
}
}(4)編輯學(xué)生信息窗體代碼:
public string StuID { get; set; } //學(xué)生編號(hào)
#region 綁定專業(yè)信息到下拉框
private void BindProfession()
{
DataTable dt = new DataTable();
DBHelper.PrepareSql("select * from ProfessionInfo");
dt = DBHelper.ExecQuery();
DataRow dr = dt.NewRow();
dr["ProfessionID"] = 0;
dr["professionName"] = "--請(qǐng)選擇--";
dt.Rows.InsertAt(dr, 0);
this.cmbPro.DataSource = dt;
this.cmbPro.DisplayMember = "professionName";
this.cmbPro.ValueMember = "ProfessionID";
}
#endregion
private void BindDetail()
{
string sql = "select * from StudentInfo where StuID = " + this.StuID;
DBHelper.PrepareSql(sql);
DataTable dt = new DataTable();
dt = DBHelper.ExecQuery();
this.txtId.Text = dt.Rows[0]["StuID"].ToString();
this.txtName.Text = dt.Rows[0]["StuName"].ToString();
this.txtAge.Text = dt.Rows[0]["StuAge"].ToString();
this.cmbPro.SelectedValue = dt.Rows[0]["ProfessionID"].ToString();
//性別處理
if (dt.Rows[0]["StuSex"].ToString().Equals("男"))
this.rbBoy.Checked = true;
else
this.rbGirl.Checked = true;
//愛(ài)好處理
string[] arrHobby = dt.Rows[0]["StuHobby"].ToString().Split(',');
foreach (string hobby in arrHobby)
{
foreach (CheckBox ck in this.panel2.Controls)
{
if (ck.Text.Equals(hobby))
ck.Checked = true;
}
}
}
private void FrmEdit_Load(object sender, EventArgs e)
{
BindProfession();
BindDetail();
}
private void btUpdate_Click(object sender, EventArgs e)
{
string sql = "update StudentInfo set StuName=@StuName,StuAge=@StuAge,StuSex=@StuSex,StuHobby=@StuHobby,ProfessionID=@ProfessionID where StuID=@StuID";
DBHelper.PrepareSql(sql);
DBHelper.SetParameter("StuName", this.txtName.Text);
DBHelper.SetParameter("StuAge", this.txtAge.Text);
//性別處理
string sex = "";
if (this.rbBoy.Checked == true) sex = this.rbBoy.Text;
if (this.rbGirl.Checked == true) sex = this.rbGirl.Text;
DBHelper.SetParameter("StuSex", sex);
//愛(ài)好處理
string hobby = "";
foreach (CheckBox ck in this.panel2.Controls)
{
if (ck.Checked == true)
{
if (!hobby.Equals(""))
hobby += ",";
hobby += ck.Text;
}
}
DBHelper.SetParameter("StuHobby", hobby);
DBHelper.SetParameter("ProfessionID", this.cmbPro.SelectedValue.ToString());
DBHelper.SetParameter("StuID", this.StuID);
int rowCount = DBHelper.ExecNonQuery();
if (rowCount == 1)
{
MessageBox.Show("修改成功!");
this.Close();
}
else
{
MessageBox.Show("修改失敗!");
}
}(5)查詢窗體中"新增"和"編輯"按鈕代碼:
private void btAdd_Click(object sender, EventArgs e)
{
FrmAdd frm = new FrmAdd();
//frm.Owner = this;
frm.Show();
}
private void btEdit_Click(object sender, EventArgs e)
{
string stuid = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
FrmEdit frm = new FrmEdit();
frm.StuID = stuid;
frm.Show();
}二、補(bǔ)充:連接字符串配置
將數(shù)據(jù)庫(kù)連接字符串直接寫在C#代碼中,如果連接字符串需要發(fā)生改變,必須在C#代碼中修改,并且重新進(jìn)行編譯的操作,給軟件實(shí)施帶來(lái)了麻煩。
解決此問(wèn)題,可以將數(shù)據(jù)庫(kù)連接字符串存放在配置文件中。
(1)在項(xiàng)目中找到App.config文件,如果沒(méi)有此文件可以添加一個(gè)應(yīng)用程序配置文件,在此配置文件的configuration節(jié)點(diǎn)內(nèi)部添加如下配置:
<connectionStrings> <add name="DefaultConn" connectionString="server=.;database=DBTEST;uid=sa;pwd=123456;"/> </connectionStrings>
(2)給項(xiàng)目添加引用"System.Configuration",并且將C#中連接字符串的賦值修改如下:
public static string connStr = ConfigurationManager.ConnectionStrings["DefaultConn"].ConnectionString;
到此這篇關(guān)于C#開(kāi)發(fā)Winform實(shí)現(xiàn)學(xué)生管理系統(tǒng)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
unity實(shí)現(xiàn)動(dòng)態(tài)排行榜
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)動(dòng)態(tài)排行榜,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
C# Distinct和重寫IEqualityComparer時(shí)要知道的二三事
這篇文章主要給大家介紹了關(guān)于C# Distinct和重寫IEqualityComparer時(shí)要知道的二三事,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放
這篇文章主要介紹了C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放,涉及C#采用Dispose模式操作資源的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
UnityRTS實(shí)現(xiàn)相機(jī)移動(dòng)縮放功能
這篇文章主要為大家詳細(xì)介紹了UnityRTS實(shí)現(xiàn)相機(jī)的移動(dòng)縮放功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
.NET操作NPOI實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出
NPOI是指構(gòu)建在POI 3.x版本之上的一個(gè)程序,NPOI可以在沒(méi)有安裝Office的情況下對(duì)Word或Excel文檔進(jìn)行讀寫操作,下面小編為大家介紹了如何操作NPOI實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出,需要的可以參考一下2023-09-09

