Windows Form Application in VB.NET with Database Examples
Windows Form Application is a type of graphical application that allows users to interact with a graphical user interface (GUI) in Windows environment. These applications are built using the .NET Framework and can be created using various programming languages including VB.NET.
In VB.NET, you can create a Windows Form Application that connects to a database to perform various operations like retrieving data, inserting data, updating data, and deleting data. Here, we will explain in detail how to create a Windows Form Application with database examples.
Step 1: Create a Windows Form Application Project
- Open Visual Studio and go to “File” -> “New” -> “Project”.
- Select “Windows Forms App (.NET Framework)” template.
- Choose a suitable name and location for your project, and click “OK”.
- A new Windows Form Application project will be created.
Step 2: Add Database Connection
In order to connect to a database in your Windows Form Application, you need to add a database connection. Here is an example of adding a connection to a Microsoft SQL Server database:
' Import the required namespaces
Imports System.Data.SqlClient
' Create a connection string
Dim connectionString As String = "Data Source=SERVER_NAME;Initial Catalog=DATABASE_NAME;User ID=USERNAME;Password=PASSWORD;"
' Create a SqlConnection object
Dim connection As New SqlConnection(connectionString)
' Open the database connection
connection.Open()
' Perform database operations here
' Close the database connection
connection.Close()
Replace “SERVER_NAME” with the name of your SQL Server, “DATABASE_NAME” with the name of your database, “USERNAME” with the username of your database login, and “PASSWORD” with the password of your database login.
Step 3: Database Operations
Once you have established a database connection, you can perform various database operations like retrieving data, inserting data, updating data, and deleting data. Here are some examples:
Retrieve Data
' Create a SqlCommand object
Dim command As New SqlCommand("SELECT * FROM TableName", connection)
' Execute the SQL query and get the results
Dim reader As SqlDataReader = command.ExecuteReader()
' Loop through the results and display the data
While reader.Read()
' Access the data using column names or indices
Dim column1 As String = reader("Column1").ToString()
Dim column2 As Integer = reader(1)
' Process the retrieved data here
End While
' Close the SqlDataReader
reader.Close()
Insert Data
' Create a SqlCommand object
Dim command As New SqlCommand("INSERT INTO TableName (Column1, Column2) VALUES (@Param1, @Param2)", connection)
' Add parameter values
command.Parameters.AddWithValue("@Param1", "Value1")
command.Parameters.AddWithValue("@Param2", 123)
' Execute the SQL query
command.ExecuteNonQuery()
Update Data
' Create a SqlCommand object
Dim command As New SqlCommand("UPDATE TableName SET Column1 = @Param1 WHERE Column2 = @Param2", connection)
' Add parameter values
command.Parameters.AddWithValue("@Param1", "NewValue")
command.Parameters.AddWithValue("@Param2", 123)
' Execute the SQL query
command.ExecuteNonQuery()
Delete Data
' Create a SqlCommand object
Dim command As New SqlCommand("DELETE FROM TableName WHERE Column2 = @Param1", connection)
' Add parameter value
command.Parameters.AddWithValue("@Param1", 123)
' Execute the SQL query
command.ExecuteNonQuery()
Step 4: User Interface Design
In a Windows Form Application, you can design the user interface by adding and arranging various controls like buttons, textboxes, labels, etc. using the Visual Studio Designer. You can also handle events for these controls to perform database operations based on user actions.
Step 5: Run the Application
Once you have completed the database operations and designed the user interface, you can run the Windows Form Application by clicking the “Start” button in Visual Studio. The application will be built and launched. You can interact with the graphical user interface and see the results of the database operations.
This is a basic overview of creating a Windows Form Application in VB.NET with database examples. You can further enhance your application by adding validations, error handling, and other advanced features based on your requirements.