Results 1 to 7 of 7

Thread: Need help with Record Count and Elaped Time when running sqLite queries

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    835

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,099

    Re: Need help with Record Count and Elaped Time when running sqLite queries

    Quote Originally Posted by IliaPreston View Post
    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.
    Quote Originally Posted by IliaPreston View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,099

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    835

    Re: Need help with Record Count and Elaped Time when running sqLite queries

    Quote Originally Posted by jmcilhinney View Post
    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

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    835

    Re: Need help with Record Count and Elaped Time when running sqLite queries

    Quote Originally Posted by jmcilhinney View Post
    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?

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,855

    Re: Need help with Record Count and Elaped Time when running sqLite queries

    Quote Originally Posted by IliaPreston View Post
    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.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,099

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width