Αναλυτικά ΣΧΕΣΕΙΣ στην Northwind Σχέση Πίνακας 1 Parent Table Πίνακας 2 Child Table Κey Foreign Key CategoriesProducts Categories Products CategoryID CategoryID SuppliersProducts Suppliers Products SupplierID SupplierID OrdersOrder Details Orders Orders Details OrderID OrderID CustomersOrders Customers Orders CustomerID CustomerID EmployeesOrders Employees Orders EmployeeID EmployeeID ProductsOrder Details Products Orders Details ProductID ProductID ShippersOrders Shippers Orders ShipperID ShipVia Ανοίξτε το VISUAL STUDIO και δηµιουργήστε ένα αντίγραφο του lab3 project Για να ορίσετε το DataSet σας επιλέξτε µόνο από τους δικούς σας πίνακες της northwind (8 σε σύνολο) [1]
Σύρετε σε µια φόρµα (Form1) ένα πίνακα π.χ. Categories και παρατηρήστε στο κώδικα της φόρµας (Form1.vb) την ρουτίνα Load ο adapter γεµίζει τον πίνακα Categories. Me.CategoriesTableAdapter.Fill(Me.NorthwindDataSet.Categories) DATABASE UPDATE TableAdapter--- FILL DATASET BINDINGSOURCE ==== Για να γίνει «populate» ένας πίνακας του dataset χρειάζεται ο ADAPTER αυτού του πίνακα Dim CategoriesTableAdapter As New NorthwindDataSetTableAdapters.CategoriesDataAdapter Ο adapter CategoriesTableAdapter παίρνει τα data από την database (fill) και επιστρέφει πίσω τις διορθωμένες εγγραφές (update) Dim DS As New NorthwindDataSet Dim categories As Ιnteger = γυρίζει 0,1,πιο μεγάλο CategoriesTableAdapter.Fill ( Me.DS.Categories ) Private Sub CategoriesForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Me.CategoriesTableAdapter.Fill (Me.NorthwindDataSet.Categories) Στο design της φόρµας πατήστε το βελάκι του adapter να δείτε τις επιλογές του Επιλέξτε τον DataSet Designer. Παρατηρήστε τις σχέσεις µεταξύ των πινάκων (και ΑΝ δεν υπάρχουν βάλτε µερικές βλ. πιο κάτω / PostGres) επιλέγοντας το επιθυµητό πεδίο και σύροντας το ποντίκι ή Αdd/Relation). [2]
BINDING (2 πίνακες στην φόρµα) CategoriesProducts Products Properties στο ProductsBindingSource 1) Περίπτωση Συνδεδεμένων Πινάκων (Categories + CategoriesProducts) Products Binding DATA SOURCE: CategoriesBindingSource DATA MEMBER: CategoriesProducts (όνοµα της σχέσης) 2) Περίπτωση MH Συνδεδεμένων Πινάκων (Categories Products) Products Binding DATA SOURCE: NorthwindSource DATA MEMBER: Products [3]
O adapter γεμίζει fill- τον πίνακα με μια query της μορφής SELECT CategoryID, CategoryName, Description FROM Categories ΑΝΑΖΗΤΗΣΗ Εστω ότι θέλουμε να γίνει αναζήτηση κατηγορίας με κριτήρια παραμετρικά (που δίνονται από τον χρήστη). Θα φτιάξουμε μια query έστω FillByCompanyName SELECT CategoryID, CategoryName, Description FROM Categories WHERE CategoryName LIKE? Το κτίσιμο της νέας query γίνεται στον adapter με add/query ( αρχείο NorthwindDataSet.xsd) Κάθε νέα query εμφανίζεται στο σχήμα κάτω από τον adapter Η προεπειλεχθείσα Fill μπορεί να αλλαχθεί στην φόρμα με την FillByCompanyName ώστε να δώσει όλες τις κατηγορίες που στο όνομα περιέχουν π.χ. ένα γράμμα έστω το e Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Me.CategoriesTableAdapter.Fill(Me.Adelab5DataSet.Categories) Me.CategoriesTableAdapter.FillByCompanyName(Me. NorthwindDataSet.Categories, "%e%") Αν τώρα μέρος του CategoryName δίνεται μέσω ενός ΤextΒox1 τότε θα έχουμε Me.CategoriesTableAdapter.FillByCompanyName (Me.NorthwindDataSet.Categories,_ "%" & TextBox1.Text & "%") [4]
Μπορεί να χρησιμοποιηθεί ένα ΤoolStrip1 control με πάνω τα εξής ToolStripLabel1 ετικέτα π.χ. Company Name: ToolStripTextBox1 να εισάγετε η επιλογή αναζήτησης ToolStripButton1 να επιζητείται η αναζήτηση Public Class Form1 Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click Try Me.CategoriesTableAdapter.Fill(Me. NorthwindDataSet.Categories,_ "%" & CategoryNameToolStripTextBox1.Text & "%") Catch ex As System.Exception System.Windows.Forms.MessageBox.Show(ex.Message) End Try End Class ΠΡΟΣΘΗΚΗ ΝΕΟΥ ΠΙΝΑΚΑ ΑDAPTER Στο σχήμα μπορεί να προστεθεί νέος TableAdapter (με δεξί κλικ) [5]
Ο νέος πίνακας (renamed Cities), μπορεί να χρησιμοποιηθεί πλέον στις φόρμες. FINDING Dim cat As NorthwindDataSet.Categories.Rows The most useful method of the typed DataTable is the FindByID method, which locates a rowby its ID in the DataTable. To locate a product by its ID, call the FindByProductID method passing a product ID as argument. The method returns a ProductsRow object that represents the matching product. The method s return value is not a copy of the found row, but a reference to the actual row in the DataTable, and you can edit it. [6]
Private Sub bttnupdate Click(...) Handles bttnupdate.click Dim selproduct As NorthwindDataSet.ProductsRow Dim RND As New System.Random selproduct = DS.Products.FindByProductID(RND.Next(1, 77)) Dim newprice As Decimal newprice = Convert.ToDecimal( InputBox_ ( Enter product s new price, selproduct.productname, selproduct.unitprice.tostring )) selproduct.unitprice = newprice This code - behind the Update Products button -, selects a product at random by its ID and prompts the user for a new price.then it sets the UnitPrice field to the user-supplied value. FILTERING The two most interesting members of the BindingSource class are the Find method and the Filter property. The Filter property is set to an expression similar to the WHERE clause of an SQL statement to filter the data on the grid. Place a new button on the form, set its caption to Filter and its name to bttnfilter, and insert the following statements in its Click even handler to filter the rows of the grid with their product name: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim filter As String 'filter = InputBox("Give Categories Name") 'CategoriesBS.Filter = "CategoryName LIKE '%" & filter.trim & "%'" filter = InputBox("Give Categories ID") CategoriesBindingSource.Filter = "CategoryID = " & filter Aναίρεση του φίλτρου CategoriesBS.Filter = "" SORTING CategoriesBindingSource.Sort = "CategoryID" ή και CategoriesBindingSource.Sort = "CategoryID DESC" [7]