




已閱讀5頁,還剩30頁未讀, 繼續(xù)免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
基于C#窗體的數據庫應用系統(tǒng) 開發(fā)基礎與方法,(4課時),內 容,簡易成績管理系統(tǒng)介紹及需求分析 可枚舉類型與集合 綁定及作用 數據集及作用 適配器及作用 ADO.NET應用系統(tǒng)體系結構 LINQ, LINQ to SQL, Entity Framework 系統(tǒng)框架實現(實例),1 簡易成績管理系統(tǒng)介紹及需求分析,本需求分析方法、步驟適用管理信息系統(tǒng),1.調研系統(tǒng)應用環(huán)境 模擬成績管理; 課程如1年級1學期語文標識,一門課程可分多個教學班級,每個班級有唯一的一個任課老師; 局域網內運行。,2.獲取成績管理業(yè)務工作的工人可能成為未來系統(tǒng)的參與者(一個系統(tǒng)用戶可以執(zhí)行多個參與者的操作)及工作內容 1)成績管理員: 課程管理、教學班安排、學生管理、老師管理、成績修改、打印成績表等 2)任課教師: 班級成績登記、打印成績登記表等 3)學生: 查詢本人成績等,3.從參與者獲取處理數據資料 1)成績管理員: 課程信息、班級信息、老師信息、學生信息、成績信息等 2)任課教師: 相關班級成績信息等 3)學生: 本人成績信息等 作為教學案例,簡化系統(tǒng): 1)老師指任課老師和管理員,用角色區(qū)分,并作為系統(tǒng)用戶。 2)成績登記過程分兩步:錄入-暫存(*);提交。因此,成績信息包括暫存成績和確定成績。 3)學生不作為系統(tǒng)用戶。,4. 數據庫,說明: 設計表TempResult的目的是為了網格錄入中學生名稱列顯示的方便,另外對于更復雜的成績管理系統(tǒng)設計此表可提高編程方便性和運行效率 在Teach表中增加state是為了控制成績錄入過程,2.1 可枚舉類型,實現了接口IEnumerable或泛型接口IEnumerable的類型是可枚舉類型 如果一個類定義了GetEnumerator()方法,其對象可作為foreach in和LINQ中的from in表達式,public interface IEnumerable IEnumerator GetEnumerator(); public interface IEnumerable: IEnumerable IEnumerator GetEnumerator(); ,public interface IEnumerator object Current get; bool MoveNext(); void Reset(); public interface IDisposable void Dispose(); public interface IEnumerator:IEnumerator, IDisposable T Current get; ,枚舉器實現接口IEnumerator或泛型接口IEnumerator,2.2 集合,僅提供枚舉功能的低級集合接口,如IEnumerable和IEnumerable; 提供大小、枚舉和同步操作的集合接口ICollection和提供大小、枚舉、添加元素和移除元素等操作的泛型集合接口ICollection; 提供可按照索引訪問元素的高級集合接口,如IList和IList; 提供專用功能的字典集合接口,如IDictionary和IDictionary等; 提供通用功能的集合類,如ArrayList和List; 提供專用功能的集合類,如Hashtable、Queue、Stack、Dictionary、Queue和Stack等。,3 綁定及作用,數據綁定,即建立控件屬性與對象屬性(或對象列表中當前對象屬性)之間關系,以實現數據同步。,(M),(V),(C),ReadValue,WriteValue,控件屬性,Binding類對象,對象屬性或 對象列表IList當前對象屬性get,set,綁定例子,BindingSamples/BasicBinding: /定義參與綁定的(數據源:如Form)對象屬性: Title private string title; public string Title get return title; set title = value; private void Form1_Load(object sender, EventArgs e) /將控件的Text屬性綁定到對象的Title屬性 Binding textBinding = new Binding(“Text“, this, “Title“,false); textBox1.DataBindings.Add(textBinding); ,BindingSource類對象,BindingSource 組件有多種用途。 它在 Windows 窗體控件與數據源之間提供流通管理CurrencyManager、更改通知和其他服務,簡化了窗體上的控件到數據源的綁定。通過它的 DataSource 屬性將 BindingSource 組件關聯到數據源。,BindingSamples/ListBinding: private BindingSource bs; private void Form1_Load(object sender, EventArgs e) /設計時bs的DataSource成員為typeof(Person),即為類型信息 bs=new BindingSource(ponents); List list = new List(); list.Add(new Person(“12345“, “LY“, true); list.Add(new Person(“67890“, “ZHI“, false); bs.DataSource = list; idTextBox.DataBindings.Add(new Binding(“Text“, bs, “ID“, true); nameTextBox.DataBindings.Add(new Binding(“Text“, bs, “Name“, true); sexCheckBox.DataBindings.Add(new Binding(“CheckState“, bs, “Sex“, true); ,4 數據集及作用,System.Data 命名空間提供ADO.NET 體系結構中內存數據庫訪問功能的類。 DataSet對象,簡單說是一個輕量級的內存數據庫。 DataSet主要相關類型DataTable、DataRow、DataColumn、DataRelation 及對應的集合類(*Collection),Constraint及子類(UniqueConstraint 、ForeignKeyConstraint)和ConstraintCollection,等等。 類型化數據集與非類型化數據集。 作用:象數據庫一樣臨時保存結構化數據,提供插入、刪除、修改數據等功能;緩沖數據庫中的數據,為快速高效更新數據庫數據提供基礎。,數據集對象與控件綁定的對象關系圖,DataSet對象綁定,ReslutManReslutMan: public static void DisplayStudent() SqlDataAdapter ad = new SqlDataAdapter(“SELECT * FROM Student“, ResultDb.GetConnection(); DataSet ds = new DataSet(); ad.Fill(ds, “Student“); BindingSource bs = new BindingSource(); bs.DataSource = ds; bs.DataMember=“Student“; dataGridView1.DataSource = bs; /或直接 dataGridView1.DataSource = ds; dataGridView1.DataMember=“Student“; ,5 適配器及作用,使用 System.Data.SqlClient、System.Data.Odbc、System.Data.OleDb或 System.Data.OracleClient 命名空間,可訪問要與 DataSet 結合使用的數據源。每個 .NET Framework 數據提供程序都有相應的 DataAdapter 適配器是數據源即數據庫和 DataSet 之間的橋梁。,不通過適配器訪問數據庫使用的對象關系圖,Connection對象,ReslutManReslutMan: class ResultDb public static readonly string ConnectionString; public static SqlConnection GetConnection() return new SqlConnection(ConnectionString); static ResultDb() ConnectionString = “Data Source=.;Initial Catalog=ResultDb;Integrated Security=True;“; ,Command對象-ExecuteNonQuery(),ReslutManReslutMan: public void Insert(string name, string pwd, int role) SqlCommand cmd = ResultDb.GetConnection().CreateCommand(); cmd.CommandText = “INSERT INTO User (idu,name,pwd,role) VALUES (idu,name,pwd,role)“; cmd.CommandType = CommandType.Text; cmd.Parameters.Add(new SqlParameter(“idu“, idu); cmd.Parameters.AddWithValue(“name“, name); cmd.Parameters.AddWithValue(“pwd“, pwd); cmd.Parameters.AddWithValue(“role“, role); cmd.Connection.Open(); cmd.ExecuteNonQuery(); cmd.Connection.Close(); ,Command對象-ExecuteReader(),ReslutManReslutMan: public static User Select(Guid idu) User other = null; SqlCommand cmd = ResultDb.GetConnection().CreateCommand(); cmd.CommandText = “SELECT * FROM User WHERE idu=idu“; cmd.CommandType = CommandType.Text; cmd.Parameters.Add(new SqlParameter(“idu“, idu); cmd.Connection.Open(); SqlDataReader reader= cmd.ExecuteReader(); if (reader.Read() other = new User(); other.idu = (Guid)reader“idu”;/可以使用整型下標 = reader“name“.ToString(); other.pwd = reader“pwd“.ToString(); other.role = (int)reader“role“; cmd.Connection.Close(); return other; ,Command對象-ExecuteScalar(),ReslutManReslutMan: public static int GetCount() SqlCommand cmd = ResultDb.GetConnection().CreateCommand(); cmd.CommandText = “SELECT Count(*) FROM User“; cmd.CommandType = CommandType.Text; cmd.Connection.Open(); object cn = cmd.ExecuteScalar(); cmd.Connection.Close(); return (int)cn; ,Command對象-存儲過程調用,ReslutManReslutMan: public static void CreateTeachTempResult() SqlCommand cmd = ResultDb.GetConnection().CreateCommand(); cmd.CommandText = “CreateTeachTempResult“; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue(“idtc“, idtc); cmd.Connection.Open(); cmd.ExecuteNonQuery(); cmd.Connection.Close(); ,通過適配器訪問數據庫使用的對象關系圖,SqlDataAdapter-Fill與DataSet對象,ReslutManReslutMan: public static void DisplayStudent() SqlDataAdapter ad = new SqlDataAdapter( “SELECT * FROM Student“, ResultDb.GetConnection(); DataSet ds = new DataSet(); ad.Fill(ds, “Student“); dataGridView1.DataSource = ds; dataGridView1.DataMember = “Student“; ,6 ADO.NET應用系統(tǒng)的體系結構,SqlDataAdapter對象-Update ReslutManReslutMan/Form2.cs,: SqlCommand selectCMD = ResultDb.GetConnection().CreateCommand(); selectCMD.CommandText=“SELECT idst,name FROM Student“; selectCMD.CommandType = CommandType.Text; /產生SqlDataAdapter對象并設置SelectCommand成員 sda = new SqlDataAdapter(selectCMD); /讀取數據庫表數據并填充到數據集表 sda.Fill(dataSet1, “Student“); /*使用關聯的sda.SelectCommand對象自動產生InsertCommand, UpdateCommand,DeleteCommand*/ SqlCommandBuilder scb = new SqlCommandBuilder(sda); /查看生成的SQL語句和參數 SqlCommand cmd = scb.GetUpdateCommand(true); String s = cmd.CommandText + Environment.NewLine; foreach (SqlParameter p in cmd.Parameters) s += “Name=“ + p.ParameterName + “,Type=“ + p.SqlDbType.ToString() + Environment.NewLine; /MessageBox.Show(s); /綁定 dgvStudent.DataSource = dataSet1; dgvStudent.DataMember = “Student“;,應該通過VS提供的工具自動生成類型化數據集和適配器,7.1 LINQ (Language Integreted Query),傳統(tǒng)上,針對數據的查詢都是以簡單的字符串表示,而沒有編譯時類型檢查或 IntelliSense 支持。此外,還必須針對以下各種數據源學習不同的查詢語言:SQL 數據庫、XML 文檔、各種 Web 服務等。 在 Visual Studio 2008以后,可以在C# 下為各種數據源編寫 LINQ 查詢:SQL Server 數據庫、XML 文檔、ADO.NET 數據集以及支持 IEnumerable 或泛型 IEnumerable)接口的任意對象集合。 所有 LINQ 查詢操作都由以下三個不同的操作組成: 1)獲取數據源 2)創(chuàng)建查詢 3)執(zhí)行查詢,LINQ例子,更多例子見ReslutMan/LINQSample,/ The Three Parts of a LINQ Query: / 1. Data source. int numbers = new int 0, 1, 2, 3, 4, 5, 6 ; / 2. Query creation. / numQuery is an IEnumerable var numQuery = from num in numbers where (num % 2) = 0 select num; / 3. Query execution. foreach (int num in numQuery) Console.Write(“0 “, num); ,7.2 LINQ to SQL,LINQ to SQL 是LINQ的一個組件,提供了用于將關系數據作為對象管理的運行時基礎結構。 在 LINQ to SQL 中,關系數據庫的數據模型映射到編程語言表示的對象關系模型(ORM)。 TO: 當應用程序運行時,LINQ to SQL 將對象模型中的語言集成查詢轉換為 SQL,然后將它們發(fā)送到數據庫進行執(zhí)行。 FROM: 當數據庫返回結果時,LINQ to SQL 將它們轉換編程語言處理的對象。 在項目中添加LINQ to SQL類項,即可產生與特定數據庫相關的一系列類。查看擴展名為dbml及相關文件。,LINQ to SQL例子, 見ReslutMan/LINQtoSQL,ResultDatabaseDataContext db = new ResultDatabaseDataContext(); private void Form1_Load(object sender, EventArgs e) teachBindingSource.DataSource = from t in d
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 年產20萬噸本色漿替代廢紙漿項目建議書(參考)
- 納米銀導電膜建設項目建議書(范文模板)
- 2022年保護地球倡議書15篇
- 加強失能老年人健康服務的綜合支持
- 貨運站場轉型升級可行性研究報告(模板范文)
- 公交專用道優(yōu)化項目可行性研究報告
- 廣東青年職業(yè)學院《制藥設備及工程設計》2023-2024學年第二學期期末試卷
- 福建體育職業(yè)技術學院《建設工程工程量清單計價實務》2023-2024學年第二學期期末試卷
- 江西醫(yī)學高等專科學?!顿Y源設備基礎》2023-2024學年第二學期期末試卷
- 學生文明禮儀教育主題班會
- 2025年工程測量員(技師)職業(yè)技能鑒定理論考試指導題庫(含答案)
- 學校教學管理指導手冊
- 《現代農業(yè)生物技術育種方法》課件
- 2025-2030年中國核桃種植深加工行業(yè)運行狀況及前景趨勢分析報告
- 貴州文物調查研究-從文物看中華民族共同體歷史的區(qū)域實踐知到智慧樹章節(jié)測試課后答案2024年秋貴州民族大學
- 婦科常見疾病及護理常規(guī)
- 化工設備巡檢培訓
- 2024銀行從業(yè)資格個人貸款教材
- 2024中國電信通信傳輸設備與線路維護服務采購協議3篇
- 空氣源熱泵培訓資料
- T∕HGJ 12400-2021 石油化工儀表線纜選型設計標準
評論
0/150
提交評論