Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

sql server - How do I raise error if the execution of an SQL Query is not completed in specified time in C#?

I'm trying to raise a timeout error in my SQL Server query using c#. I thought a timer would work; however I'm new to c# and have been having trouble figuring it out. I've searched through several questions on this site, and on other sites trying to find out how to raise a timeout error of my query.

Here is my code:

using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = query;
                cmd.CommandTimeout = timeOut;
                cmd.Connection = DBAction.GetSqlConnection(projectId);
                SqlDataReader reader = cmd.ExecuteReader();
                
                    string[] colname = Enumerable.Range(0, reader.FieldCount).Select(x => reader.GetName(x)).ToArray();
                    while (reader.Read())
                            {
                                 //do something
                            }       
            }

I'm not sure on how to add a timer to this that returns an error when the execution of the query takes longer than the specified time, or even where to put it. I've tried finding other posts about it but none of the ones I've found are similar to this; I could also just be really bad at my searches. Any kind of help would be appreciated, and if you need any more information let me know. I renamed a few things for security, but this is how the code is otherwise. To note: I'm using this for testing purposes, and not for production, so only a few people will ever actually see this but it's necessary.

question from:https://stackoverflow.com/questions/65904636/how-do-i-raise-error-if-the-execution-of-an-sql-query-is-not-completed-in-specif

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...