@manhnguyenv

Welcome to my blog!

SQL Server Query Timeout

March 11, 2020 08:55

SQL Server Query Timeout (edit)

SqlCommand.CommandTimeout

https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.commandtimeout?redirectedfrom=MSDN&view=netframework-4.5#System_Data_SqlClient_SqlCommand_CommandTimeout

using System;
using System.Data.SqlClient;

public class A {

public static void Main() {
string connectionString = "";
// Wait for 5 second delay in the command
string queryString = "waitfor delay '00:00:05'";
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand command = new SqlCommand(queryString, connection);
// Setting command timeout to 1 second
command.CommandTimeout = 1;
try {
command.ExecuteNonQuery();
}
catch (SqlException e) {
Console.WriteLine("Got expected SqlException due to command timeout ");
Console.WriteLine(e);
}
}
}
}

Execution Time-out:

enter image description here

Categories

Recent posts