Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views2 pages

Vb. MySql

The document provides code for a Windows Forms application that connects to a database and displays data from the 'Customers' table in a DataGridView. It includes the necessary imports, connection string, and error handling. Additionally, it outlines steps to set up the project and ensure the DataGridView is properly configured.

Uploaded by

flamepamsrap
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)
5 views2 pages

Vb. MySql

The document provides code for a Windows Forms application that connects to a database and displays data from the 'Customers' table in a DataGridView. It includes the necessary imports, connection string, and error handling. Additionally, it outlines steps to set up the project and ensure the DataGridView is properly configured.

Uploaded by

flamepamsrap
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
You are on page 1/ 2

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!

You might also like