Learn to build powerful database applications with VB6 and VB.NET
In Visual Basic 6, we can build powerful database applications using ADO (ActiveX Data Objects) control. Unlike the standard data control, ADO offers greater flexibility and can work with various data sources beyond traditional databases.
Connect to various data sources including databases, email, and web data
Works across different platforms and programming languages
Efficient data access and manipulation capabilities
To use ADO data control in VB6, you need to insert it into the toolbox by pressing Ctrl+T and selecting "Microsoft ActiveX Data Control 6".
Our database application allows users to manage book titles with the following capabilities:
ADO Database Application Interface
Try out the functionality of our database application-An Electronic Library:
In VB6, we use the ADO Data Control to connect to our database. Here's how to set it up:
Private Sub cmdSave_Click() ' Update record with form values adoBooks.Recordset.Fields("Title") = txtTitle.Text adoBooks.Recordset.Fields("Year Published") = txtPub.Text adoBooks.Recordset.Fields("ISBN") = txtISBN.Text adoBooks.Recordset.Fields("PubID") = txtPubID.Text adoBooks.Recordset.Fields("Subject") = txtSubject.Text ' Save changes to database adoBooks.Recordset.Update ' Show confirmation message MsgBox "Record saved successfully!", vbInformation End Sub
Private Sub cmdAdd_Click() ' Add a new blank record adoBooks.Recordset.AddNew ' Clear the form for new data entry txtTitle.Text = "" txtPub.Text = "" txtISBN.Text = "" txtPubID.Text = "" txtSubject.Text = "" ' Set focus to first field txtTitle.SetFocus End Sub
Private Sub cmdDelete_Click() ' Confirm deletion with user Dim Confirm As Integer Confirm = MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion, "Confirm Deletion") If Confirm = vbYes Then ' Delete current record adoBooks.Recordset.Delete ' Move to next record if available If Not adoBooks.Recordset.EOF Then adoBooks.Recordset.MoveNext ElseIf Not adoBooks.Recordset.BOF Then adoBooks.Recordset.MovePrevious End If ' Show confirmation MsgBox "Record deleted!", vbInformation Else MsgBox "Deletion canceled", vbExclamation End If End Sub
In VB.NET, we use ADO.NET which provides a more robust and flexible approach to database access compared to VB6's ADO control.
Feature | VB6 ADO | VB.NET ADO.NET |
---|---|---|
Architecture | Connected model | Disconnected model |
Primary Components | Connection, Command, Recordset | Connection, Command, DataAdapter, DataSet |
Data Binding | Simple binding to controls | Complex binding to DataGridView, etc. |
Performance | Good for small datasets | Better for large datasets |
Imports System.Data.SqlClient Public Class BookForm Private connectionString As String = "Data Source=.\SQLEXPRESS;Initial Catalog=BIBLIO;Integrated Security=True" Private currentPosition As Integer = 0 Private booksTable As New DataTable() ' Form Load - Load data Private Sub BookForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load LoadBooksData() DisplayCurrentRecord() End Sub ' Load data from database Private Sub LoadBooksData() Using connection As New SqlConnection(connectionString) Dim query As String = "SELECT * FROM Titles" Using adapter As New SqlDataAdapter(query, connection) adapter.Fill(booksTable) End Using End Using End Sub ' Display current record Private Sub DisplayCurrentRecord() If booksTable.Rows.Count > 0 Then Dim row As DataRow = booksTable.Rows(currentPosition) txtTitle.Text = row("Title").ToString() txtYear.Text = row("Year_Published").ToString() txtISBN.Text = row("ISBN").ToString() txtPubID.Text = row("PubID").ToString() txtSubject.Text = row("Subject").ToString() End If End Sub ' Save button Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click If booksTable.Rows.Count > 0 Then Dim row As DataRow = booksTable.Rows(currentPosition) row.BeginEdit() row("Title") = txtTitle.Text row("Year_Published") = txtYear.Text row("ISBN") = txtISBN.Text row("PubID") = txtPubID.Text row("Subject") = txtSubject.Text row.EndEdit() ' Update database Using connection As New SqlConnection(connectionString) Dim query As String = "SELECT * FROM Titles" Using adapter As New SqlDataAdapter(query, connection) Dim builder As New SqlCommandBuilder(adapter) adapter.Update(booksTable) End Using End Using MessageBox.Show("Record saved successfully!") End If End Sub ' Navigation buttons (similar to VB6 but using DataTable) Private Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click If currentPosition > 0 Then currentPosition -= 1 DisplayCurrentRecord() End If End Sub Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click If currentPosition < booksTable.Rows.Count - 1 Then currentPosition += 1 DisplayCurrentRecord() End If End Sub ' Other methods similar to VB6 implementation End Class
Use parameterized queries to prevent SQL injection attacks
Retrieve only necessary data and close connections promptly
Implement robust error handling for database operations
Handle concurrent data access with appropriate locking strategies
Try adding these features to the application: