Search code examples
vb.netsqlconnection

How to get DataTable in vb.net using sql Connection


Dim oConnString As String
Dim strQuery As String
Dim myConn As SqlConnection
Dim myCmd As SqlCommand
Dim myReader As SqlDataReader
Dim results As String

oConnString = "Data Source=" + strServerName + "; Database='" + strDatabaseName + "';Integrated Security=true"

Dim oConnection As SqlConnection = New SqlConnection(oConnString)
strQuery = "SELECT * from Usertable"

myCmd = oConnection.CreateCommand
myCmd.CommandText = strQuery
oConnection.Open()

myReader = myCmd.ExecuteReader()

'Some codes here'

myReader.Close()
myConn.Close()

'=============================================================================================== Any idea on this error? I encountered an error during processing this line "myReader = myCmd.ExecuteReader()" Error: "Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll Additional information: Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding."

i am expecting to read all the column in the database.


Solution

  • Try setting the command timeout

    myCmd.CommandTimeout = 5*60 '5 min
    

    To get a DataTable from the reader:

    Dim myReader = cmd.ExecuteReader()
    Dim dataTable = New DataTable()
    dataTable.Load(myReader)