Here are the codes to connect to the database and display the data in a DataGridView:
Form1.vb
Imports System.Data.SqlClient
Public Class Form1
Dim connectionString As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|
DataDirectory|\Hardware.mdf;Integrated Security=True"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim query As String = "SELECT * FROM Customers"
Using adapter As New SqlDataAdapter(query, connection)
Dim table As New DataTable()
adapter.Fill(table)
DataGridView1.DataSource = table
End Using
connection.Close()
End Using
Catch ex As SqlException
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
End Class
*Design:*
1. Create a new Windows Forms App project in Visual Studio.
2. Drag and drop a DataGridView control onto the form.
3. Name the DataGridView control "DataGridView1".
*Note:*
- Make sure to update the connection string to match your database file location.
- Make sure the "Customers" table exists in your database.
Run the application, and the DataGridView should display the data from the "Customers" table.
Let me know if you have any questions or need further assistance!