4-2 ADO.NET-查询和检索数据13
生活随笔
收集整理的這篇文章主要介紹了
4-2 ADO.NET-查询和检索数据13
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//==========動(dòng)態(tài)程序設(shè)計(jì)部分================ using?System; using?System.Collections.Generic; using?System.ComponentModel; using?System.Data; using?System.Drawing; using?System.Text; using?System.Windows.Forms; ? namespace?WindowsApplication1 { ????public?partial?class?Form14?:?Form ????{ ????????public?Form14() ????????{ ????????????InitializeComponent(); ????????} ????????Buessiness?teacher =?new?Buessiness(); ????????Mydatabase?DB =?new?Mydatabase(); ????????///?<summary> ????????///??dataGridView1的數(shù)據(jù)綁定事件編碼 ????????///?</summary> ????????public?void?bind(int?i) ????????{ ????????????string?sql =?""; ????????????if?(i == 1) ????????????{ ????????????????sql =?"select tcno,sktime from tc where tno=2 and cno in (select distinct cno from sc where isposted=1) order by tcno asc"; ????????????} ????????????if?(i == 2) ????????????{ ????????????????string?skxq = comboBox1.SelectedValue.ToString(); ????????????????sql =?"select tcno,sktime from tc where tno=2 and cno in (select distinct cno from sc where isposted=1) and skxq="?+ skxq +?" order by tcno desc"; ????????????} ????????????if?(i == 3) ????????????{ ????????????????string?skbj = comboBox2.SelectedValue.ToString(); ????????????????sql =?"select tcno,sktime from tc where tno=2 and cno in (select distinct cno from sc where isposted=1) and bno="?+ skbj +?" order by tcno desc"; ????????????} ????????????DataTable?dt = DB.GetDataTable(sql); ????????????dataGridView1.DataSource = dt; ????????????dataGridView1.AllowUserToAddRows =?false; ????????} ????????///?<summary> ????????///?初始化事件 ????????///?</summary> ????????private?void?Form14_Load(object?sender,?EventArgs?e) ????????{ ????????????//首先,將教師的信息顯示出來。假設(shè)我們進(jìn)入系統(tǒng)教師的ID編號(hào)是2號(hào),來自teacher表 ????????????int?user_id = 2; ????????????int?dno, xb;//變量表示為單位編號(hào)和系部編號(hào) ????????????string?zc, degree, dname, tname; ????????????//變量表示:職稱、學(xué)位信息、單位名稱和教師姓名 ??????????teacher.Getteacherinfo(user_id,?out?dno,?out?xb,?out?zc,?out?degree,?out?dname,?out?tname); ????????????label6.Text =?Convert.ToString(user_id).Trim(); ????????????label7.Text = tname.Trim(); ????????????label8.Text = degree.Trim(); ????????????label9.Text = dname.Trim(); ????????????label10.Text = zc.Trim(); ????????????//其次,添加前五個(gè)學(xué)期的信息到comboBox1 ????????????string?sql2 =?"select distinct top 5 skxq,SUBSTRING(skxq,1,4)+'-'+cast((CAST (SUBSTRING(skxq,1,4) AS int)+1) as varchar(10))+'學(xué)年第'+SUBSTRING(skxq,5,1)+'學(xué)期' as skxqname??from tc order by skxq desc"; ????????????DataSet?ds1 = DB.GetDataSet(sql2); ????????????comboBox1.DataSource = ds1.Tables[0]; ????????????comboBox1.DisplayMember =?"skxqname"; ????????????comboBox1.ValueMember =?"skxq"; ????????????//然后,添加按照班級查詢信息 ????????????string?sql =?"select bno,bname from class where bno in(select bno from tc where tno=2)"; ????????????DataSet?ds2 = DB.GetDataSet(sql); ????????????comboBox2.DataSource = ds2.Tables[0]; ????????????comboBox2.DisplayMember =?"bname"; ????????????comboBox2.ValueMember =?"bno"; ????????????//最后,顯示電子網(wǎng)格中的數(shù)據(jù)信息 ????????????bind(1); ????????} ??????? ??????private?void?dataGridView1_RowPrePaint_1(object?sender,?DataGridViewRowPrePaintEventArgs?e) ????????{ ????????????int?k = dataGridView1.Rows.Count; ????????????if?(e.RowIndex != -1) ????????????{ ????????????????DataGridViewRow?DGrow = dataGridView1.Rows[e.RowIndex]; ????????????????int?i =?Convert.ToInt32(DGrow.Cells["tcno"].Value); ????????????????//此處需要獲取課程表的課程性質(zhì)名稱 ????????????????int?cno, tno, sktime, bno, zt, schno, stunum, yy, wy, ztbk, stunumbk, yybk, wybk, ztqk, stunumqk, yyqk, wyqk; ????????????????string?skxq, tname, cname, bname, skxqn, schname; ????????????????teacher.GetCourselist(i,?out?cno,?out?tno,?out?skxq,?out?sktime,?out?bno,?out?tname,?out?cname,?out?bname,?out?skxqn,?out?zt,?out?schno,?out?schname,?out?stunum,?out?yy,?out?wy,?out?ztbk,?out?stunumbk,?out?yybk,?out?wybk,?out?ztqk,?out?stunumqk,?out?yyqk,?out?wyqk); ????????????????DGrow.Cells["skxq"].Value = skxqn; ????????????????DGrow.Cells["banji"].Value = bname; ????????????????DGrow.Cells["school"].Value = schname; ????????????????DGrow.Cells["coursename"].Value = cname; ????????????????DGrow.Cells["sktime"].Value = sktime.ToString(); ????????????????DGrow.Cells["stunum"].Value = stunum.ToString(); ????????????????DGrow.Cells["yy"].Value = yy.ToString(); ????????????????DGrow.Cells["wy"].Value = wy.ToString(); ????????????} ????????} ????????///?<summary> ????????///?按照學(xué)期信息查詢 ????????///?</summary> ????????private?void?button1_Click(object?sender,?EventArgs?e) ????????{ ????????????bind(2); ????????} ????????///?<summary> ????????///?按照課程查詢 ????????///?</summary> ????????private?void?button2_Click(object?sender,?EventArgs?e) ????????{ ????????????bind(3); ????????} ????????///?<summary> ????????///查詢?nèi)?/span> ????????///?</summary> ????????private?void?button3_Click(object?sender,?EventArgs?e) ????????{ ????????????bind(1); ????????} ????} } 運(yùn)行后的效果如圖4-93所示 圖4-93?程序執(zhí)行效果圖
本文轉(zhuǎn)自 qianshao 51CTO博客,原文鏈接:http://blog.51cto.com/qianshao/216112,如需轉(zhuǎn)載請自行聯(lián)系原作者
4-4??本章小結(jié)
ADO.NET小結(jié),請參加圖4-94所示: 圖4-94 ADO.NET總結(jié)圖 圖中內(nèi)部箭頭表示應(yīng)用程序從數(shù)據(jù)庫讀取數(shù)據(jù),外圍箭頭表示從應(yīng)用程序向數(shù)據(jù)庫寫數(shù)據(jù)。對于本章知識(shí)點(diǎn)而言,涉及面廣,知識(shí)結(jié)構(gòu)復(fù)雜,但回頭總結(jié)我們不難發(fā)現(xiàn),以下的知識(shí)點(diǎn)是學(xué)習(xí)本章知識(shí)的重點(diǎn)和難點(diǎn): n在DataSet對象內(nèi)表示的數(shù)據(jù)是數(shù)據(jù)庫的部分或全部的斷開式內(nèi)存副本 nDataAdapter對象用來填充數(shù)據(jù)集和用來更新數(shù)據(jù)集到數(shù)據(jù)庫,這樣方便了數(shù)據(jù)庫和數(shù)據(jù)集之間的交互 n類型化數(shù)據(jù)集對象是DataSet類的派生類的實(shí)例,這些類都基于XML結(jié)構(gòu) nDataTable表示一個(gè)內(nèi)存數(shù)據(jù)表,而DataColumn表示DataTable中列的結(jié)構(gòu) nDataView是DataTable中存儲(chǔ)的數(shù)據(jù)的表示層 nDataReader對象提供只進(jìn)、只讀和連接式數(shù)據(jù)訪問,并要求使用專用的數(shù)據(jù)連接 nDataReader對象提供檢索強(qiáng)類型化數(shù)據(jù)的方法 n在數(shù)據(jù)庫編程中使用數(shù)據(jù)綁定控件時(shí),?DataGridView控件是Visual?Studio?.NET中提供的最通用、最強(qiáng)大和最靈活的控件 nDataGridView控件以表的形式顯示數(shù)據(jù),并根據(jù)需要支持?jǐn)?shù)據(jù)編輯功能,如插入、更新、刪除、排序和分頁 n使用DataSource屬性為DataGridView控件設(shè)置一個(gè)有效的數(shù)據(jù)源 n調(diào)用Update()方法來執(zhí)行相應(yīng)的插入、更新和刪除操作時(shí),將執(zhí)行DataAdapter的InsertCommand、UpdateCommand和DeleteCommand屬性 n定制DataGridView界面 ? 課 后 練 習(xí) 1、本練習(xí)目標(biāo)是要求編寫一個(gè)應(yīng)用程序,可以添加、修改、刪除學(xué)生基本信息。用戶界面如圖4-95所示,數(shù)據(jù)庫為school1,僅用了兩個(gè)表,mz是表示民族的表,student是表示學(xué)生的表。具體字段設(shè)計(jì)情況請參見圖4-96,數(shù)據(jù)庫環(huán)境是SQL?Server?2005。 圖4-95?學(xué)生基本信息應(yīng)用程序界面圖 圖4-96?數(shù)據(jù)庫設(shè)計(jì)圖 2、本練習(xí)目標(biāo)是要求編寫一個(gè)應(yīng)用程序,實(shí)現(xiàn)可以添加、修改、刪除、查詢數(shù)據(jù)庫中學(xué)生基本信息的功能。用戶界面如圖4-97所示,數(shù)據(jù)庫為圖4-96所示。 圖4-97?學(xué)生基本信息應(yīng)用程序界面圖本文轉(zhuǎn)自 qianshao 51CTO博客,原文鏈接:http://blog.51cto.com/qianshao/216112,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的4-2 ADO.NET-查询和检索数据13的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中时间各种格式
- 下一篇: mydate97设置时间