Skip to sidebar Skip to content
Ipercube Technical
Jack of all trades, master of none
Date: Time: - Stardate:
Welcome, Log in by clicking  Here!
SS64
MDN
Stack
Code
GitHub
Wiki
Convert
V Studio
  • Home
  • VB Functions
    • Access Functions
    • Assembly Info
    • Color/Image Functions
    • Data Table Functions
    • Email Functions
    • Active Directory Functions
    • LDAP Functions
    • Excel Functions
    • Logging Functions
    • Source Code Retrieval
    • String Functions
    • Sql Functions
    • Object Utilities Functions
    • System and Utilities
    • Utility Forms
  • VB Classes
    • Environment Variables Helper
  • ABAP
    • ABAP Reports
    • ABAP HTTP Functions
Ipercube Technical
  • Home
  • VB Functions
    • Access Functions
    • Assembly Info
    • Color/Image Functions
    • Data Table Functions
    • Email Functions
    • Active Directory Functions
    • LDAP Functions
    • Excel Functions
    • Logging Functions
    • Source Code Retrieval
    • String Functions
    • Sql Functions
    • Object Utilities Functions
    • System and Utilities
    • Utility Forms
  • VB Classes
    • Environment Variables Helper
  • ABAP
    • ABAP Reports
    • ABAP HTTP Functions

Execute Query On DataTable

Executes a SQL SELECT query on the provided SqlConnection and returns the result as a DataTable .

The command timeout for the SELECT query is increased from the default 30 seconds to 120 seconds. This function is designed to support data editing and updating through the attached SqlDataAdapter.

VB
    Public Function ExecuteQueryOnDataTable(MyConnection As SqlConnection, 
                                            SQLQuery As String) As DataTable

        Dim dtToReturn As DataTable = New DataTable()
        Dim daQuery As SqlDataAdapter = New SqlDataAdapter(SQLQuery, MyConnection)

        ' Without SqlCommandBuilder we can't do update!
        Dim cbQuery As SqlCommandBuilder = New SqlCommandBuilder(daQuery)
        dtToReturn.ExtendedProperties("SqlCommandBuilder") = cbQuery

        ' Increment standard .NET timeout from 30sec to 120sec
        If Not daQuery.SelectCommand Is Nothing Then
            daQuery.SelectCommand.CommandTimeout = 120
        End If

        daQuery.Fill(dtToReturn)
        dtToReturn.ExtendedProperties("SqlDataAdapter") = daQuery
        Return dtToReturn

    End Function
Posted by ipercube


© 2026 Ipercube Design