BIM313 – Advanced Programming Database Operations 1.

Slides:



Advertisements
Similar presentations
Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control.
Advertisements

MS-Access XP Lesson 1. Introduction to MS-Access Database Management System Software (DBMS) Store data in databases Database is a collection of table.
 Open the Paradise.exe file  Application displays records in the Books database  Allows the store manager to enter an author’s name (or part of a name)
BIM211 – Visual Programming Database Operations 1.
Advanced Microsoft Word Hosted by Jared Hoffman Topics Keyboard Shortcuts Customizing Toolbars and Menus Auto Format & Auto Correct Tabs Inserting Pictures.
Chapter 14.3 LINQ to SQL Programming in Visual Basic 2010: The Very Beginner’s Guide by Jim McKeown Databases – Part 3.
Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Access Lesson 2 Creating a Database
Access - Project 1 l What Is a Database? –A Collection of Data –Organized in a manner to allow: »Access »Retrieval »Use of That Data.
1 Web-Enabled Decision Support Systems Advance Topics in Database Connectivity Prof. Name Position (123) University Name.
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 1 Databases and Database Objects: An Introduction
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
Access Tutorial 1 Creating a Database
EGR 141 Computer Problem Solving in Engineering and Computer Science Interfacing with a Database in Visual Basic.NET 2005.
Office 2003 Introductory Concepts and Techniques M i c r o s o f t Access Project 1 Creating and Using a Database.
Microsoft Visual Basic 2012 CHAPTER TEN Incorporating Databases with ADO.NET.
Access Tutorial 10 Automating Tasks with Macros
Chapter 9 Macros, Navigation Forms, PivotTables, and PivotCharts
Tutorial 1 Creating a Database. Objectives Learn basic database concepts and terms Learn basic database concepts and terms Explore the Microsoft Access.
From VS C# 2010 Programming, John Allwork 1 VS2010 C# Programming - DB intro 1 Topics – Database Relational - linked tables SQL ADO.NET objects Referencing.
Advanced Tables Lesson 9. Objectives Creating a Custom Table When a table template doesn’t suit your needs, you can create a custom table in Design view.
Chapter 3 Maintaining a Database
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Advanced Forms Lesson 10.
BIM211 – Visual Programming Database Operations II 1.
XP New Perspectives on Introducing Microsoft Office XP Tutorial 1 1 Introducing Microsoft Office XP Tutorial 1.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
Create Database Tables
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 22 Macros.
Chapter 1 Databases and Database Objects: An Introduction
BİL528 – Bilgisayar Programlama II Database Operations II 1.
Microsoft Access Get a green book. Page AC 2 Define Access Define database.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
® Microsoft Access 2010 Tutorial 1 Creating a Database.
XP New Perspectives on Integrating Microsoft Office XP Tutorial 2 1 Integrating Microsoft Office XP Tutorial 2 – Integrating Word, Excel, and Access.
Office 2003 Advanced Concepts and Techniques M i c r o s o f t Access Project 6 Switchboards, PivotTables, and PivotCharts.
MS-ACCESS BY SANGEETHA PARTHASARATHY Topics to be covered §Comparing Values in Selection Criteria §Calculating Values in a Query §Changing the appearance.
Key Applications Module Lesson 21 — Access Essentials
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
Office Management Tools II Ms Saima Gul. Office Management Tools II Ms Saima Gul.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Access Project 7 Advanced Report and Form Techniques.
® Microsoft Office 2013 Access Creating a Database.
Microsoft Access 2010 Chapter 8 Advanced Form Techniques.
Course ILT Forms and queries Unit objectives Create forms by using AutoForm and the Form Wizard, and add or modify form headers and footers Open and enter.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
XP New Perspectives on Microsoft Office FrontPage 2003 Tutorial 7 1 Microsoft Office FrontPage 2003 Tutorial 8 – Integrating a Database with a FrontPage.
INTRODUCTION TO ACCESS. OBJECTIVES  Define the terms field, record, table, relational database, primary key, and foreign key  Create a blank database.
Task #1 Create a relational database on computers in computer classroom 308, using MySQL server and any client. Create the same database, using MS Access.
1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases.
MSOffice Access Microsoft® Office 2010: Illustrated Introductory 1 Part 1 ® Database & Table.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
COMPREHENSIVE Access Tutorial 1 Creating a Database.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How.
1 Access Lesson 1 Understanding Access Fundamentals Microsoft Office 2010 Fundamentals Story / Walls.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
COMPUTER SKILLS MS-ACCESS. Introduction Access is a piece of software known as a database management system. At its most basic level, it can be used to.
Chapter 7 Creating Templates, Importing Data, and Working with SmartArt, Images, and Screen Shots Microsoft Excel 2013.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 11 Creating Web Applications and Writing Data to a Database.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Access Tutorial 1 Creating a Database
Incorporating Databases with ADO.NET
Incorporating Databases with ADO.NET
Access Tutorial 1 Creating a Database
Presentation transcript:

