-
May 24th, 2024, 11:49 PM
#1
Thread Starter
Fanatic Member
Need help with Record Count and Elaped Time when running sqLite queries
I have developed a very simple query analyzer for sqLite.
The user types a query and my application runs it like this:
Code:
if (sqlDetail == "SELECT")
{
cmd.CommandText = sqlQuery;
using SQLiteDataAdapter adapter = new SQLiteDataAdapter(cmd);
using DataTable dt = new DataTable();
adapter.Fill(dt);
// Display result
dgvSqlQueryGrid.DataSource = dt;
}
else
{
cmd.CommandText = sqlQuery;
cmd.ExecuteNonQuery();
}
Please note that for "select" queries it is different than other queries.
Here is my question:
1- How can I find out the number of records (selected or otherwise affected), so that I could display that in a separate textbox?
2- How can I find out the the amount of time spent (in milliseconds) to run the query, so that I could display that in a separate textbox?
The cmd object doesn't have a RecordCount or ElapsedTime property
Please advise.
Thanks.
-
May 25th, 2024, 03:06 AM
#2
Re: Need help with Record Count and Elaped Time when running sqLite queries
 Originally Posted by IliaPreston
1- How can I find out the number of records (selected or otherwise affected), so that I could display that in a separate textbox?
Both Fill and ExecuteNonQuery return the number of records retrieved and affected respectively.
 Originally Posted by IliaPreston
2- How can I find out the the amount of time spent (in milliseconds) to run the query, so that I could display that in a separate textbox?
You can measure the amount of time your application spends executing a section of code using the Stopwatch class. If you mean the actual time the database spent executing the SQL then that's a different matter.
-
May 25th, 2024, 03:08 AM
#3
Re: Need help with Record Count and Elaped Time when running sqLite queries
DRY. You are setting the CommandText to the same value in both the 'if' and 'else' blocks, so it doesn't belong in either.
-
May 27th, 2024, 07:37 AM
#4
Thread Starter
Fanatic Member
Re: Need help with Record Count and Elaped Time when running sqLite queries
 Originally Posted by jmcilhinney
Both Fill and ExecuteNonQuery return the number of records retrieved and affected respectively.
You can measure the amount of time your application spends executing a section of code using the Stopwatch class. If you mean the actual time the database spent executing the SQL then that's a different matter.
Thanks a lot for your help.
Now, it works fine.
I really appreciate it.
Ilia
-
May 27th, 2024, 07:40 AM
#5
Thread Starter
Fanatic Member
Re: Need help with Record Count and Elaped Time when running sqLite queries
 Originally Posted by jmcilhinney
DRY. You are setting the CommandText to the same value in both the 'if' and 'else' blocks, so it doesn't belong in either.
I totally understand what you are saying, and I totally agree with it, but, that doesn't cause any problem.
It works fine.
So, really speaking, if that line of code is repeated in those two blocks, who cares?
-
May 27th, 2024, 08:39 AM
#6
Re: Need help with Record Count and Elaped Time when running sqLite queries
 Originally Posted by IliaPreston
I totally understand what you are saying, and I totally agree with it, but, that doesn't cause any problem.
It works fine.
So, really speaking, if that line of code is repeated in those two blocks, who cares?
I would say it matters because it becomes habit forming, one duplicated line here and there may not seem a lot but over time they add up. This makes troubleshooting harder, maintenance harder, bug fixing harder, and even being able to skim and understand code harder.
I would always suggest aiming for clean, easy to understand code; especially when doing so would be a matter of seconds such as in the case jmcilhinney pointed out.
-
May 27th, 2024, 09:34 AM
#7
Re: Need help with Record Count and Elaped Time when running sqLite queries
It doesn't cause any problems now but who's to say it won't in the future? How many times do you think someone has had duplicated code that they thought was not a problem and then later changed one instance but forgot to change the other. If you do things the right way every time, you're not going to do them the wrong way that one time that is actually does matter.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|