Chapter 3 windows database applications

15 235 0
Chapter 3  windows database applications

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter Windows Database Applications Advanced Programming Using Visual Basic.NET Universal Data Access (UDA) • Microsoft’s strategy for accessing data for multiple providers • Goal is to access any type of data from any application on any type of computer Visual VisualBasic Basic and and Database Database Applications Applications 3- Giáo Viên: Nguyễn Văn Thắng OLEDB • Technology designed to implement the UDA concept • Standardized interface that allows reference to data from any source using the same programming tools Visual VisualBasic Basic and and Database Database Applications Applications 3- ADO.NET Giáo Viên: Nguyễn Văn Thắng ActiveX ActiveXData DataObjects Objectsà àADO ADO • Microsoft’s latest database object model • Allows VB programmers to use a standard set of objects to refer to data from any source • NET approach uses disconnected datasets with common data representation (data types) from multiple sources Visual VisualBasic Basic and and Database Database Applications Applications 3- Giáo Viên: Nguyễn Văn Thắng ADO.NET XML XMLisisan anindustry industrystandard standard format formatfor forstoring storingand and transferring transferringdata dataover over multiple multipleplatforms platforms • NET framework is integrated with XML (Extensible Markup Language) • Multitier applications that use disconnected datasets provide for – Flexibility adapt to changes in data or presentation tier – Scalability handle increases in the number of users and servers Visual VisualBasic Basic and and Database Database Applications Applications 3- Giáo Viên: Nguyễn Văn Thắng ADO.NET Components • Data Providers – manipulate the data using SQL statements or stored procedures – SQLClient for SQL Server – OleDbClient for all other database formats – An ODBC provider is available for download Visual VisualBasic Basic and and Database Database Applications Applications 3- Giáo Viên: Nguyễn Văn Thắng ADO.NET Components • DataSet Objects holds a copy of the data in memory • Dataset objects can be populated with data from many sources • Regardless of the data source, code handles DataSet objects the same • Dataset objects hold one or more DataTable objects Visual VisualBasic Basic and and Database Database Applications Applications 3- Giáo Viên: Nguyễn Văn Thắng A Sample DataTable • Each row represents a record of data for one entity • Each column represents a different element of data field Visual VisualBasic Basic and and Database Database Applications Applications 3- Giáo Viên: Nguyễn Văn Thắng DataTable Characteristics • A primary key field (or combination of fields) uniquely identifies each record • A relational database contains multiple tables • Relationships between tables are established using a foreign key Visual VisualBasic Basic and and Database Database Applications Applications 3- Giáo Viên: Nguyễn Văn Thắng Visual VisualBasic Basic and and Database Database Applications Applications The DataSet Object Model DataSet DataTable Collection DataTable DataRowCollection DataColumnCollection ParentRelations ChildRelations ConstraintCollection DataView DataRelationCollection 3- 10 Giáo Viên: Nguyễn Văn Thắng Data Provider Objects • Connection object – link to a data source • DataAdapter object – handles retrieving and updating data in a DataSet object • Command object – storeprocedure and executes SQL statements A NET NETdata dataprovider providerisisaaset set • DataReader object – ofAofcomponents componentsdesigned designed for forfast fastaccess accessand and for read-only access manipulation manipulationofofdata data Visual VisualBasic Basic and and Database Database Applications Applications 3- 11 Giáo Viên: Nguyễn Văn Thắng Use the DataSet object • To transfer data between tiers • To manipulate the data without an open connection • To relate data from multiple sources • To bind data to a Windows form Visual VisualBasic Basic and and Database Database Applications Applications 3- 12 Giáo Viên: Nguyễn Văn Thắng Visual VisualBasic Basic and and Database Database Applications Applications NET Data Provider Objects NET Data Provider Connection Command Data Adapter SelectCommand InsertCommand UpdateCommand DeleteCommand 3- 13 DataReader XML Data Giáo Viên: Nguyễn Văn Thắng XML XMLisisan anindustry industrystandard standard format for storing format for storingand and transferring transferringdata data • XML is generated automatically • Data stored in XML is all text, identified by tags • XML files can be edited by any text editor • Tags identify fields by name 172-32-1176 Visual VisualBasic Basic and and Database Database Applications Applications 3- 14 Giáo Viên: Nguyễn Văn Thắng XML Schema File • Describes the fields, data types, and constraints • View dataset schema in the xsd file found in the Solution Explorer Visual VisualBasic Basic and and Database Database Applications Applications 3- 15 Giáo Viên: Nguyễn Văn Thắng XML Data Format Advantages • Strong data typing allows for proper handling of data • ADO.NET can treat XML data as objects • Data handling in XML and ADO.NET executes faster Visual VisualBasic Basic and and Database Database Applications Applications 3- 16 Giáo Viên: Nguyễn Văn Thắng Microsoft Database Files • Access – uses the Jet Engine – Designed for single users or small networked databases • SQL Server – Supports many users – More robust security and reliability • MSDE (Microsoft Data Engine) – desktop version of SQL Server MSDE MSDEand and SQL SQLServer Server 3- 17 Giáo Viên: Nguyễn Văn Thắng Obtaining Data Set up a connection establishes a link to a data source Set up a data adapter handles retrieving and updating the data Add controls to the form and set properties to bind controls to the fields in the DataSet object Write VB code to fill the DataSet object MSDE MSDEand and SQL SQLServer Server 3- 18 Giáo Viên: Nguyễn Văn Thắng Obtaining Data Data Source Web Form Connection Data Adapter DataSet Windows Form MSDE MSDEand and SQL SQLServer Server Use UseServer ServerExplorer Explorerto toselect select and anddrag dragfields fieldsto tothe theform form Connection Connectionand andadapter adapter components componentsare areautomatically automatically added addedto tothe thecomponent componenttray tray 3- 19 Giáo Viên: Nguyễn Văn Thắng Display the DataSet in the DataGrid MSDE MSDEand and SQL SQLServer Server 3- 20 Giáo Viên: Nguyễn Văn Thắng 10 Populating Combo Boxes with Data • Set the DataSource property – Connects to the dataset • Set the DisplayMember property – Connects to the field name • Use the Fill method to load the list daEmployee.Fill(DsEmployee1) Displaying DisplayingData Data in inIndividual Individual Fields Fields 3- 21 Giáo Viên: Nguyễn Văn Thắng Sorting Data for the ListBox • Use the DataView object – Virtual view of an existing table • Select the DataView tool from the Data section of the toolbox Displaying DisplayingData Data in inIndividual Individual Fields Fields 3- 22 Giáo Viên: Nguyễn Văn Thắng 11 Creating a Parameterized Query • Use the WHERE clause in the SQL SELECT statement to specify the criteria • Use a wildcard in place of the actual value SELECT SELECT emp_id, emp_id, fname, fname, minit, minit, lname, lname, job_id, job_id, job_lvl, job_lvl, hire_date hire_date FROM FROM employee employee WHERE WHERE lname lname == @lname @lname Displaying DisplayingData Data in inIndividual Individual Fields Fields 3- 23 Giáo Viên: Nguyễn Văn Thắng Using the Query Builder Displaying DisplayingData Data in inIndividual Individual Fields Fields 3- 24 Giáo Viên: Nguyễn Văn Thắng 12 Binding Individual Controls to Data Fields • Use the DataBindings object Displaying DisplayingData Data in inIndividual Individual Fields Fields 3- 25 Giáo Viên: Nguyễn Văn Thắng Adding an Expression to the DataSet Schema Displaying DisplayingData Data in inIndividual Individual Fields Fields 3- 26 Giáo Viên: Nguyễn Văn Thắng 13 Using the ValueMember Property of Combo Boxes • DisplayMember property determines the field to display in the list • ValueMember property holds record’s unique field value • Make the record key field the search parameter daEmployee.SelectCommand.Parameters(“@emp_id”).Value daEmployee.SelectCommand.Parameters(“@emp_id”).Value == cboNames.SelectedValue.ToString cboNames.SelectedValue.ToString Displaying DisplayingData Data in inIndividual Individual Fields Fields 3- 27 Giáo Viên: Nguyễn Văn Thắng Multiple Tier Projects • Separate database access from the user interface • Create a data component as a separate tier User Interface (Form) DataSet Connection DataAdapter Multiple Multiple Tiers Tiers 3- 28 Methods Giáo Viên: Nguyễn Văn Thắng 14 Binding to Combo Boxes • Set the combo box DataSource property to the data view • Set the DisplayMember and ValueMember properties from fields in the data view Multiple Multiple Tiers Tiers 3- 29 Giáo Viên: Nguyễn Văn Thắng DataBindings for Multiple Tiers (phần sinh viên tự Multiple Tiers tham khảo sách)Multiple Tiers • Set the DataBindings property in code using the DataBindings.Add method • General Form ControlName.DataBindings.Add(“text”, ControlName.DataBindings.Add(“text”, DataSource, DataSource, “FieldName”) “FieldName”) • Examples lblEmployeeID.DataBindings.Add(“text”, lblEmployeeID.DataBindings.Add(“text”, dsEmployee.Tables(“employee”), dsEmployee.Tables(“employee”), “emp_id”) “emp_id”) lblHireDate.DataBindings.Add(“text”, lblHireDate.DataBindings.Add(“text”, dsEmployee.Tables(0), dsEmployee.Tables(0), “hire_date”) “hire_date”) • Bind fields in the Form_Load event procedure or another event procedure 3- 30 Giáo Viên: Nguyễn Văn Thắng 15 [...]... Data in inIndividual Individual Fields Fields 3- 23 Giáo Viên: Nguyễn Văn Thắng Using the Query Builder Displaying DisplayingData Data in inIndividual Individual Fields Fields 3- 24 Giáo Viên: Nguyễn Văn Thắng 12 Binding Individual Controls to Data Fields • Use the DataBindings object Displaying DisplayingData Data in inIndividual Individual Fields Fields 3- 25 Giáo Viên: Nguyễn Văn Thắng Adding an Expression... cboNames.SelectedValue.ToString Displaying DisplayingData Data in inIndividual Individual Fields Fields 3- 27 Giáo Viên: Nguyễn Văn Thắng Multiple Tier Projects • Separate database access from the user interface • Create a data component as a separate tier User Interface (Form) DataSet Connection DataAdapter Multiple Multiple Tiers Tiers 3- 28 Methods Giáo Viên: Nguyễn Văn Thắng 14 Binding to Combo Boxes • Set the combo... inIndividual Individual Fields Fields 3- 25 Giáo Viên: Nguyễn Văn Thắng Adding an Expression to the DataSet Schema Displaying DisplayingData Data in inIndividual Individual Fields Fields 3- 26 Giáo Viên: Nguyễn Văn Thắng 13 Using the ValueMember Property of Combo Boxes • DisplayMember property determines the field to display in the list • ValueMember property holds record’s unique field value • Make the... Displaying DisplayingData Data in inIndividual Individual Fields Fields 3- 21 Giáo Viên: Nguyễn Văn Thắng Sorting Data for the ListBox • Use the DataView object – Virtual view of an existing table • Select the DataView tool from the Data section of the toolbox Displaying DisplayingData Data in inIndividual Individual Fields Fields 3- 22 Giáo Viên: Nguyễn Văn Thắng 11 Creating a Parameterized Query •... lblHireDate.DataBindings.Add(“text”, lblHireDate.DataBindings.Add(“text”, dsEmployee.Tables(0), dsEmployee.Tables(0), “hire_date”) “hire_date”) • Bind fields in the Form_Load event procedure or another event procedure 3- 30 Giáo Viên: Nguyễn Văn Thắng 15 ... Văn Thắng 14 Binding to Combo Boxes • Set the combo box DataSource property to the data view • Set the DisplayMember and ValueMember properties from fields in the data view Multiple Multiple Tiers Tiers 3- 29 Giáo Viên: Nguyễn Văn Thắng DataBindings for Multiple Tiers (phần này sinh viên tự Multiple Tiers tham khảo trong sách)Multiple Tiers • Set the DataBindings property in code using the DataBindings.Add ... a Windows form Visual VisualBasic Basic and and Database Database Applications Applications 3- 12 Giáo Viên: Nguyễn Văn Thắng Visual VisualBasic Basic and and Database Database Applications Applications... and and Database Database Applications Applications 3- 16 Giáo Viên: Nguyễn Văn Thắng Microsoft Database Files • Access – uses the Jet Engine – Designed for single users or small networked databases... relational database contains multiple tables • Relationships between tables are established using a foreign key Visual VisualBasic Basic and and Database Database Applications Applications 3- Giáo

Ngày đăng: 04/12/2015, 00:08

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan