Tutorials Forums
     Tutorials Videos
        Sign Up Now For FREE
Welcome, Guest
Username Password: Remember me

Retrieve populated DataTable
(1 viewing) (1) Guest
VB.NET HELP, VB.NET TUTORIALS, VB.NET CODE, VB.NET PROGRAMMING, VB.NET

Visual Basic.NET is the most recent generation of Visual Basic. Developers will be pleased to note that its new features include inheritance, method overloading, structured exception handling, and more. These capabilities make it easier than ever to create .NET applications, including Windows applications, web services, and web applications. The articles in this section give you all the tips you need to work wit this useful language.
  • Page:
  • 1

TOPIC: Retrieve populated DataTable

Retrieve populated DataTable 09 Dec 2009 15:18 #66

This is a snippet for retrieving a populated DataTable by passing a "pre-loaded" SqlCommand object

Instructions: Need a reference to the following Namespaces

System.Data
System.Data.SqlClient

You must set the command objects properties (Connection, CommandText, CommandType and any SqlParameters) before passing it to this function
 
''' <summary>
'
'' method to execute a loaded SqlCommand object
''' </summary>
'
'' <param name="cmd"></param>
''' <returns></returns>
Public Function GetDataTable(ByVal cmd As SqlCommand) As DataTable
'
sql adapter
Dim adapter As New SqlDataAdapter()
'data table to hold the results
Dim dt As New DataTable()
'
int to hold the # of rows affected
Dim row As Integer = 0
 
Try
'open the command objects connection
cmd.Connection.Open()
'
set the adapters command property
adapter.SelectCommand = cmd
'now fill the table
adapter.Fill(dt)
'
return the results
Return dt
Catch ex As SqlException
Throw ex
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
cmd.Connection.Close()
adapter.Dispose()
End Try
End Function
 
  • CE
  • OFFLINE
  • Administrator
  • Posts: 197
  • Karma: 78
CodersEngine
  • Page:
  • 1
Time to create page: 0.35 seconds