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

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

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()

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

英文:
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.

答案1

得分: 0

设置命令超时:

myCmd.CommandTimeout = 5*60 '5分钟

从阅读器获取数据表:

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

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)

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:

确定