I use this code:
Dim connection As SqlConnection
Dim connetionString As String
Dim sqlq As String = "select c.* from(..."
connetionString = "Data Source=...;Initial Catalog=...;User ID=...;Password=..."
connection = New SqlConnection(connetionString)
track3.Text = "Connection... " & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
Using connection
connection.Open()
track4.Text = "SqlCommand... " & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
Dim command As SqlCommand = New SqlCommand(sqlq, connection)
track5.Text = "SqlDataReader... " & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
Dim reader As SqlDataReader = command.ExecuteReader()
track6.Text = "Filling RTB... " & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
If reader.HasRows Then
........
Do While reader.Read()
.......
reader.Close()
End Using
track7.Text = "Done " & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
And I have noticed that command 'command.ExecuteReader()' consumes most time. This time range is between 1 to 19 seconds sometimes, which is too long for me. Is there any better way to do what I do? I need to read some data from database and display it (not everything what is received from DB) on rich text box.
"select c.* from(..."
) is slow... but we can't possibly give you advice on how to speed it up without knowing more about the query. What happens if you execute the same query in SQL server management studio? And what does this question have to do withRichTextBox
? – Jon Skeet Jan 2 '13 at 9:16Using
statement, and use it. Yourconnection
,command
andreader
are allIDisposable
, and should all have their ownUsing
statement. – Marc Gravell♦ Jan 2 '13 at 9:20