7. the fcl: files, databases, & data structures. 2 microsoft objectives “the framework class...
Post on 22-Dec-2015
215 views
TRANSCRIPT
![Page 1: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/1.jpg)
7. The FCL: files, databases, & data structures
![Page 2: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/2.jpg)
2Microsoft
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
![Page 3: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/3.jpg)
3Microsoft
Part 1
• File I/O…
![Page 4: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/4.jpg)
4Microsoft
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
![Page 5: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/5.jpg)
5Microsoft
Character I/O
• Classes provided to do character IO– most methods defined in base classes
• Two possible data locations– disk– string
TextReader
StreamReader StringReader
TextWriter
StreamWriter StringWriter
read from disk read from stringwrite to disk write to StringBuilder
![Page 6: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/6.jpg)
6Microsoft
StreamWriter
• StreamWriter usage:– open file with one of many constructors– write with overloaded Write / WriteLine methods– close
automaticallyconverted to stringusing 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
![Page 7: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/7.jpg)
7Microsoft
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 onlychar or string
char, string text file
![Page 8: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/8.jpg)
8Microsoft
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?
![Page 9: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/9.jpg)
9Microsoft
Part 2
• Database access…
![Page 10: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/10.jpg)
10Microsoft
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
![Page 11: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/11.jpg)
11Microsoft
Relational technology
• ADO.NET is designed to access relational databases• Example:
– Sales database with customers, orders, and products
![Page 12: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/12.jpg)
12Microsoft
Overview of database access
• Three steps:
1. open connection to database
2. execute SQL to update DB / retrieve records
3. close connection
![Page 13: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/13.jpg)
13Microsoft
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
![Page 14: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/14.jpg)
14Microsoft
Building connection strings
• Connection strings are vendor-specific, not well-documented• Where to turn for help?
– www.connectionstrings.com– www.able-consulting.com/ADO_conn.htm
![Page 15: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/15.jpg)
15Microsoft
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 readerrecordrecordrecord
![Page 16: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/16.jpg)
16Microsoft
Step 3: close connection
• Be sure to close connection…– to flush pending updates– so others can access DB (connections are limited resources)
dbConn.Close();
![Page 17: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/17.jpg)
17Microsoft
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();}
![Page 18: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/18.jpg)
18Microsoft
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?!");
![Page 19: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/19.jpg)
19Microsoft
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;
![Page 20: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/20.jpg)
20Microsoft
DataSets
• DataSets are an in-memory, read-write data structure– easily filled with data from a database– easily displayed in a GUI app
DataSet
Product Price Quantity
Ants $ 0.49 5000
Birds $ 4.49 500
Cats $29.95 100
Dogs $79.95 20
DBCommand ConnectionDataAdapter
![Page 21: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/21.jpg)
21Microsoft
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");
![Page 22: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/22.jpg)
22Microsoft
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 conflictdbConn.Close();
![Page 23: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/23.jpg)
23Microsoft
Part 3
• Data structures…
![Page 24: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/24.jpg)
24Microsoft
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
![Page 25: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/25.jpg)
25Microsoft
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
![Page 26: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/26.jpg)
26Microsoft
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 memoryin underlying array
searching
![Page 27: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/27.jpg)
27Microsoft
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
![Page 28: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/28.jpg)
28Microsoft
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 iterationinterfaces, casting
element 0element 1element 2
true
"sister"
![Page 29: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/29.jpg)
29Microsoft
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
![Page 30: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/30.jpg)
30Microsoft
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
![Page 31: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/31.jpg)
31Microsoft
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
![Page 32: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/32.jpg)
32Microsoft
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/net– Oracle for .NET: http://otn.oracle.com/tech/windows/odpnet/– Rotor (SSCLI): http://msdn.microsoft.com/net/sscli– Mono: http://www.go-mono.com/
![Page 33: 7. The FCL: files, databases, & data structures. 2 Microsoft Objectives “The Framework Class Library (FCL) contains thousands of classes, from graphics](https://reader030.vdocument.in/reader030/viewer/2022032523/56649d775503460f94a58d93/html5/thumbnails/33.jpg)
33Microsoft
Lab?
• Work on lab #4, "Interfaces and FCL"…