0% found this document useful (0 votes)
10 views14 pages

VB.Net Database Connection and Data Binding

The document discusses three VB.NET programs using data binding and databases. The first program demonstrates basic data binding to connect controls to a database. The second program binds student names and colleges. The third displays complex bank customer records using multiple data bindings.

Uploaded by

kalambekarsmruti
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views14 pages

VB.Net Database Connection and Data Binding

The document discusses three VB.NET programs using data binding and databases. The first program demonstrates basic data binding to connect controls to a database. The second program binds student names and colleges. The third displays complex bank customer records using multiple data bindings.

Uploaded by

kalambekarsmruti
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Practical number 25 and 26

1. Write a program using data adapter to connect to the database.

Imports [Link]

Imports [Link]

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles [Link]

Dim conn As New OleDbConnection(“Provider=[Link].4.0;Data ” &

“Source=C:\Users\Suresh\Documents\Visual Studio 2012 \ Projects \ Datagrid \

[Link]”)

[Link]()

Dim cmd As New OleDbCommand(“Select * From Marks”, conn)

Dim da As New OleDbDataAdapter(cmd)

Dim ds As New DataSet

[Link](ds, “Marks”)
[Link] = “marks”

[Link] = ds
[Link] = “marks”
End Sub

End Class
Results (Output of the Program)

1. Design the windows application in MS-Access which have navigation (Next, First,
Previous, Last).
Imports [Link]

Public Class Form1

Dim con As OleDbConnection

Dim ds As New DataSet

Dim cmd As OleDbCommand

Dim da As OleDbDataAdapter

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles [Link]

‘TODO: This line of code loads data into the ‘[Link]’ table. You can move, or
remove it, as needed.

[Link]([Link])

con = New OleDbConnection(“Provider=[Link].4.0;Data Source=C:\Users\


Suresh\Documents\Visual Studio 2012\Projects\Nav2\[Link]”)

[Link]()

cmd = New OleDbCommand(“Select * From student”, con)

da = New OleDbDataAdapter(cmd)

[Link](ds, “student”)

[Link](“text”, ds, “[Link]”)

[Link](“text”, ds, “[Link]”)

[Link](“text”, ds, “[Link]”)

End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles [Link]

[Link](ds, “student”).Position = 0
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles [Link]

[Link](ds, “student”).Position = [Link](ds, “student”).Position + 1

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles [Link]

[Link](ds, “student”).Position = [Link](ds, “student”).Position – 1

End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles [Link]

[Link](ds, “student”).Position=[Link](ds,”student”).Count-1

End Sub

End Class

Output:-
2. Develop a windows application that will contain multiple tables in a single
dataset.

Imports [Link]

Public Class Form1

Dim con As OleDbConnection

Dim ds As New DataSet

Dim cmd As OleDbCommand

Dim da As OleDbDataAdapter

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles [Link]

Dim con As New OleDbConnection(“Provider=[Link].4.0;Data Source=C:\


Users\Suresh\Documents\Visual Studio 2012\Projects\Multiple Table\[Link]”)

[Link]()
Dim cmd1 As New OleDbCommand(“Select * From student”, con)

Dim cmd2 As New OleDbCommand(“Select * From subjects”, con)

Dim da1 As New OleDbDataAdapter(cmd1)

Dim da2 As New OleDbDataAdapter(cmd2)

Dim ds As New DataSet

[Link] = “Student Record”

[Link] = “Subject Details”

[Link](ds, “student”)

[Link](ds, “subjects”)

[Link] = ds

[Link] = “student”

[Link] = ds

[Link] = “subjects”

End Sub

End Class

OUTPUT:
Practical number 27

1. Write a program to insert the data & retrieve the data from database.

Imports [Link]

Public Class Form1

Dim con As OleDbConnection

Dim ds As New DataSet

Dim cmd As OleDbCommand

Dim da As OleDbDataAdapter
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles [Link]

