System.IO Namespace Imports System.IO



Σχετικά έγγραφα
ΣΧΕΣΕΙΣ στην Northwind

Lab1 Ανάπτυξη απλής εφαρµογής. Solutions, Projects, GUI, Events, Debugging.

Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων. Εξάμηνο 7 ο

Προγραμματισμός Η/Υ. Ενότητα 12 η : Αρχεία Κειμένου. Ι. Ψαρομήλιγκος Χ. Κυτάγιας Τμήμα Λογιστικής & Χρηματοοικονομικής

Instruction Execution Times

ΓΡΑΜΜΙΚΟΣ & ΔΙΚΤΥΑΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ

(C) 2010 Pearson Education, Inc. All rights reserved.

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 24/3/2007

Εργαστήριο Δομημένος Προγραμματισμός (C#) Τμήμα Μηχανολογίας Νικόλαος Ζ. Ζάχαρης Καθηγητής Εφαρμογών

SOAP API. Table of Contents

Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων. Εξάμηνο 7 ο

1. ΕΛΕΓΧΟΙ ΕΓΚΥΡΟΤΗΤΑΣ ΣΕ ΜΙΑ ΦΟΡΜΑ

ΕΛΛΗΝΙΚΗ ΔΗΜΟΚΡΑΤΙΑ Ανώτατο Εκπαιδευτικό Ίδρυμα Πειραιά Τεχνολογικού Τομέα. Προγραμματισμός Η/Υ

Πώς εκτυπώνουμε μία λίστα από εγγραφές μίας Access database

ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΥΠΡΟΥ - ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ ΕΠΛ 133: ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΕΦΗΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΕΡΓΑΣΤΗΡΙΟ 3 Javadoc Tutorial

Οδηγίες Αγοράς Ηλεκτρονικού Βιβλίου Instructions for Buying an ebook

VBA ΣΤΟ WORD. 1. Συχνά, όταν ήθελα να δώσω ένα φυλλάδιο εργασίας με ασκήσεις στους μαθητές έκανα το εξής: Version ΗΜΙΤΕΛΗΣ!!!!

Threads. Components. (Download File My.Computer.Network.DownloadFile(" files/list.txt", "C:\Temp\list.

Εγκατάσταση λογισμικού και αναβάθμιση συσκευής Device software installation and software upgrade

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων. Εξάμηνο 7 ο

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 6/5/2006

Πώς εκτυπώνουμε μία λίστα εγγραφών από μία Access database (γενικός τρόπος)

Section 8.3 Trigonometric Equations

EE512: Error Control Coding

Στο εστιατόριο «ToDokimasesPrinToBgaleisStonKosmo?» έξω από τους δακτυλίους του Κρόνου, οι παραγγελίες γίνονται ηλεκτρονικά.

CYTA Cloud Server Set Up Instructions

Μdi form νέες Solution Explorer Add Windows Form. Startup form Solution Explorer Properties/Application/StartupForm. Add New Item

Dynamic types, Lambda calculus machines Section and Practice Problems Apr 21 22, 2016

MOCϦϣΎϬϴϠϋϝϮμΤϟϢΗϊΟήϤϟ. USE northwind SELECT employeeid, lastname, firstname, title FROM employees GO

Πρόβλημα 1: Αναζήτηση Ελάχιστης/Μέγιστης Τιμής

Δημιουργία Λογαριασμού Διαχείρισης Business Telephony Create a Management Account for Business Telephony

2 Composition. Invertible Mappings

department listing department name αχχουντσ ϕανε βαλικτ δδσϕηασδδη σδηφγ ασκϕηλκ τεχηνιχαλ αλαν ϕουν διξ τεχηνιχαλ ϕοην µαριανι

How to register an account with the Hellenic Community of Sheffield.

ΚΥΠΡΙΑΚΟΣ ΣΥΝΔΕΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY 21 ος ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ Δεύτερος Γύρος - 30 Μαρτίου 2011

Αρχές Τεχνολογίας Λογισμικού Εργαστήριο

Section 9.2 Polar Equations and Graphs

derivation of the Laplacian from rectangular to spherical coordinates

PortSip Softphone. Ελληνικά Ι English 1/20

Αρχεία Ένα αρχείο αποτελείται από μία σειρά ομοειδών δεδομένων που ονομάζονται λογικές εγγραφές (logical record)

Approximation of distance between locations on earth given by latitude and longitude

Homework 3 Solutions

2 η Εργαστηριακή Άσκηση

Αρχεία κειμένου και η VB.NET

Phys460.nb Solution for the t-dependent Schrodinger s equation How did we find the solution? (not required)

3.4 SUM AND DIFFERENCE FORMULAS. NOTE: cos(α+β) cos α + cos β cos(α-β) cos α -cos β

Physical DB Design. B-Trees Index files can become quite large for large main files Indices on index files are possible.

Κατανεμημένα Συστήματα. Javascript LCR example

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

Δομές Δεδομένων - Εργαστήριο 2. Λίστες

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 5: Component Adaptation Environment (COPE)

HOMEWORK 4 = G. In order to plot the stress versus the stretch we define a normalized stretch:

7η Εργαστηριακή Άσκηση: Προβολή εικόνας στη φόρμα με εκτέλεση ερωτήματος.

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 11/3/2006

The Simply Typed Lambda Calculus

MySQL + Γλώσσα Προγραμματισμού. Βάσεις Δεδομένων Ευαγγελία Πιτουρά 1

Modbus basic setup notes for IO-Link AL1xxx Master Block

Εργαστήριο 9. Styling with Javascript

1. ΑΝΟΙΞΤΕ ΤΟΝ ΠΙΝΑΚΑ CUSTOMER ΚΑΙ ΣΤΟ ΜΕΝΟΥ ΕΠΙΛΕΞΤΕ

10 η Διάλεξη Python Βάσεις δεδομένων στη python

PARTIAL NOTES for 6.1 Trigonometric Identities

SPEEDO AQUABEAT. Specially Designed for Aquatic Athletes and Active People

Other Test Constructions: Likelihood Ratio & Bayes Tests

Partial Trace and Partial Transpose

Advanced Subsidiary Unit 1: Understanding and Written Response

Math 6 SL Probability Distributions Practice Test Mark Scheme

Example Sheet 3 Solutions

The challenges of non-stable predicates

Section 7.6 Double and Half Angle Formulas

[1] P Q. Fig. 3.1

C.S. 430 Assignment 6, Sample Solutions

Concrete Mathematics Exercises from 30 September 2016

About these lecture notes. Simply Typed λ-calculus. Types

Χρειάζεται να φέρω μαζί μου τα πρωτότυπα έγγραφα ή τα αντίγραφα; Asking if you need to provide the original documents or copies Ποια είναι τα κριτήρια

Econ 2110: Fall 2008 Suggested Solutions to Problem Set 8 questions or comments to Dan Fetter 1

Γράψιμο και Διάβασμα σε φύλλο του Excel

ΙΚΤΥΩΤΟ ΜΟΝΤΕΛΟ (Network Model) Μαθ. # 15

6.1. Dirac Equation. Hamiltonian. Dirac Eq.

Capacitors - Capacitance, Charge and Potential Difference

ANSWERSHEET (TOPIC = DIFFERENTIAL CALCULUS) COLLECTION #2. h 0 h h 0 h h 0 ( ) g k = g 0 + g 1 + g g 2009 =?

Εργαστήριο Δομημένος Προγραμματισμός (C#) Τμήμα Μηχανολογίας Νικόλαος Ζ. Ζάχαρης Καθηγητής Εφαρμογών

Numerical Analysis FMN011

Τ.Ε.Ι. ΛΑΜΙΑΣ ΣΧΟΛΗ ΤΕΧΝΟΛΟΓΙΚΩΝ ΕΦΑΡΜΟΓΩΝ ΤΜΗΜΑ ΗΛΕΚΤΡΟΝΙΚΗΣ

Αποθηκευμένες Διαδικασίες Stored Routines (Procedures & Functions)

Congruence Classes of Invertible Matrices of Order 3 over F 2

Πώς μπορεί κανείς να έχει έναν διερμηνέα κατά την επίσκεψή του στον Οικογενειακό του Γιατρό στο Ίσλινγκτον Getting an interpreter when you visit your

Assalamu `alaikum wr. wb.

Practice Exam 2. Conceptual Questions. 1. State a Basic identity and then verify it. (a) Identity: Solution: One identity is csc(θ) = 1

the total number of electrons passing through the lamp.

Εργαστήριο Οργάνωσης Η/Υ. Δαδαλιάρης Αντώνιος

(Διαφάνειες Νίκου Βιδάκη)

ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΕΙΡΑΙΩΣ ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ. Βάσεις Δεδομένων (4 ο εξάμηνο) Εργαστήριο MySQL #2

Test Data Management in Practice

Potential Dividers. 46 minutes. 46 marks. Page 1 of 11

The new Remote Networks Regulatory Framework Το νέο Ρυθμιστικό Πλαίσιο των Απομακρυσμένων Δικτύων

Συστήματα Διαχείρισης Βάσεων Δεδομένων

b. Use the parametrization from (a) to compute the area of S a as S a ds. Be sure to substitute for ds!

. Εργαστήριο Βάσεων Δεδομένων. Triggers

HY150a Φροντιστήριο 3 24/11/2017

Transcript:

System.IO Namespace Imports System.IO ίνει την δυνατότητα να δουλέψεις µε αρχεία και δεδοµένα: DriveInfo,Directory,DirectoryInfo,File,FileInfo,FileSystemObject FileStream Path StreamReader/StreamWriter Controls OpenFileDialog SaveFileDialog FolderBrowserDialog Ολα τα Dialog Control έχουν την µέθοδο ShowDialog() Επιλογή Αρχείου- OpenFileDialog Βάζοντας πάνω στην φόρµα ένα OpenFileDialog control και καλώντας την OpenFileDialog1.ShowDialog(), θα εµφανιστεί το παραπάνω παράθυρο, στο οποίο διακρίνονται δυο βασικές ιδιότητες το Filename και το φίλτρο επιλογής τύπου αρχείου (Filter). If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then MsgBox( File name given: + OpenFileDialog1.Filename ) Τo Filename property περιέχει όλο το path. Η απάντηση στον διάλογο µπορεί νάναι της µορφής π.χ. Windows.Forms.DialogResult.OK [1]

Filter Property - Φίλτρο Σε ζεύγη (περιγραφή pattern), που µεταξύ τους χωρίζονται µε π.χ. Text files (*.txt) *.txt All files (*.*) *.* "Databases files (*.mdb;*.mda;*.mde) *.mdb;*.mda;*.mde" multiple pattern µε ; OpenFileDialog1.Filter = "Text files (*.txt) *.txt All files (*.*) *.*" OpenFileDialog1.FileName = "" Πολλές άλλες Properties.. Set InitialDirectory, RestoreDirectory. Εvents FileOK event HelpRequest event 1.Ανάγνωση Αρχείου ( My.Computer.FileSystem sr = New StreamReader(filename) File.OpenText My.Computer.FileSystem.OpenTextFileReader) Ανάγνωση αρχείου κειµένου Υπάρχουν διάφοροι τρόποι για ανάγνωση αρχείου, οι δύο πιο γνωστοί είναι µε την χρήση του My Namespace και του component My.Computer.FileSystem (µε µεθόδους ανάγνωσης ReadAllBytes και ReadAllText). της κλάσης StreamReader (µε µεθόδους ανάγνωσης Read, ReadLine και ReadToEnd). My.Computer.FileSystem component / ReadAllText Ολο τo κείµενο του επιλεχθέντος αρχείου µπορεί να αναγνωστεί (µε µιας) και να επιστραφεί σε ένα String που µπορεί να αντιγραφεί π.χ. µέσα σε ένα textbox control (µε ιδιότητες ScrollBars=Both, Multiline=True), όπως πιο κάτω: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles Button1.Click Dim s As String [2]

If (OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK) Then s = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName) TextBox1.Text = s MsgBox("An error occurred." & vbcrlf & ex.message) End End Sub ( ιαβάζοντας ένα text αρχείο µε το Μy.Computer.FileSystem.ReadAllText και ΟpenFileDialog ) The ReadAllText method copies the entire contents of the specified text file to a string variable or object (in this case, a string variable named s), so in terms of performance and coding time, ReadAllText is faster than reading the file one line at a time (όπως µε την ReadLine του StreamReader βλέπε πιο κάτω) (ReadAllText -> Insert Snippet / Fundamentals Collections, Data Types, File System, Math / File System Processing Drives, Folders, And Files / Read Text From A File ) The StreamReader Class / ReadΤοEnd ReadLine Imports System.IO Μέθοδοι ανάγνωσης Read, ReadLine, ReadToEnd(), διαβάζει το stream και το επιστρέφει σε µια µεταβλητή String. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_ System.EventArgs) Handles Button1.Click Dim sr As StreamReader If (OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK) Then sr = New StreamReader(OpenFileDialog1.FileName) 'sr=file.opentext(openfiledialog1.filename) 'Reads the stream from the current position to the end of the stream. TextBox3.Text = sr.readtoend() MsgBox("An error occurred." & vbcrlf & ex.message) End sr.close() End Sub ( ιαβάζοντας ένα text αρχείο µε StreamReader1.ReadToEnd και ΟpenFileDialog ) You can also use a combination of the My namespace and the StreamReader class. Dim sr As StreamReader Dim AllText, LineOfText As String sr =My.Computer.FileSystem.OpenTextFileReader(OpenFileDialog1.FileName) 'sr = File.OpenText(OpenFileDialog1.FileName) Do Until sr.endofstream 'read lines from file LineOfText = sr.readline() 'add each line to the AllText variable AllText = AllText & LineOfText & vbcrlf Loop [3]

TextBox1.Text = AllText 'display file sr.close() ( ιαβάζοντας ένα text αρχείο µε StreamReader1.ReadLine και ΟpenFileDialog και ΜyNamespace) Mπορείτε να χρησιµοποιήσετε το OpenFileDialog control µε την συνάρτηση FileOpen (αριθµός αρχείου, διαδροµή, κατάσταση) - αριθµός 1..255- και τα LineInput, EOF και FileClose Dim All As String =, Line As String= FieOpen(1,OpenFileDialog1.FileName,OpenMode.Input) Do Until EOF(1) Line= LineInput(1) All = All & Line & vbcrlf Loop FileClose(1) ( ιαβάζοντας ένα text αρχείο µε FileOpen και ΟpenFileDialog ) µπορείτε επίσης να χρησιµοποιήσετε το OpenFileDialog control µε την συνάρτηση PrintLine Tέλος µπορείτε και να διαβάσετε ένα υπάρχον text αρχείο σας χωρίς χρήση διαλόγου OpenFileDialog: Dim lfile As String = "C:\Documents and Settings\All Users\allfiles.txt" ' Create an instance of StreamReader to read from a file. Dim SR As StreamReader = New StreamReader(lfile) Dim str As String 'Read and display the lines from the file until the end of the file Do str = SR.ReadLine If Not str = String.Empty Then ListBox1.Items.Add(str) Loop Until str Is Nothing SR.Close() MsgBox("Reading file error... " + vbcrlf + ex.message()) End ( ιαβάζοντας ένα text αρχείο µε StreamReader και εµφανίζοντας τις γραµµές του σε ένα ListBox ) Aνάθεση Ονόµατος Αρχείου- SaveFileDialog SaveFileDialog παρόµοιο µε OpenFileDialog ίνει δυνατότητα απαγόρευσης overwritings. Εvents FileOK event Private Sub SaveFileDialog1_FileOk(ByVal sender As Object, ByVal e As_ System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk If MessageBox.Show("Are you sure:", "SUR", MessageBoxButtons.YesNo) =_ Windows.Forms.DialogResult.No Then e.cancel = True End Sub HelpRequest event [4]

2. Σώσιµο Αρχείου ( My.Computer.FileSystem sw = New StreamWriter(filename) File.CreateText My.Computer.FileSystem.OpenTextFileWriter) Aποθήκευση αρχείου κειµένου My.Computer.FileSystem component / WriteAllText Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles Button2.Click SaveFileDialog1.Filter = "Text files (*.txt) *.txt If (SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK) Then My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName,_ TextBox2.Text,False) End Sub ( ηµιουργώντας ένα text αρχείο µε Μy.Computer.FileSystem.WriteAllText και SaveFileDialog ) WriteAllText µε 3 παραµέτρους: 1. το όνοµα του αρχείου που επιλέγει ο χρήστης (εδώ, ο χρήστης δίνει το όνοµα του αρχείου µέσω του SaveFileDialog1). 2. από πού θα πάρει τα δεδοµένα για να τα γράψει στο αρχείο (εδώ, το Τext του TextBox2). 3. προσδιορίζει αν θα γίνει προσάρτηση στο τυχόν υπάρχον κείµενο ή όχι (append the text or overwrite the existing text - εδώ η τιµή False προσδιορίζει ότι θα γίνει overwrite the existing text). The StreamWriter Class / WriteLine StreamWriter Class παρόµοια µε τηνstreamreader Class Μέθοδοι εγγραφής, Write(String), WriteLine(String) - γράφει το string µε line terminator στο text stream. Imports System.IO ηµιουργώντας/ ιαβάζοντας ένα text αρχείο µε StreamWriter και WriteLine. /StreamReader και ReadLine Aποθήκευση στοιχείων (γραµµών) Πίνακα της βάσης σε αρχείο Dim sw As StreamWriter sw = File.CreateText(SaveFileDialog1.FileName) For Each dr As DataRow In NorthwindDataSet.Tables("Customers").Rows sw.writeline( dr("companyname").tostring ()) Next sw.close() Aνάγνωση στοιχείων από αρχείο για εισαγωγή (γραµµών) σε Πίνακα της βάσης Dim sr As StreamReader sr = File.OpenText(SaveFileDialog1.FileName) Dim P As Array Do Until sr.endofstream 'read lines from file LineOfText = sr.readline() P = strline.split(",")'split fields into Array 'εισαγωγή στον Πίνακα µε την µέθοδο του Adapter.Insert(P(0), P(1),. ) Loop sw.close() [5]

4.Directory Information The following examples demonstrates some of the main members of the DirectoryInfo class. Imports System.IO ' Specify the directories you want to manipulate. Dim dir As DirectoryInfo = New DirectoryInfo("c:\MyDir") ' Determine whether the directory exists. If dir.exists Then MsgBox("That path exists already.") Return 'Create the directory. dir.create() MsgBox("The directory was created successfully.") MsgBox(String.Format("Create process failed: {0}", ex.tostring())) End... ' to delete the directory. Dim dir As DirectoryInfo = New DirectoryInfo("c:\MyDir") dir.delete() MsgBox("The directory was deleted successfully.") MsgBox(String.Format("Delete process failed: {0}", ex.tostring())) End 'Directory s files (subfolders not included). Dim dir As DirectoryInfo = New DirectoryInfo("c:\Documents And Settings\All Users") If Not dir.exists Then MsgBox("Dir not found..") Else MsgBox("Dir s file ) For Each fi As FileInfo In dir.getfiles() MsgBox("Name:" + fi.name & " size:" & fi.length) Next. Μια Function που υπολογίζει το µέγεθος ενός Directory µπορεί να είναι η παρακάτω: Public Shared Function DirSize(ByVal d As DirectoryInfo) As Long Dim Size As Long = 0 ' Add file sizes. For Each fi As FileInfo In d.getfiles() Size += fi.length Next fi ' Add subdirectory sizes. For Each dir As DirectoryInfo In d.getdirectories() Size += DirSize(dir) ' ΑΝΑ ΡΟΜΗ για κάθε subfolder Next dir Return Size [6]

End Function 'DirSize 5.SQL s Queries USE Northwind Action queries ( INSERT,DELETE,UPDATE ) DELETE Customers WHERE Country IS NULL ExecuteNonQuery DELETE Orders WHERE OrderDate < 1/1/1998 (But you ll get an error, because you can t delete rows from the Orders table that are referenced by rows in the Order Details table) INSERT Customers (CustomerID, CompanyName) VALUES ( Cust1, Company A ) INSERT INTO SelectedProducts SELECT * FROM Products WHERE CategoryID = 4 (The INSERT INTO statement allows you to select columns from one table and insert them into another one with the same structure as the output of the selection query.note that you need not create the new table ahead of time) UPDATE Customers SET Country= United Kingdom WHERE Country = UK Aggregate/scalar queries ( SELECT COUNT(),SUM(),AVG(),MIN(),MAX() ) SELECT COUNT(CustomerID) FROM Customers WHERE Country = Germany The aggregate functions are used to summarize data from one or more tables. SELECT SUM(Quantity * UnitPrice * (1 - Discount)) FROM [Order Details] WHERE ProductID = 11 ExecuteScalar SELECT SUM(Quantity), SUM(Quantity * UnitPrice * (1 - Discount)) FROM [Order Details] WHERE ProductID = 11 Select queries ( SELECT ) SELECT DISTINCT CompanyName FROM Customers WHERE Country = Germany You can also combine multiple conditions with the AND/OR operator. ExecuteReader SELECT * FROM Products WHERE UnitPrice IS NULL SELECT CompanyName, ContactName, Country, City FROM Customers ORDER BY Country, City SELECT Orders.OrderID, [Order Details].ProductID,[Order Details].UnitPrice,[Order Details].Quantity (1 - [Order Details].Discount) AS SubTotal FROM Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID (Because the Order Details table s name contains spaces, it s embedded in square brackets). SELECT CompanyName FROM Customers WHERE Country IN ( Germany, Austria, Switzerland ) SELECT OrderID, OrderDate, CompanyName FROM Orders WHERE (OrderDate BETWEEN 1/1/1997 AND 12/31/1997 ) JOIN (Left,Right, Inner) FROM (left table) LEFT JOIN (right table) ON (left table).(field) = (right table).(field) SELECT Customers.CompanyName, Orders.UnitPrice [7]

FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID Programming with ADO.NET ADO.NET provides two basic methods of accessing data: Stream-based data access (DataReader object) Set-based data access (DataSet Structure) 1. DataReader Submitting the data to the database with the stream-based approach, you must create the appropriate INSERT/UPDATE/DELETE statements and then execute them against the database. The client application reads the data returned by a query through the DataReader object and must store it somehow at the client. ADO.NET provides three core classes for accessing databases: the Connection, the Command, and the DataReader. The Connection Using the SQL Server 2008 DBMS - Using OleDB Server - Imports System.Data.SqlClient ή Imports System.Data.OleDb Use the Connection object to execute statements against the database and then close the connection To connect to a data source, you must specify a connection string the parameters of which might differ for each provider and data source: Dim CNstring As String CNstring = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source c:\test\northwind.mdb;" 'ή CNstring = "Data Source = localhost; Initial Catalog = Northwind; uid = user name;password = user password") (εναλλακτικά : CNstring = InputBox("Please enter a Connection String", "CN", CNstring) If CNstring.Trim = "" Then Exit Sub Dim CN As New OleDbConnection (CNstring) 'ή As New SqlConnection(CNstring) If CNstring.Trim = "" Then Exit Sub CN.Open() If CN.State = ConnectionState.Open Then MsgBox("Connected to database " & CN.Database) MsgBox("Failed to open connection " & vbcrlf & ex.message) End If CN.State = ConnectionState.Open Then CN.Close() The Command Dim SqlString As String SqlString = "UPDATE Categories SET CategoryName = 'Tralala lalo'" & " WHERE CategoryID = 3" 'MsgBox(SqlString) [8]

Dim CMD As New OleDbCommand(SqlString,CN) ' ή SqlCommand If CN.State = ConnectionState.Closed Then CN.Open() Dim rows As Integer = CMD.ExecuteNonQuery() If rows = 1 Then MsgBox("Table Categories updated successfully") Else MsgBox("Failed to update the Categories table") CMD.CommandText = "SELECT COUNT(*) FROM Categories" count = CMD.ExecuteScalar() MsgBox("Number of categories records: " & count.tostring()) MsgBox("Operation failed " & ex.message & " " & CN.Database) End If CN.State = ConnectionState.Open Then CN.Close() The Reader SqlString = "SELECT * FROM Categories" CMD.CommandText = SqlString If CN.State = ConnectionState.Closed Then CN.Open() Dim Reader As OleDbDataReader ' ή SqlDataReader Dim str As String Reader = CMD.ExecuteReader() While Reader.Read() ' process the current row in the result set str = Convert.ToString ( Reader.Item("CategoryName") ) & vbtab str & = Convert.ToString( Reader.Item("Description") ) & vbtab ListBox1.Items.Add(str) End While MsgBox("Operation failed " & ex.message) End If CN.State = ConnectionState.Open Then CN.Close() HasRows This is a Boolean property that specifies whether there s a result set to read data from. If the query selected no rows at all, the HasRows property will return False. FieldCount This property returns the number of columns in the current result set. Note that the DataReader object doesn t know the number of rows returned by the query. Read This method moves the pointer in front of the next row in the result set. Use this method to read the rows of the result set, usually from within a While loop GetValue If you can t be sure about the type of a column, use the GetValue method GetName Use this method to retrieve the name of a column, which must be specified by its order in the result set. IsDbNull This method returns True if the column specified by its ordinal in the current row is Null. [9]

2. DataSet (The process of building data-driven applications isn t complicated and to a large extent is abstracted by the Connection, Command, and DataReader classes. The problem with these classes is that they don t offer a consistent method for storing the data at the client. You can store the data in a ListBox control. You can also create an ArrayList of custom objects. The issue of storing data at the client isn t pressing when the client application is connected to the database and all updates take place in real time. As soon as the user edits a row, the row is submitted to the database and no work is lost.) To simplify the storage of data at the client, ADO.NET offers a powerful mechanism, the DataSet. The set-based approach uses the same objects as the stream-based approach behind the scenes, and it abstracts most of the grunt work required to set up a link to the database, retrieve the data, and store it in the client computer s memory. You can also create DataSets and the supporting objects with the visual tools of the IDE. You can think of the DataSet as a small database (it isn t actually) that lives in memory. The DataSet lets you copy a small section of the database at the client, work with it, and then submit the changes made at the client back to the database. Actually, it s not the DataSet that submits the changes, but a class that s used in tandem with the DataSet: the DataAdapter class. DataSets are filled with DataAdapters,which is a container for Connection and Command objects. There are two ways to create a DataSet: You can use the visual tools of Visual Studio or Create a DataSet entirely from within your code typed DataSet untyped DataSet untyped DataSet Products1.Products.Rows(0).Item( ProductName ) typed DataSet Dim productrow As Products.ProductsRow = Products1.Products.Rows(0) productrow.productname productrow.unitprice The visual tools generate a number of classes on-the-fly, such as the ProductsRow class, and expose them to your code. As soon as you enter the string productrow and the following period in the code window, you will see the members of the ProductsRow class, which include the names of the columns in the corresponding table. Dim CNstring As String CNstring = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source =c:\test\northwind.mdb; " Dim CN As New OleDbConnection(CNstring) Dim DS As New DataSet Dim DACategories As New OleDbDataAdapter ' ή SqlDataAdapter Dim SqlString As String = "SELECT * FROM Categories" DACategories.SelectCommand = New OleDb.OleDbCommand(SqlString, CN) ' ή SqlClient.SqlCommand (SqlString) DACategories.Fill(DS, "Categories") Dim str As String For Each r As DataRow In DS.Tables("Categories").Rows ' process prodrow row: π.χ. γραµµή 5 -> DS.Tables("Categories").Rows(5) str = r.item("categoryname").tostring()str &= vbtab & r.item("description").tostring()listbox1.items.add(str) Next [10]

Eστω ότι το Dataset λέγεται NorthwindDataSet και έχει γίνει µε Visual Tools. To παρακάτω loop γράφει όλα τα CategoryNames του πίνακα Categories µέσω StreamWriter στο αρχείο µε όνοµα "onoma.txt"). Dim strwr As StreamWriter strwr = File.CreateText("c:\test\onoma.txt") Dim str As String For Each r As DataRow In NorthwindDataSet.Tables("Categories").Rows str= r ("CategoryName").ToString() strwr.writeline(str) Next strwr.close() Προσπέλαση σε ένα πίνακα του dataset: NorthwindDataSet.Tables(0) ή NorthwindDataSet.Tables ("Categories") ή NorthwindDataSet.Categories Προσπέλαση σε γραµµή πίνακα: NorthwindDataSet.Tables("Categories").Rows(5) '6 η γραµµή Προσπέλαση σε στοιχείο γραµµής πίνακα: NorthwindDataSet.Tables("Categories").Rows(5).Item(1) ή NorthwindDataSet.Tables("Categories").Rows(5).Item("CategoryName") '6 η γραµµή,categoryname Π.χ. For Each r As DataRow In NorthwindDataSet.Categories.Rows ' process Categories Row r: ' r.item("categoryname") ' r.item("description"),and so on Next Παρακάτω εισάγεται µια γραµµή στον πίνακα Categories µέσω του CategoriesTableAdapter - φυσικά αν δηµιουργεί duplicate values δεν γίνεται εισαγωγή Dim LineofText As String ="Katigoria9,Perigrafi9" Dim P As Array = LineofText.Split(",") 'P(0)="Katigoria9" P(1)="Perigrafi9" Me.CategoriesTableAdapter.Insert(P(0), P(1), Nothing) MsgBox(ex.Message()) End Aπαιτείται νέο Fill για να εµφανιστούν οι διαφορές στον client [11]