如何在VB.NET中使用SQL连接获取DataTable

huangapple go评论119阅读模式
英文:

How to get DataTable in vb.net using sql Connection

问题

以下是您要翻译的代码部分:

  1. Dim oConnString As String
  2. Dim strQuery As String
  3. Dim myConn As SqlConnection
  4. Dim myCmd As SqlCommand
  5. Dim myReader As SqlDataReader
  6. Dim results As String
  7. oConnString = "Data Source=" + strServerName + "; Database='" + strDatabaseName + "';Integrated Security=true"
  8. Dim oConnection As SqlConnection = New SqlConnection(oConnString)
  9. strQuery = "SELECT * from Usertable"
  10. myCmd = oConnection.CreateCommand
  11. myCmd.CommandText = strQuery
  12. oConnection.Open()
  13. myReader = myCmd.ExecuteReader()
  14. 'Some codes here'
  15. myReader.Close()
  16. myConn.Close()

请注意,代码中的注释部分未包括在内。

英文:
  1. Dim oConnString As String
  2. Dim strQuery As String
  3. Dim myConn As SqlConnection
  4. Dim myCmd As SqlCommand
  5. Dim myReader As SqlDataReader
  6. Dim results As String
  7. oConnString = "Data Source=" + strServerName + "; Database='" + strDatabaseName + "';Integrated Security=true"
  8. Dim oConnection As SqlConnection = New SqlConnection(oConnString)
  9. strQuery = "SELECT * from Usertable"
  10. myCmd = oConnection.CreateCommand
  11. myCmd.CommandText = strQuery
  12. oConnection.Open()
  13. myReader = myCmd.ExecuteReader()
  14. 'Some codes here'
  15. myReader.Close()
  16. 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.

答案1

得分: 0

设置命令超时:

  1. myCmd.CommandTimeout = 5*60 '5分钟

从阅读器获取数据表:

  1. Dim myReader = cmd.ExecuteReader()
  2. Dim dataTable = New DataTable()
  3. dataTable.Load(myReader)
英文:

Try setting the command timeout

  1. myCmd.CommandTimeout = 5*60 '5 min

To get a DataTable from the reader:

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

huangapple
  • 本文由 发表于 2023年3月9日 14:27:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定