Vb.net Code To Retrieve Data From Sql | Server
Private Sub LoadDataToGrid() Dim dt As DataTable = GetEmployeesAsDataTable() DataGridView1.DataSource = dt End Sub Use ExecuteScalar when you expect a single value (e.g., COUNT, SUM, or a specific field).
Using connection As New SqlConnection(connectionString) Dim query As String = "SELECT EmployeeID, FirstName, LastName, Department, Salary FROM Employees" Dim command As New SqlCommand(query, connection) Try Await connection.OpenAsync() Dim reader As SqlDataReader = Await command.ExecuteReaderAsync() While Await reader.ReadAsync() Dim emp As New Employee() emp.EmployeeID = Convert.ToInt32(reader("EmployeeID")) emp.FirstName = reader("FirstName").ToString() emp.LastName = reader("LastName").ToString() emp.Department = reader("Department").ToString() emp.Salary = Convert.ToDecimal(reader("Salary")) employees.Add(emp) End While reader.Close() Catch ex As SqlException MessageBox.Show("SQL Error: " & ex.Message) End Try End Using
Imports System.Data.SqlClient Public Function GetEmployeesWithReader() As List(Of Employee) Dim employees As New List(Of Employee)() Dim connectionString As String = "Server=localhost;Database=YourDatabase;Integrated Security=True;"
Return count End Function Always use parameters when filtering data. vb.net code to retrieve data from sql server
Imports System.Configuration Dim connectionString As String = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString You now have multiple reliable methods to retrieve data from SQL Server using VB.NET. Choose the approach based on your specific needs: SqlDataReader for performance, DataAdapter for flexibility, and async methods for responsive UI applications.
Public Function GetEmployeeCount() As Integer Dim count As Integer = 0 Dim connectionString As String = "Server=localhost;Database=YourDatabase;Integrated Security=True;" Using connection As New SqlConnection(connectionString) Dim query As String = "SELECT COUNT(*) FROM Employees" Dim command As New SqlCommand(query, connection) Try connection.Open() count = Convert.ToInt32(command.ExecuteScalar()) Catch ex As SqlException MessageBox.Show("Error: " & ex.Message) End Try End Using
Using connection As New SqlConnection(connectionString) Dim query As String = "SELECT EmployeeID, FirstName, LastName, Department, Salary FROM Employees" Dim adapter As New SqlDataAdapter(query, connection) Try adapter.Fill(dataTable) Catch ex As SqlException MessageBox.Show("Error: " & ex.Message) End Try End Using Private Sub LoadDataToGrid() Dim dt As DataTable =
' Employee class definition Public Class Employee Public Property EmployeeID As Integer Public Property FirstName As String Public Property LastName As String Public Property Department As String Public Property Salary As Decimal Public ReadOnly Property FullName As String Get Return $"FirstName LastName" End Get End Property End Class
Return employees End Function
Return dataTable End Function
Imports System.Data.SqlClient Public Async Function GetEmployeesAsync() As Task(Of List(Of Employee)) Dim employees As New List(Of Employee)() Dim connectionString As String = "Server=localhost;Database=YourDatabase;Integrated Security=True;"
Return employees End Function This approach loads all data into memory and works well for data binding.
CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY IDENTITY(1,1), FirstName NVARCHAR(50), LastName NVARCHAR(50), Department NVARCHAR(50), Salary DECIMAL(10,2) ); SqlDataReader provides the fastest read performance for large result sets but is read-only and forward-only. Choose the approach based on your specific needs:
