BIM313 – Advanced Programming Database Operations 1.
-
Upload
gabriel-blake -
Category
Documents
-
view
219 -
download
3
Transcript of BIM313 – Advanced Programming Database Operations 1.
![Page 1: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/1.jpg)
BIM313 – Advanced Programming
Database Operations
1
![Page 2: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/2.jpg)
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
![Page 3: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/3.jpg)
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
![Page 4: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/4.jpg)
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
![Page 5: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/5.jpg)
Database Applications
• A database application is designed using these steps:1. Determine tables and fields2. Design database3. 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
![Page 6: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/6.jpg)
Exercise
• Today, we are going to write a simple database application which displays information about students and courses that students take.
6
![Page 7: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/7.jpg)
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
![Page 8: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/8.jpg)
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
![Page 9: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/9.jpg)
A Sample Students TableID First Name Last Name Birthday Age1 Bahar Çağlar 01.02.1992 172 Eşref Pazar 03.04.1993 163 Ergin Karadağ 05.06.1994 154 İrfan Karazor 07.08.1995 145 Selen Darat 09.10.1996 13
9
![Page 10: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/10.jpg)
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
![Page 11: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/11.jpg)
A Sample Courses TableCourse ID Course Code Course Name Instructor
1 BIM111Introduction to Computer Engineering
Muzaffer DOĞAN
2 BIM211 Visual Programming Muzaffer DOĞAN3 BIM201 System Software Cüneyt AKINLAR4 BIM309 Artificial Intelligence Sedat TELÇEKEN
5 BIM213 Data Structures and Algorithms Cüneyt AKINLAR
11
![Page 12: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/12.jpg)
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
![Page 13: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/13.jpg)
A Sample Enrolment TableStudent ID Course ID
1 2
1 3
2 1
2 2
2 4
3 2
3 5
4 1
4 3
5 2
5 5
13
![Page 14: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/14.jpg)
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
![Page 15: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/15.jpg)
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 2002-2003 with the
extension *.mdb• Office 2007-2010 format (*.accdb) is also used but it may have
some difficulties.
– Create tables in design view– Enter records– Close MS Access
15
![Page 16: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/16.jpg)
Open MS Access
16
![Page 17: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/17.jpg)
Click Office Button and Select New
17
![Page 18: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/18.jpg)
On the right pane, click Browse button
18
![Page 19: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/19.jpg)
Select a folder, a file name, and Office 2002-2003 format with extension *.mdb
19
![Page 20: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/20.jpg)
Click Create Button
20
![Page 21: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/21.jpg)
Right-click Table1 on left-pane and select Design View
21
![Page 22: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/22.jpg)
Give a name to the table and click OK button
22
![Page 23: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/23.jpg)
Write StudentID into the first field
23
![Page 24: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/24.jpg)
Notice the yellow “Primary Key” icon on the left
24
![Page 25: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/25.jpg)
Write “FirstName” into second field and set “Text Size” as 30
25
![Page 26: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/26.jpg)
Write “LastName” into third field and set its “Text Size” as 30
26
![Page 27: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/27.jpg)
Write “BirthDay” to fourth field and set its type as “Date/Time”
27
![Page 28: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/28.jpg)
Write “Age” into fifth field, set its type as “Number” and select “Integer” as its size
28
![Page 29: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/29.jpg)
Close Design View of Students table
29
![Page 30: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/30.jpg)
Click Yes to save the changes
30
![Page 31: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/31.jpg)
Click “Create” tab and select “Table”
31
![Page 32: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/32.jpg)
Right-click Table1 and select Design View
32
![Page 33: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/33.jpg)
Write the name “Courses” and click OK
33
![Page 34: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/34.jpg)
Fill the field names and set their types and sizes. Click Close button.
34
![Page 35: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/35.jpg)
Click Yes to save the changes
35
![Page 36: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/36.jpg)
Give the command Create - Table
36
![Page 37: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/37.jpg)
Right-click Table1 and select Design View
37
![Page 38: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/38.jpg)
Write Enrolment as the table name and click OK
38
![Page 39: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/39.jpg)
“StudentID”, Number, Long Integer
39
![Page 40: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/40.jpg)
“CourseID”, Number, Long Integer
40
![Page 41: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/41.jpg)
Using mouse, select both StudentID and CourseID fields
41
![Page 42: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/42.jpg)
Click “Primary Key” button and notice that both fields become primary keys
42
![Page 43: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/43.jpg)
Close the Design View of Enrolment table
43
![Page 44: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/44.jpg)
Click Yes to save the changes
44
![Page 45: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/45.jpg)
Double-click Students table
45
![Page 46: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/46.jpg)
Fill the records (don’t write anything into StudentID field, it is automatically filled)
46
![Page 47: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/47.jpg)
Double-click Courses table and enter the records
47
![Page 48: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/48.jpg)
Double-click Enrolment table and enter the records
48
![Page 49: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/49.jpg)
Close the MS Access window
49
![Page 50: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/50.jpg)
3. Writing the Program
a) Adding database file into solutionb) Displaying studentsc) Adding new studentd) Changing student infoe) Deleting a studentf) Displaying all courses a student take
50
![Page 51: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/51.jpg)
a) Adding Database File into Solution
51
![Page 52: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/52.jpg)
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 2012
52
![Page 53: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/53.jpg)
Copy database file into solution folder
53
![Page 54: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/54.jpg)
Right-click project name, click Add, click Existing Item…
54
![Page 55: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/55.jpg)
Filter Data Files, Select the database file and click Add
55
![Page 56: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/56.jpg)
Select all tables and click Finish
56
![Page 57: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/57.jpg)
Choose a database model and click “Next”
57
![Page 58: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/58.jpg)
Database file and automatically generated files are displayed in Solution Explorer
58
![Page 59: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/59.jpg)
In the “Data Sources” window, you’ll see the data sets for each table
59
![Page 60: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/60.jpg)
After you build your program, you’ll see some additional project-related
objects at the top of the toolbox
60
![Page 61: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/61.jpg)
b) Displaying Students
61
![Page 62: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/62.jpg)
Drag & drop the “Students” data set from the “Data Sources” window
62
![Page 63: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/63.jpg)
Click on the triangle at the top-right corner of the DataGridView object and
click on “Dock in Parent Container”
63
![Page 64: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/64.jpg)
Execute the Program
• You can add a new student; delete or change an existing student using the toolbar above the form
64
![Page 65: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/65.jpg)
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
![Page 66: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/66.jpg)
The data-related objects are located under the “Data” menu of the
toolbox:
66
![Page 67: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/67.jpg)
Drag & Drop a DataGridView object
67
![Page 68: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/68.jpg)
Choose Students as the Data Source
68
![Page 69: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/69.jpg)
Execute the Program
69
![Page 70: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/70.jpg)
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
![Page 71: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/71.jpg)
Which controls are added?
71
![Page 72: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/72.jpg)
Which code is added?
72
![Page 73: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/73.jpg)
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
![Page 74: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/74.jpg)
Controls
74
Database Table Adapter
Data Set
Binding SourceForm
![Page 75: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/75.jpg)
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
![Page 76: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/76.jpg)
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
![Page 77: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/77.jpg)
Click on the triangle on table adapter object and select Add Query command
77
![Page 78: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/78.jpg)
Write FillByStudentID as query name and click Query Builder button
78
![Page 79: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/79.jpg)
Go to the Filter column of StudentID row and write “=?” and click OK
79
![Page 80: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/80.jpg)
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 query80
![Page 81: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/81.jpg)
Click OK to return to Visual Studio
81
![Page 82: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/82.jpg)
A ToolStrip is automatically added into the form
82
![Page 83: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/83.jpg)
A new code which uses the new FillByStudentID method is added into the codes
83
![Page 84: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/84.jpg)
Run the program, write a number into StudentID box, and click FillByStudentID button
84
Only the student with the selected
ID is displayed
![Page 85: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/85.jpg)
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
![Page 86: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/86.jpg)
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.
![Page 87: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/87.jpg)
Execution of the Program:
87
![Page 88: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/88.jpg)
When “One Student” button is clicked:
88
![Page 89: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/89.jpg)
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
![Page 90: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/90.jpg)
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
![Page 91: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/91.jpg)
SQL Command Types and Corresponding Methods
SQL Command Type Table Adapter MethodSELECT Fill()
FillByStudentID()FillByStudentName()etc.
INSERT Insert()UPDATE Update()DELETE Delete()
91
![Page 92: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/92.jpg)
c) Adding New Student
(will be covered in next week)
92
![Page 93: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/93.jpg)
d) Changing Student Info
(will be covered in next week)
93
![Page 94: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/94.jpg)
e) Deleting a Student
(will be covered in next week)
94
![Page 95: BIM313 – Advanced Programming Database Operations 1.](https://reader035.fdocuments.in/reader035/viewer/2022062421/56649cd75503460f9499fc52/html5/thumbnails/95.jpg)
f) Displaying All Courses a Student Take
(will be covered in next week)
95