NET Frameworks week 4 November 24, 2003. Agenda – November 24, 2003 Open Questions Namespaces Namespace (Files and Directories) File I/O and

  • View
    216

  • Download
    1

Embed Size (px)

Text of NET Frameworks week 4 November 24, 2003. Agenda – November 24, 2003 Open...

  • Slide 1
  • .NET Frameworks week 4 November 24, 2003
  • Slide 2
  • Agenda November 24, 2003 Open Questions Namespaces System.IO Namespace (Files and Directories) File I/O and Directory Class Exercise Databases and SQL (GRID Control) Simple Books Database Class Exercise Books Database Grid Populator Class Exercise XML Guestbook Database Homework Exercise due December 4th
  • Slide 3
  • WeekTopics 5 - Thursday 12/4/2003 Bootstrapping the Common Language Resource (CLR) Modules and Assemblies.Net Packaging Public and Private Keys Shared Assemblies The Global Assemble Cache (GAC) Strong Names.NET Frameworks Language Overview Frameworks Class Library Advanced Concepts (Serialization and Reflection) Course Schedule
  • Slide 4
  • WeekTopics 6 - Monday 12/8/2003 ASP.NET Programming.NET Web Form Application Introduction to Web Services Web Security 7 - Thursday 12/11/2003 Web Services Class Project (continued) 8 - Monday 12/15/2003 Web Services Class Project (continued) Course Schedule
  • Slide 5
  • Tonights Class Goals Namespaces Files and Streams and Directories ADO.NET Database Programming Connectors Adapters DataSets Using the Grid Control Datasets and XML
  • Slide 6
  • Namespaces
  • Slide 7
  • The namespace keyword is used to declare a scope. This namespace scope lets you organize code and gives you a way to create globally-unique types. Even if you do not explicitly declare one, a default namespace is created. This unnamed namespace, sometimes called the global namespace, is present in every file. Any identifier in the global namespace is available for use in a named namespace. Namespaces implicitly have public access and this is not modifiable.
  • Slide 8
  • Namespaces Used to organize classes and types into a hierarchical structure. Makes it easier to find types. Helps prevent name collisions. Declared with the namespace keyword. namespace HelloVS { } Namespaces can be nested namespace HelloVS { namespace Foo { } } namespace HelloVS.Foo { }
  • Slide 9
  • Namespaces namespace name[.name1]...] { type-declarations } name, name1 - A namespace name can be any legal identifier. A namespace name can contain periods. type-declarations - Within a namespace, you can declare one or more of the following types: another namespace class interface struct enum delegate
  • Slide 10
  • Namespaces and Using Namespaces can get pretty long e.g. System.Runtime.Remoting.Channels.Http To use the HttpChannel class, you would need to type: System.Runtime.Remoting.Channels.Http.HttpChannel Fortunately, C# gives us a shorthand mechanism using System.Runtime.Remoting.Channels.Http; HttpChannel hc = new .; Using can only be used with a namespace, not a class name Using is just syntactic sugar, does not link any code automatically. So System.Console.WriteLine can become Console.WriteLine
  • Slide 11
  • Slide 12
  • Files and Streams and Directories
  • Slide 13
  • Directories and Files Reading and Writing a Sequential Files Using OpenFileDialog and SaveFileDialog
  • Slide 14
  • System.IO Namespace Directory Class File Class StreamReader Class SteamWriter Class
  • Slide 15
  • Directory Class Exposes static methods for creating, moving, and enumerating through directories and subdirectories.
  • Slide 16
  • Directory Class using System; using System.IO; class Class1 { [STAThread] static void Main(string[] args) { string path = "c:\\MyDir"; string target = "c:\\TestDir"; if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); // Create Dir. } if (Directory.Exists(target)) { Directory.Delete(target, True); // Delete Directory } Directory.Move(path, target); // Move Directory File.CreateText(target + "\\myfile.txt"); // New File }
  • Slide 17
  • File Class Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.
  • Slide 18
  • File Class using System; using System.IO; class Class1 { [STAThread] static void Main(string[] args) { string target = "c:\\TestDir\\Test.Txt"; if (File.Exists(target)) { File.Delete(target ); } else { StreamWriter sw = File.CreateText(target); sw.WriteLine("Hello World"); sw.Close(); }
  • Slide 19
  • StreamReader Class Implements a TextReader that reads characters from a byte stream in a particular encoding.
  • Slide 20
  • StreamReader Class using System; using System.IO; class Class1 { [STAThread] static void Main(string[] args) { using (StreamReader sr = new StreamReader("TestFile.txt")) { string line; while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); }
  • Slide 21
  • SteamWriter Class Implements a TextWriter for writing characters to a stream in a particular encoding.
  • Slide 22
  • StreamWriter Class using System; using System.IO; class Class1 { [STAThread] static void Main(string[] args) { using (StreamWriter sw = new StreamWriter("TestFile.txt")) { sw.Write("This is the "); sw.WriteLine("header for the file."); sw.WriteLine("-------------------"); sw.Write("The date is: "); sw.WriteLine(DateTime.Now); }
  • Slide 23
  • Represents a common dialog box that displays the control that allows the user to open a file. This class cannot be inherited. OpenFileDialog
  • Slide 24
  • System.Windows.Forms Namespace OpenFileDialog SaveFileDialog
  • Slide 25
  • SaveFileDialog - Represents a common dialog box that allows the user to specify options for saving a file. This class cannot be inherited. SaveFileDialog
  • Slide 26
  • OpenFileDialog protected void button1_Click(object sender, System.EventArgs e) { Stream myStream; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "c:\\" ; openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ; openFileDialog1.FilterIndex = 2 ; openFileDialog1.RestoreDirectory = true ; if(openFileDialog1.ShowDialog() == DialogResult.OK) { if((myStream = openFileDialog1.OpenFile())!= null) { // Insert code to read the stream here. myStream.Close(); }
  • Slide 27
  • SaveFileDialog protected void button1_Click(object sender, System.EventArgs e) { Stream myStream ; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ; saveFileDialog1.FilterIndex = 2 ; saveFileDialog1.RestoreDirectory = true ; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { if ((myStream = saveFileDialog1.OpenFile()) != null) { // Code to write the stream goes here. myStream.Close(); }
  • Slide 28
  • Class Exercise FileIO Class
  • Slide 29
  • Slide 30
  • Databases and SQL Introduction Relational Database Model Structured Query Language (SQL) Basic: SELECT Query WHERE Clause ORDER BY Clause INSERT Statement UPDATE Statement DELETE Statement
  • Slide 31
  • Introduction Database: Integrated collection of data Database management system (DBMS) Provides mechanisms for storing and organizing data in a way that is consistent with databases format Allows storage and access to database without knowledge of internal representation Relational Databases most popular Use Structured Query Language to perform queries (search) and manipulate data Programming languages need an interface to interact with relational databases
  • Slide 32
  • Relational Database Model Logical representation of data: Relationships can be considered without concern for physical structure of data Composed of tables Rows called records Columns called fields Primary key: field that contains unique data Each record can be identified by at least one distinct value New sets made from queries called result sets
  • Slide 33
  • Relational Database Model numbernamedepartmentsalarylocation 23603Jones4131100New Jersey 24568Kerwin4132000New Jersey 34589Larson6421800Los Angeles 35761Myers6111400Orlando 47132Neumann4139000New Jersey 78321Stephens6118500Orlando record/row field/column primary key
  • Slide 34
  • Books Database Microsoft Access One Table Books Title Author Copyright Publisher Comments On Website
  • Slide 35
  • Structured Query Language (SQL) Used to request data (perform queries) and manipulate data
  • Slide 36
  • Structured Query Language (SQL) SQL Key WordDescription SELECTSelect (retrieve) fields from one or more tables. FROMTables from which to get fields. Required in every Select statement. WHERECriteria for selection that determines the rows to be retrieved. ORDER BYCriteria for ordering records.
  • Slide 37
  • Basic Select Query Extracts information from one or more tables in a database Format: Basic: SELECT * FROM tableName * extracts all columns To get specific fields use a comma separated list instead of * Example: SELECT * FROM Books
  • Slide 38
  • Where Clause Used to specify certain criteria in a query Basic form: SELECT * FROM tableName WHERE criteria Example: SELECT * FROM Books WHERE Author = Robert Parker Can use LIKE clause Used for pattern matching Uses wildcards *: zero or more characters take its place ?: exactly one character takes its place