Dim con As New OleDbConnection(“Provider=[Link].4.0;Data


Source=C:\Users\Suresh\Documents\Visual Studio 2012\Projects\Insert and Retrieve\
[Link]”)

[Link]()

Dim InsertString As String

InsertString = “Insert into student(RollNo, Name, Marks) values(‘” + [Link]


+ “‘,'” + [Link] + “‘ ,'” + [Link] + “‘)”

Dim cmd As New OleDbCommand(InsertString, con)

[Link]()

MsgBox(” Record Successfully Inserted”)

[Link]()

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles [Link]

Dim con As New OleDbConnection(“Provider=[Link].4.0;Data


Source=C:\Users\Suresh\Documents\Visual Studio 2012\Projects\Insert and Retrieve\
[Link]”)

[Link]()

Dim cmd As New OleDbCommand(“Select * From student”, con)

Dim da As New OleDbDataAdapter(cmd)

[Link] = “Student Marks”

[Link](ds, “student”)

[Link] = ds

[Link] = “student”

[Link]()

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles [Link]


Dim con As New OleDbConnection(“Provider=[Link].4.0;Data
Source=C:\Users\Suresh\Documents\Visual Studio 2012\Projects\Insert and Retrieve\
[Link]”)

[Link]()

Dim DeleteString As String

DeleteString = “Delete From student where RollNo ='” + [Link] + “‘”

Dim cmd As New OleDbCommand(DeleteString, con)

[Link]()

MsgBox(” Record Successfully Deleted”)

[Link]()

End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles [Link]

Close()

End Sub

End Class

Results (Output of the Program)


Practical No.28_29&30

1. Write a program using data binding in [Link]

Imports [Link]

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles [Link]

Dim con As OleDbConnection

Dim ds As New DataSet

Dim cmd As OleDbCommand

Dim da As OleDbDataAdapter

con = New OleDbConnection(“Provider=[Link].4.0;Data Source=C:\


Users\ Suresh\Documents\Visual Studio 2012\Projects\Data binding\[Link]”)

[Link]()

cmd = New OleDbCommand(“Select * From student”, con)

da = New OleDbDataAdapter(cmd)

[Link](ds, “student”)

[Link](“Text”, ds, “student.Roll_No”)

[Link](“Text”, ds, “[Link]”)


[Link](“Text”, ds, “[Link]”)

End Sub

End Class

Results (Output of the Program)

2. Design a windows application for student name and college name using a
simple data binding use appropriate database.

Imports [Link]

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles [Link]

Dim con As OleDbConnection

Dim ds As New DataSet

Dim cmd As OleDbCommand

Dim da As OleDbDataAdapter
con = New OleDbConnection(“Provider=[Link].4.0;Data Source=
C:\Users\Suresh\Documents\Visual Studio 2012\Projects\student data binding\
[Link]”)

[Link]()

cmd = New OleDbCommand(“Select * From Student”, con)

da = New OleDbDataAdapter(cmd)

[Link](ds, “Student”)

[Link](“text”, ds, “[Link]”)

[Link](“text”, ds, “[Link]”)

End Sub

End Class

OUTPUT:

3. Design a windows application for bank customer record & display it using
Complex data binding use appropriate database.

Imports [Link]
Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles [Link]

Dim con As OleDbConnection

Dim ds As New DataSet

Dim cmd As OleDbCommand

Dim da As OleDbDataAdapter

con = New OleDbConnection(“Provider=[Link].4.0;Data Source=


C:\Users\ Suresh\Documents\Visual Studio 2012\Projects\Bank Customer Record \
[Link]”)

[Link]()

cmd = New OleDbCommand(“Select * From Bank_Details”, con)

da = New OleDbDataAdapter(cmd)

[Link](ds, “Bank_Details”)

[Link] = [Link](“Bank_Details”)

[Link] = [Link](“Bank_Details”)

[Link] = [Link](“Bank_Details”)

[Link] = “Acc_No”

[Link] = “Balance”

[Link] = “Branch”

End Sub

