5

I'm making a query in c# with ADO to select one record from Microsoft SQL Express database. for some test purpose I need to make a delay about 300ms with in SQL Express so that I could simulate my required situation. There is a limitations that I can not use any store procedures.

Any clue would be appreciated.

Ehsan Zargar Ershadi
  • 24,115
  • 17
  • 65
  • 95

2 Answers2

5

You could possibly inject a WAITFOR into your SQL:

See this SO QA for more info Sleep Command in T-SQL?

Given a command sent to the database:

SELECT * FROM MyTable

Embedded in a SqlCommand:

using (var conn = new SqlConnection("connection string"))
using (var comm = new SqlCommand("", conn))
{
    conn.Open();
    comm.CommandText = "SELECT * FROM MyTable";
    comm.ExecuteNonQuery();
}

Change it to something like this:

    using (var conn = new SqlConnection("connection string"))
    using (var comm = new SqlCommand("", conn))
    {
        conn.Open();
#if DEBUG
        comm.CommandText = "WAITFOR DELAY '00:00:01.000'; SELECT * FROM MyTable";
#else
        comm.CommandText = "SELECT * FROM MyTable";
#endif
        comm.ExecuteNonQuery();
    }

To embed a 1 second wait.

Ideally you don't want to do this in such an ugly and questionable way, but for the purposes of demonstration it will suffice.

Community
  • 1
  • 1
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
2

You could include a WAITFOR command in your SQL batch.

Or, you could add a lot of data, and query over non-indexed columns, that's sure to make it slow.

driis
  • 161,458
  • 45
  • 265
  • 341