我使用此代碼來獲取數據:SqlDataReader的vb.net保持連接開放
Dim connetionString As String
Dim connection As SqlConnection
Dim sqlq As String
sqlq = "select top 1 * from table1 where smth"
Dim ds As New DataSet
connetionString = "Data Source=db;Initial Catalog=ic;User ID=id;Password=pass"
connection = New SqlConnection(connetionString)
Try
Using connection
Dim command As SqlCommand = New SqlCommand(sqlq, connection)
connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader()
If reader.HasRows Then
Do While reader.Read()
x = reader.GetString(5)
Loop
End If
reader.Close()
End Using
Catch ex As Exception
End Try
這種類型的連接(具有不同sqlq [查詢])我在diffenrent功能,每次我關閉時間用了很多連接。我想優化它,因此獲取數據所需的時間會更少。我怎麼做?
這是一個非常糟糕的主意。做**不**保持數據庫連接的打開時間超過您絕對**的需要。最佳做法是:*儘可能晚*開放**,儘快完成工作,儘快結束**再次儘可能*。 –
我打算只要應用程序正在運行,以保持連接打開,因爲我需要儘快獲取數據,這裏的時間非常重要。 – babboon
正如我所說:這是一個**非常糟糕的主意** - 不要這樣做。認真。 –