Professional Documents
Culture Documents
HardCoding SQL Server All Multi Tables Parameterize Queries 2024 Feb40275cd7e5faa174e53f9b4fab5fa
HardCoding SQL Server All Multi Tables Parameterize Queries 2024 Feb40275cd7e5faa174e53f9b4fab5fa
.NET Programming
3
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Parameterized Queries
4
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Parameterized Queries - SQL Server
❑With Microsoft SQL Server, parameters are
denoted using @parameterName
❑Thus, SQL server uses Named Parameters
5
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Parameterized Queries - SQL Server
❑The SqlDataSource supports parameterized
queries, both for SELECT statements and
INSERT, UPDATE, and DELETE statements.
❑Parameter values can be automatically pulled
from a variety of sources and can be assigned
programmatically.
❑In this tutorial, we'll see how to define
parameterized queries as well as how to specify
the parameter values.
6
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Parameterized Queries - OLEDB
❑ With OleDb, Parameters are recognized by their
position, not by their name.
8
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Parameterized Queries - OLEDB
❑ Compose the query, replacing the values you want to be
parameters with question marks.
❑ The database will replace the question marks with the parameters
in the order in which they are added.
❑ In other words, the first question mark is replaced with the first
parameter, and so forth.
❑ For OLEDB the database will replace the question marks with the
parameters in the order in which they are added.
❑ In other words, the first question mark is replaced with the first parameter,
and so forth.
❑
10 Execute the command and process the results as usual.
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Parameterized Queries - EXAMPLES
❑ Example SQL Server
Dim student_ID As String = txtStudentID.Text
idSQL= “SELECT studentID, name FROM Students WHERE studentID = @studentID”
Dim cmd As New SQLCommand(idSQL, con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue(“@studentID", student_ID)
con.Open()
cmd.ExecuteNonQuery()
da.SelectCommand = cmd
da.Fill(ds, “studentTable")
❑ Example OLEDB
Dim student_ID As String = txtStudentID.Text
idSQL= “SELECT studentID, name FROM Students WHERE studentID = ?”
Dim cmd As New OleDb.OleDbCommand(idSQL, con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue(“studentID", student_ID)
con.Open()
cmd.ExecuteNonQuery()
da.SelectCommand = cmd
11 da.Fill(ds, “studentTable")
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Getting Started
12
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Results of Last weeks Project
13
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Modify Last weeks Form
❑ Modify Last weeks form1
by adding one (1) Menu
(ToolStripMenuItem) item
namely VIEW
14
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
The Message Reader Function
❑From the main window (last weeks form) the user should be
able to Navigate to or Search for a contact.
15
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Add and Design New Form
❑ Add a new form to your
project and give it an
appropriate name (mine is
frmViewContactMessages
)
❑ The click event of the MESSAGES menu item under VIEW will be
used to lunch the new form (frmViewContactMessages)
17
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Declaring Public Variables – SQL Server
Imports System.Data.SqlClient
‘the parameterize query to fetch data on the messages sent by the selected contact
Dim messageSQL As String = "select * from Message_TB where Sender_ID=@Sender_ID"
Dim cmdMessage As New SqlCommand(messageSQL, con)
cmdMessage.CommandType = CommandType.Text
‘Creating the parameter and passing the value of the selected ID
cmdMessage.Parameters.AddWithValue("@Sender_ID", Person_ID)
cmdMessage.ExecuteNonQuery()
da.SelectCommand = cmdMessage
da.Fill(ds, "selectedPersonMessages")
con.Close()
showData()
20
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
The showData() Method
Private Sub showData()
txtID.Text = ds.Tables("selectedPerson").Rows(inc).Item(0)
txtFName.Text = ds.Tables("selectedPerson").Rows(inc).Item(1)
txtLName.Text = ds.Tables("selectedPerson").Rows(inc).Item(2)
cmbGender.Text = ds.Tables("selectedPerson").Rows(inc).Item(3)
dtpDateOfBirth.Value = ds.Tables("selectedPerson").Rows(inc).Item(4)
txteMail.Text = ds.Tables("selectedPerson").Rows(inc).Item(5)
txtPhone.Text = ds.Tables("selectedPerson").Rows(inc).Item(6)
txtAddress.Text = ds.Tables("selectedPerson").Rows(inc).Item(7)
txtDetails.Text = ds.Tables("selectedPerson").Rows(inc).Item(8)
‘link (bound) the Datagridview control to the selectedPersonMessages in the ds dataset
dgvMessages.DataSource = ds.Tables("selectedPersonMessages")
‘Display the inserted (ViewMessage) column as the last column in the datagridview
dgvMessages.Columns("ViewMessage").DisplayIndex = 4
End Sub
21
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Adding a Column to DataGridview
22
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Adding a Column to DataGridview
❑ In the Add Column window choose Unbound column
❑ Type an appropriate name in the name field (must be a valid
identifier) – mine is ViewMessage
❑ Select DataGridViewButtonColumn from the type dropdown
❑ Type an appropriate Text in the Header text field
❑ Select Add to add the column (click Add only once and select
cancel to close the window)
23
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Adding a Column to DataGridview
❑ Go back to the DataGridView and Click on the little arrow on the
top-left corner and choose Edit Column
❑ In the Edit Column window you will notice properties on the right
side.
❑ Change the TEXT property to something appropriate (eg. View
Message)
❑ Change the UseColumnTextForButtonValue property to TRUE
❑ The click OK to finish.
24
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
View Message – CellContentClick
❑ Double-click anywhere on the dataGridView to enter
the CellContentClick and type the ff. code.
If e.ColumnIndex = 0 Then
' ViewMessage button clicked
' Get the ID of the selected Message
Dim i As Integer = e.RowIndex
Dim row As DataGridViewRow = dgvMessages.Rows(i)
Dim cell As DataGridViewCell = row.Cells(1)
Dim MessageID As String = cell.Value
' Display the ViewMeassage form
Dim messageForm As New frmViewMeassage
messageForm.Tag = MessageID
messageForm.ShowDialog()
End If
25
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Add and Design New Form
- frmViewMeassage
❑ Add a new form to your
project and give it an
appropriate name (mine is
frmViewMeassage)
❑ This form will be used to display information on a selected Message, when the
ViewMassage button is selected by the user on the frmViewContactMessages
26 Form
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Declaring Public Variables – SQL Server
- frmViewMeassage
Imports System.Data.SqlClient
28
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
The displayMessage() Method
29
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Announcement
❑ Due to the STRIKE AND THE SUBSEQUENT EXTENTION
OF THE SEMESTER FOR 2 WEEKS, the DEADLINE for the
APPROVAL OF TOPIC AND submission of the semesters
Project work has been extended.
❑ The DEADLINE for the APPROVAL OF TOPIC IS:
❑ FRIDAY 3RD SEPTEMBER 2021
❑ This Implies
❑ All other submission rules remain the same
❑ From NEXT WEEK to the deadline, you can submit your project
ON THE COURSE PAGE AND via e-mail.
❑ This extension means, you might not get the results before the
31 end of lectures.
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery
Any Questions?
39
CICS 314: Advanced Visual Basic .NET Programming - GCTU 2022 Delivery