4-2 ADO.NET-查询和检索数据13
生活随笔
收集整理的這篇文章主要介紹了
4-2 ADO.NET-查询和检索数据13
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//==========動態程序設計部分================ 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的數據綁定事件編碼 ????????///?</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) ????????{ ????????????//首先,將教師的信息顯示出來。假設我們進入系統教師的ID編號是2號,來自teacher表 ????????????int?user_id = 2; ????????????int?dno, xb;//變量表示為單位編號和系部編號 ????????????string?zc, degree, dname, tname; ????????????//變量表示:職稱、學位信息、單位名稱和教師姓名 ??????????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(); ????????????//其次,添加前五個學期的信息到comboBox1 ????????????string?sql2 =?"select distinct top 5 skxq,SUBSTRING(skxq,1,4)+'-'+cast((CAST (SUBSTRING(skxq,1,4) AS int)+1) as varchar(10))+'學年第'+SUBSTRING(skxq,5,1)+'學期' 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"; ????????????//最后,顯示電子網格中的數據信息 ????????????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); ????????????????//此處需要獲取課程表的課程性質名稱 ????????????????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> ????????///?按照學期信息查詢 ????????///?</summary> ????????private?void?button1_Click(object?sender,?EventArgs?e) ????????{ ????????????bind(2); ????????} ????????///?<summary> ????????///?按照課程查詢 ????????///?</summary> ????????private?void?button2_Click(object?sender,?EventArgs?e) ????????{ ????????????bind(3); ????????} ????????///?<summary> ????????///查詢全部 ????????///?</summary> ????????private?void?button3_Click(object?sender,?EventArgs?e) ????????{ ????????????bind(1); ????????} ????} } 運行后的效果如圖4-93所示 圖4-93?程序執行效果圖
本文轉自 qianshao 51CTO博客,原文鏈接:http://blog.51cto.com/qianshao/216112,如需轉載請自行聯系原作者
4-4??本章小結
ADO.NET小結,請參加圖4-94所示: 圖4-94 ADO.NET總結圖 圖中內部箭頭表示應用程序從數據庫讀取數據,外圍箭頭表示從應用程序向數據庫寫數據。對于本章知識點而言,涉及面廣,知識結構復雜,但回頭總結我們不難發現,以下的知識點是學習本章知識的重點和難點: n在DataSet對象內表示的數據是數據庫的部分或全部的斷開式內存副本 nDataAdapter對象用來填充數據集和用來更新數據集到數據庫,這樣方便了數據庫和數據集之間的交互 n類型化數據集對象是DataSet類的派生類的實例,這些類都基于XML結構 nDataTable表示一個內存數據表,而DataColumn表示DataTable中列的結構 nDataView是DataTable中存儲的數據的表示層 nDataReader對象提供只進、只讀和連接式數據訪問,并要求使用專用的數據連接 nDataReader對象提供檢索強類型化數據的方法 n在數據庫編程中使用數據綁定控件時,?DataGridView控件是Visual?Studio?.NET中提供的最通用、最強大和最靈活的控件 nDataGridView控件以表的形式顯示數據,并根據需要支持數據編輯功能,如插入、更新、刪除、排序和分頁 n使用DataSource屬性為DataGridView控件設置一個有效的數據源 n調用Update()方法來執行相應的插入、更新和刪除操作時,將執行DataAdapter的InsertCommand、UpdateCommand和DeleteCommand屬性 n定制DataGridView界面 ? 課 后 練 習 1、本練習目標是要求編寫一個應用程序,可以添加、修改、刪除學生基本信息。用戶界面如圖4-95所示,數據庫為school1,僅用了兩個表,mz是表示民族的表,student是表示學生的表。具體字段設計情況請參見圖4-96,數據庫環境是SQL?Server?2005。 圖4-95?學生基本信息應用程序界面圖 圖4-96?數據庫設計圖 2、本練習目標是要求編寫一個應用程序,實現可以添加、修改、刪除、查詢數據庫中學生基本信息的功能。用戶界面如圖4-97所示,數據庫為圖4-96所示。 圖4-97?學生基本信息應用程序界面圖本文轉自 qianshao 51CTO博客,原文鏈接:http://blog.51cto.com/qianshao/216112,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的4-2 ADO.NET-查询和检索数据13的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中时间各种格式
- 下一篇: mydate97设置时间