//==========動态程式設計部分================
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;
/// 初始化事件
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();
/// 按照學期資訊查詢
private void button1_Click(object sender, EventArgs e)
bind(2);
/// 按照課程查詢
private void button2_Click(object sender, EventArgs e)
bind(3);
///查詢全部
private void button3_Click(object sender, EventArgs e)
}
}
運作後的效果如圖4-93所示
圖4-93 程式執行效果圖
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,如需轉載請自行聯系原作者