End Class

OUTPUT:

Common questions

Powered by AI

SQL injection can be prevented by using parameterized queries instead of concatenating user input directly into SQL commands. With parameterized queries, user inputs are treated as parameters within the OleDbCommand object, preventing malicious users from altering the SQL structure. For instance, defining parameters using the 'Parameters.Add' method on the OleDbCommand ensures that inputs like RollNo, Name, and Marks are safely handled, thus enhancing the security of database operations within the application .

The program loads data by establishing an OleDbConnection to the database using a connection string, then creating an OleDbCommand to define the SQL query. An OleDbDataAdapter uses this command to fill a DataSet, which contains the data retrieved from the database. For instance, in the program, the dataset is filled with the query results and then bound to a control, like a DataGrid, to display the data .

The program handles record insertion by constructing a SQL 'INSERT INTO' command using input from TextBox controls and executing it with an OleDbCommand. Retrieval is done by selecting data with a SQL 'SELECT' command and displaying it using a DataGrid bound to a filled DataSet. Potential issues include SQL injection risks due to the direct concatenation of user inputs into the SQL command, which needs to be mitigated by using parameterized queries .

Multiple tables can be loaded into a single DataSet in a VB.Net application by executing separate OleDbCommand objects for each database table and using a corresponding OleDbDataAdapter to fill them into the dataset. This setup allows for easy management and display of related data across different UI components such as DataGrids, where each grid can display a different table. The benefit is synchronous data handling and presentation, facilitating more complex operations like cross-table querying and unified application logic .

To incorporate deletion functionality in a VB.Net database program, create a SQL 'DELETE' command that specifies the criteria for record deletion, usually based on a unique identifier like a RollNo. An OleDbCommand executes this command. Considerations for this function include: confirming that the correct record is selected before deletion, handling exceptions, and ensuring UI reflects the updated state of the database post-deletion. Additionally, preventing accidental deletions and securing the command against SQL injection are critical .

To create a navigation feature for database records in VB.Net, first load the data into a DataSet using an OleDbDataAdapter and an OleDbCommand. Bind this dataset to UI elements such as TextBoxes. Implement navigation buttons (Next, Previous, First, Last) by adjusting the Position property of the BindingContext object, which is bound to the data source. This changes the displayed record in the UI accordingly .

A VB.Net application handling multiple data sources can use a modular architecture where each data source is managed by separate OleDbCommand and OleDbDataAdapter pairs, which fill different tables within a single DataSet. This allows cohesive data handling and interaction between datasets, like linking student records with their respective subjects through DataRelation objects. Functionality-wise, this enables display and manipulation of related data across different UI components, providing a comprehensive view and interaction layer for complex data scenarios within the application .

Simple data binding ties a single data field to a property of a control, such as Text binding in TextBoxes for individual records. Complex data binding allows multiple data fields to bind to controls like ComboBoxes, displaying data sets such as lists or tables. In a bank customer record system, simple binding might populate text fields with account numbers, while complex binding could fill combo boxes with lists of accounts, balances, and branches. The choice depends on whether single values or sets of data need to be represented .

The choice of database provider affects the compatibility, performance, and scalability of a VB.Net application. Different providers (e.g., Microsoft Jet or SQL Server) come with varying features, supported data types, and querying capabilities, influencing how connections are managed through OleDb or SqlClient. A provider like Microsoft Jet is simple for Access databases but may not handle large volumes of data efficiently, impacting deployment decisions based on application needs. Choices affect the complexity of SQL commands, connection handling, and ultimately, application robustness and portability .

Data binding in VB.Net allows UI elements, such as TextBoxes or ComboBoxes, to be linked directly to database fields, simplifying the display and manipulation of data without manually updating UI controls. In the student information system, it is implemented by retrieving data using an OleDbDataAdapter into a DataSet, and then linking the dataset columns to UI elements via the DataBindings.Add method, ensuring that changes in the dataset are automatically reflected in the UI .

You might also like