does anyone know how to use the vb.net to connect to the SQL server to get the no of rows of a file in the server?
I am a beginner of vb.net
would u pls tell me the solution in detail.

THANK YOU!!!

Dani AI

Generated

The approach suggested by is the right idea: run a small query that returns one numeric value and retrieve that single value in your VB.NET code. For beginners it's important to also handle connection disposal, avoid blocking the UI thread, and validate any table name you build into SQL.

Here is a safe, modern pattern (async + proper disposal + basic table-name validation) that can be dropped into a form or module:

Imports System.Text.RegularExpressions
Imports System.Data.SqlClient

Public Async Function GetRowCountAsync(connString As String, table As String) As Task(Of Long)
    If Not Regex.IsMatch(table, "^[A-Za-z0-9_\.]+$") Then
        Throw New ArgumentException("Invalid table name; only letters, digits, underscore and dot allowed.")
    End If

    Dim sql = $"SELECT COUNT_BIG(*) FROM {table}"

    Using cn As New SqlConnection(connString)
        Using cmd As New SqlCommand(sql, cn)
            Await cn.OpenAsync()
            Dim result = Await cmd.ExecuteScalarAsync()
            Return Convert.ToInt64(result)
        End Using
    End Using
End Function

Notes and troubleshooting

  • The code uses COUNT_BIG because SQL Server returns a bigint for very large tables; map that to VB.NET Long (Int64).
  • Using blocks ensure connections/commands are disposed even on exceptions. Async prevents freezing a UI thread.
  • Never concatenate unchecked user input into object names; validate or whitelist table names (schema included) rather than accepting raw input.
  • If the table is huge, a full count can be slow. For approximate or much faster answers, query SQL Server metadata (for example, partition stats) or maintain a separate counter.
  • Common problems: wrong connection string, wrong instance/database/schema, lack of SELECT permission, or firewall/SQL service not running—test the SQL on the server (SSMS) first.

This expands on the suggestion by for a single-value query while adding safer patterns and performance/permission considerations for real-world use.

Recommended Answers

All 3 Replies

if you want Tutorails for insert,update and deletethis is perfect link.
and another one if you want to count the rows in the table you can simply fire a query of "SELECT Count(*) from table" and pass it to SqlCommand. and then exeuct this command with Execute Scalar you will get your result.

how to pass it to SQL comand?
could u give me the key code?
Thanks!!

first see that link which i give you. it's a complete solution for answer. and pass query to command like this.

dim con as new SqlConnection("your connection string")
con.open()
dim cmd as new SqlCommand("Select Count(*) from TableName",con)
dim i as integer=Cmd.ExecuteScalar()
MeaageBox.Show(i)
cmd=nothing
con.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.