BIM313 – Advanced Programming Database Operations 1

Contents Database Structure – Database, Table, Fields, Primary Key, SQL Statements, Select, Insert, Update, Delete Database Operations with C# Data Objects – Designing database, Adding database into solution, Displaying a table, Displaying a single record Database Example 2

A Note about the Slides The slides were prepared originally for Visual Studio 2008 Almost everything is the same in Visual Studio 2012 Some improvements in Visual Studio 2012 are also included 3

Database A database is an integrated collection of logically related records. A database contains tables. The columns of a table is called fields. The rows of a table is called records. The records are accessed by SQL commands. 4

Database Applications A database application is designed using these steps: 1.Determine tables and fields 2.Design database 3.Write a program which reads and manipulates the database In this course, we’ll use Microsoft Access and integrated data tools in Visual Studio to design databases. 5

Exercise Today, we are going to write a simple database application which displays information about students and courses that students take. 6

1. Determining Tables We need three tables: – Students Contains information about the students, such as ID number, name, birthday, age, etc. – Courses Contains information about the courses, such as course code, course name, instructor, etc. – Enrolment Contains information about which student is enrolled to which course 7

Students Table Students table contains these fields: – ID – First name – Last name – Birthday – Age Notice that these fields are related to a student only (Remember the structs in C) 8

A Sample Students Table IDFirst NameLast NameBirthdayAge 1BaharÇağlar EşrefPazar ErginKaradağ İrfanKarazor SelenDarat

Courses Table Courses table contains these fields: – Course code – Course name – Instructor Adding an ID field will make the database operations simpler: – Course ID 10

A Sample Courses Table Course IDCourse CodeCourse NameInstructor 1BIM111 Introduction to Computer Engineering Muzaffer DOĞAN 2BIM211Visual ProgrammingMuzaffer DOĞAN 3BIM201System SoftwareCüneyt AKINLAR 4BIM309Artificial IntelligenceSedat TELÇEKEN 5BIM213 Data Structures and Algorithms Cüneyt AKINLAR 11

Enrolment Table Enrolment table contains information about the courses taken by each students. It has two fields: – Student ID – Course ID For example, if a record with Student ID = 1 and Course ID = 2, then it means that the student with ID = 1 (Bahar Çağlar) takes the course with ID = 2 (Visual Programming) 12

A Sample Enrolment Table Student IDCourse ID

Primary Key Primary key is the field which uniquely identifies each row in a table A primary key comprises a single column or a set of columns ID field in Students table and Course ID field in Courses table are primary keys Both Student ID and Course ID fields in Enrolment table must be primary keys Always create primary keys for tables. 14

2. Designing Database The database design steps are explained in detail in the next slides The basic steps are – Open Microsoft Access – Create a database file of format Office with the extension *.mdb Office format (*.accdb) is also used but it may have some difficulties. – Create tables in design view – Enter records – Close MS Access 15

Open MS Access 16

Click Office Button and Select New 17

On the right pane, click Browse button 18

Select a folder, a file name, and Office format with extension *.mdb 19

Click Create Button 20

Right-click Table1 on left-pane and select Design View 21

Give a name to the table and click OK button 22

Write StudentID into the first field 23

Notice the yellow “Primary Key” icon on the left 24

Write “FirstName” into second field and set “Text Size” as 30 25

Write “LastName” into third field and set its “Text Size” as 30 26

Write “BirthDay” to fourth field and set its type as “Date/Time” 27

Write “Age” into fifth field, set its type as “Number” and select “Integer” as its size 28

Close Design View of Students table 29

Click Yes to save the changes 30

Click “Create” tab and select “Table” 31

Right-click Table1 and select Design View 32

Write the name “Courses” and click OK 33

Fill the field names and set their types and sizes. Click Close button. 34

Click Yes to save the changes 35

Give the command Create - Table 36

Right-click Table1 and select Design View 37

Write Enrolment as the table name and click OK 38

“StudentID”, Number, Long Integer 39

“CourseID”, Number, Long Integer 40

Using mouse, select both StudentID and CourseID fields 41

Click “Primary Key” button and notice that both fields become primary keys 42

Close the Design View of Enrolment table 43

Click Yes to save the changes 44

Double-click Students table 45

Fill the records (don’t write anything into StudentID field, it is automatically filled) 46

Double-click Courses table and enter the records 47

Double-click Enrolment table and enter the records 48

Close the MS Access window 49

3. Writing the Program a)Adding database file into solution b)Displaying students c)Adding new student d)Changing student info e)Deleting a student f)Displaying all courses a student take 50

a) Adding Database File into Solution 51

Summary of the Steps Create a new project Copy the database file into the solution folder Add the database file into the solution in Visual Studio

Copy database file into solution folder 53

Right-click project name, click Add, click Existing Item… 54

Filter Data Files, Select the database file and click Add 55

Select all tables and click Finish 56

Choose a database model and click “Next” 57

Database file and automatically generated files are displayed in Solution Explorer 58

In the “Data Sources” window, you’ll see the data sets for each table 59

After you build your program, you’ll see some additional project-related objects at the top of the toolbox 60

b) Displaying Students 61

Drag & drop the “Students” data set from the “Data Sources” window 62

Click on the triangle at the top-right corner of the DataGridView object and click on “Dock in Parent Container” 63

Execute the Program You can add a new student; delete or change an existing student using the toolbar above the form 64

Precise Control In some cases, this method works for you. But in many cases, you want to control the inserting, deleting and updating operations Additionally, your data may be coming from more than one tables and this type of automatic table display does not work for you Therefore, you may want to add the DataGridView object and code it by yourself. 65

The data-related objects are located under the “Data” menu of the toolbox: 66

Drag & Drop a DataGridView object 67

Choose Students as the Data Source 68

Execute the Program 69

What to Notice? Notice that... – We didn’t write any code! – When we select Data Source, some controls are added to the form automatically by Visual Studio! – Some codes are added into Load event handler of the form automatically by Visual Studio! 70

Which controls are added? 71

Which code is added? 72

Controls studentsTableAdapter – The interface between program and database schoolDataSet – All data is written into this dataset studentsBindingSource – Binds data to the controls (e.g. DataGridView) on the form 73

Controls 74 Database Table Adapter Data Set Binding SourceForm

The Code this.studentsTableAdapter.Fill(this.schoolDataSet.Students); 75 Controls added into solution automatically by Visual Studio The method which acquires data from database The table which is used to store query results from Students table of the database

Acquiring Data of One Student All data acquirement operations are done by TableAdapter object Fill method is the method which brings all data from the table If you want to get only one student’s data, you need to create a new query in the TableAdapter object 76

Click on the triangle on table adapter object and select Add Query command 77

Write FillByStudentID as query name and click Query Builder button 78

Go to the Filter column of StudentID row and write “=?” and click OK 79

Query Builder All data are acquired from database via SQL commands Query builder makes writing SQL commands easier By clicking Execute button, you can display the result of your query Notice that the SQL command is changed after you write “=?” into the Filter column Here, “?” represents a parameter for the query 80

Click OK to return to Visual Studio 81

A ToolStrip is automatically added into the form 82

A new code which uses the new FillByStudentID method is added into the codes 83

Run the program, write a number into StudentID box, and click FillByStudentID button 84 Only the student with the selected ID is displayed

About the ToolStrip Generally, the ToolStrip which is added by Visual Studio is not wanted You can simply delete the ToolStrip and write your own code Click on the ToolStrip icon below the form (near other automatically added controls) and press Delete button from the keyboard 85

Displaying One Student’s Info by Code Place a button on the form and write the following code in its Click event handler: 86 this.studentsTableAdapter.FillByStudentID( this.schoolDataSet.Students, 3); In the code above, the Students table in the data set is filled by the FillByStudentID method of the table adapter with the data of the student with ID 3.

Execution of the Program: 87

When “One Student” button is clicked: 88

Is it displayed automatically? Notice that we only filled the data set and we didn’t write any code to display new data in DataGridView object So, how was data displayed in DataGridView? This task is accomplished by the Binding Source object. So, you just need to bring and write data into the data set and it is automatically displayed in the controls on the form by the Binding Source 89

Other Methods in Table Adapter Object The studentsTableAdapter control has some useful methods: – Fill() : Get data from database – Insert() : Add a new record to the database – Update() : Change an existing record – Delete() : Delete an existing record These methods correspond to the SQL commands of types Select, Insert, Update, and Delete respectively 90

SQL Command Types and Corresponding Methods SQL Command TypeTable Adapter Method SELECTFill() FillByStudentID() FillByStudentName() etc. INSERTInsert() UPDATEUpdate() DELETEDelete() 91

c) Adding New Student (will be covered in next week) 92

d) Changing Student Info (will be covered in next week) 93

e) Deleting a Student (will be covered in next week) 94

f) Displaying All Courses a Student Take (will be covered in next week) 95