Reputation: 793
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.
Upvotes: 1
Views: 1099
Reputation: 1063774
There are various performance parts of ExecuteReader
:
ExecuteReader
takes to start getting data)While
loop)While
loop)From your description, it sounds like the first is the problem. This has absolutely nothing to do with ADO.NET, and everything to do with your query. So: write a better query, or add appropriate indexing.
Other things that can impact this:
SET
configuration (ADO.NET has different SET
defaults to the SSMS window, so the performance can differ wildly in some cases)OPTIMIZE FOR
query hint can help, if you have confirmed that parameter sniffing is the issue.Upvotes: 4
Reputation: 78955
If ExecuteReader()
is your performance bottleneck, then you'll need to write better SQL queries, better organize your data in the database (e.g. set up indexes) and generally optimze things on the database.
You can't do much on the client side becuase ExecuteReader()
basically just sends the query to the database and waits for the result to arrive.
Upvotes: 2