您好,登錄后才能下訂單哦!
這篇文章主要介紹“C#怎么開(kāi)發(fā)Winform實(shí)現(xiàn)學(xué)生管理系統(tǒng)”,在日常操作中,相信很多人在C#怎么開(kāi)發(fā)Winform實(shí)現(xiàn)學(xué)生管理系統(tǒng)問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”C#怎么開(kāi)發(fā)Winform實(shí)現(xiàn)學(xué)生管理系統(tǒng)”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
數(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容器包含。
(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(); }
將數(shù)據(jù)庫(kù)連接字符串直接寫(xiě)在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)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。