Download presentation
Presentation is loading. Please wait.
1
7. The FCL: files, databases, & data structures
2
2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics to I/O to networking to XML processing. The goal of the FCL? To present an abstract, portable view of the underlying operating system…” File I/O Database access Data structures
3
3 Microsoft Part 1 File I/O…
4
4 Microsoft I/O library Input/output library in System.IO namespace Compiled into mscorlib.dll assembly Support provided for: –file and directory management –text files –binary files
5
5 Microsoft Character I/O Classes provided to do character IO –most methods defined in base classes Two possible data locations –disk –string TextReader StreamReaderStringReader TextWriter StreamWriterStringWriter read from disk read from string write to disk write to StringBuilder
6
6 Microsoft StreamWriter StreamWriter usage: –open file with one of many constructors –write with overloaded Write / WriteLine methods –close automatically converted to string using ToString StreamWriter sw = new StreamWriter("Chores.txt"); int n = 3; sw.WriteLine("Go to pet store"); sw.Write("Feed all "); sw.Write(n); sw.WriteLine(" cats"); sw.Close(); open write close char, bool, string, short, int, long, float, double, etc. text file
7
7 Microsoft StreamReader StreamReader usage: –open file with one of many constructors –read characters or strings with Read / ReadLine methods –close StreamReader sr = new StreamReader("Chores.txt"); string s; while ((s = sr.ReadLine()) != null) Console.WriteLine(s); sr.Close(); open read close can read only char or string char, string text file
8
8 Microsoft Files and directories Lots of utility classes for working with files & directories – Directory :for manipulating directories and drives – File :for manipulating files – Path :for manipulating path strings using System.IO; string[] drives = Directory.GetLogicalDrives(); foreach (string s in drives) { if (Directory.Exists(s))... } all drives disk in drive?
9
9 Microsoft Part 2 Database access…
10
10 Microsoft Database library Database access provided by System.Data.* namespaces Compiled into System.Data.dll assembly Known collectively as ADO.NET –native support for SQL Server and Oracle –support for other databases via older OleDB technology –requires a knowledge of SQL Core namespaces: –general: System.Data, System.Data.Common –SQL Server: System.Data.SqlClient –Oracle: System.Data.OracleClient –OleDB: System.Data.OleDb
11
11 Microsoft Relational technology ADO.NET is designed to access relational databases Example: –Sales database with customers, orders, and products
12
12 Microsoft Overview of database access Three steps: 1.open connection to database 2.execute SQL to update DB / retrieve records 3.close connection
13
13 Microsoft Step 1: open connection Connections are opened based on connection string info –here we open a connection to a MS Access 2000 database –"Sales.mdb" must exist in same dir as.EXE (e.g. bin\Debug) using System.Data; using System.Data.OleDb; string sConnection; sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=Sales.mdb"; IDbConnection dbConn; dbConn = new OleDbConnection(sConnection); dbConn.Open(); MessageBox.Show( dbConn.State.ToString() ); connection
14
14 Microsoft Building connection strings Connection strings are vendor-specific, not well-documented Where to turn for help? –www.connectionstrings.comwww.connectionstrings.com –www.able-consulting.com/ADO_conn.htmwww.able-consulting.com/ADO_conn.htm
15
15 Microsoft Step 2: retrieve records Retrieve records via SQL Select query –read-only access by database field names string sql, name; sql = "Select * From Customers Order By LastName Asc, FirstName Asc;"; IDbCommand dbCmd; dbCmd = new OleDbCommand(); dbCmd.CommandText = sql; dbCmd.Connection = dbConn; IDataReader dbReader; dbReader = dbCmd.ExecuteReader(); while (dbReader.Read()) { // retrieve records 1-by-1... name = dbReader["LastName"] + ", " + dbReader["FirstName"]; this.listBox1.Items.Add(name); } data reader record
16
16 Microsoft Step 3: close connection Be sure to close connection… –to flush pending updates –so others can access DB (connections are limited resources) dbConn.Close();
17
17 Microsoft Guaranteed close? Ensure DB is closed via try-catch-finally: IDbConnection dbConn = null; try { dbConn.Open();. } catch(Exception ex) { System.Diagnostics.EventLog.WriteEntry("MyApp", ex.Message); System.Diagnostics.EventLog.WriteEntry("MyApp", ex.StackTrace); throw ex; } finally { if ((dbConn != null) && (dbConn.State != ConnectionState.Closed)) dbConn.Close(); }
18
18 Microsoft Updating a database To update database, execute an SQL Action query Example: –delete customer by their id number int result, cid = ?; string sql; sql = String.Format("Delete From Customers Where CID={0};", cid); IDbCommand dbCmd; dbCmd = new OleDbCommand(); dbCmd.CommandText = sql; dbCmd.Connection = dbConn; dbConn.Open(); result = dbCmd.ExecuteNonQuery(); dbConn.Close(); if (result != 1) throw new Exception("Action failed to delete 1 customer?!");
19
19 Microsoft Example of action queries Insert, update and delete: Insert Into Customers(CID, FirstName, LastName, CreditLimit, Balance) Values(118, 'Jia', 'Zhang', 10000.0, 0.0); Update Customers Set CreditLimit = 40000000000.0, Balance = 0.0 Where LastName = 'Gates' and FirstName = 'Bill'; Delete From Customers Where CID = 666;
20
20 Microsoft DataSets DataSets are an in-memory, read-write data structure –easily filled with data from a database –easily displayed in a GUI app DataSet ProductPriceQuantity Ants $ 0.495000 Birds $ 4.49500 Cats $29.95100 Dogs $79.9520 DB Command Connection DataAdapter
21
21 Microsoft Example Retrieve product info and display in a DataGrid: sql = "Select * From Products;". DataSet ds; IDataAdapter adapter; ds = new DataSet(); adapter = new OleDbDataAdapter((OleDbCommand) dbCmd); dbConn.Open(); adapter.Fill(ds); dbConn.Close(); this.dataGrid1.SetDataBinding(ds, "Table");
22
22 Microsoft Flushing changes back to database Reconnect, and apply adapter's Update() method: sql = "Select * From Products;". DataSet ds; IDataAdapter adapter; ds = (DataSet) this.dataGrid1.DataSource; adapter = new OleDbDataAdapter((OleDbCommand) dbCmd); OleDbCommandBuilder cmdBuilder; cmdBuilder = new OleDbCommandBuilder((OleDbDataAdapter) adapter); dbConn.Open(); adapter.Update(ds); // this fails if updates conflict dbConn.Close();
23
23 Microsoft Part 3 Data structures…
24
24 Microsoft Collections library Data structures in.NET are generally known as Collections Located in the namespace System.Collections Compiled into mscorlib.dll assembly Defined in terms of object for generic use Core classes: – Array – ArrayList – Hashtable – Stack – Queue
25
25 Microsoft Collection interfaces Collections implement various interfaces to ensure uniformity –classes that implement the same interface offer same services –makes library easier to learn and use –allows generic code to be written against interface Core interfaces: – ICollection – IEnumerable – IEnumerator – IList – IComparer – IComparable
26
26 Microsoft ArrayList ArrayList provides storage for sequence of elements –duplicate values ok –data stored internally as an array, automatically resized –primarily manipulated via Ilist public class ArrayList : IList, IEnumerable,... { // IList services... // additional services int Capacity { get... set... } void TrimToSize() int BinarySearch(object value) int IndexOf (object value, int startIndex) int LastIndexOf (object value, int startIndex)... } control of memory in underlying array searching
27
27 Microsoft IList interface IList defines sequence of elements –can be accessed by index public interface IList : ICollection { int Add (object value); void Insert(int index, object value); void Remove (object value); void RemoveAt(int index); void Clear (); bool Contains(object value); int IndexOf (object value); object this[int index] { get; set; } bool IsReadOnly { get; } bool IsFixedSize { get; } } add new elements remove containment testing read/write existing element structural properties
28
28 Microsoft Example Creating and using an ArrayList: using System.Collections; ArrayList a = new ArrayList(); a.Add("mom"); // added to end... a.Add("dad"); a.Add("sister"); Console.WriteLine(a[2]); // direct access if (a.Contains("dad")) // search... foreach (string s in a) // iterate Console.WriteLine(s); handles iteration interfaces, casting element 0 element 1 element 2 true "sister"
29
29 Microsoft Hashtable Hashtable provides collection of key/value pairs –keys must be unique, values hold the data –stores object reference for both key and value –GetHashCode method of key used to determine placement Hashtable ages = new Hashtable(); ages["Ann"] = 27; ages["Bob"] = 32; ages.Add("Tom", 15); ages["Ann"] = 28; int a = (int) ages["Ann"]; create add update retrieve
30
30 Microsoft Hashtable traversal Can traverse Hashtable contents –each element is DictionaryEntry struct –data exposed in Key and Value properties Hashtable ages = new Hashtable(); ages["Ann"] = 27; ages["Bob"] = 32; ages["Tom"] = 15; foreach (DictionaryEntry entry in ages) { string name = (string) entry.Key; int age = (int) entry.Value;... } enumerate entries get key and value
31
31 Microsoft Summary The FCL is huge The FCL is quite powerful The FCL is essentially a portable OS –fully-implemented on Windows 98 and above –partially-implemented on FreeBSD, Mac OS X, and Linux
32
32 Microsoft References Books: –J. Richter, "Applied Microsoft.NET Framework Programming" –T. Thai and H. Lam, ".NET Framework Essentials" –W. Vaughn and P. Blackburn, "ADO.NET Examples and Best Practices for C# Programmers" –B. Beauchemin, "Essential ADO.NET" Web sites: –http://msdn.microsoft.com/nethttp://msdn.microsoft.com/net –Oracle for.NET: http://otn.oracle.com/tech/windows/odpnet/http://otn.oracle.com/tech/windows/odpnet/ –Rotor (SSCLI): http://msdn.microsoft.com/net/ssclihttp://msdn.microsoft.com/net/sscli –Mono: http://www.go-mono.com/http://www.go-mono.com/
33
33 Microsoft Lab? Work on lab #4, "Interfaces and FCL"…
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.