Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Time out in a SQL Query VB.NET

Posted on 2011-09-17
8
Medium Priority
?
334 Views
Last Modified: 2012-05-12
Hi all,

Im getting the following message when updating records on a SQL Server:

Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

The code is very simple:

Dim cadena2 As String = "update available_mills set status=1 where id=99 and location=@f3"
Dim cmd2 As New SqlCommand(cadena2, con2)
cmd2.CommandType = CommandType.Text
cmd2.Parameters.AddWithValue("@f3", F3)
cmd2.ExecuteNonQuery()

This updates about 4000 records every hour

Thanks!
0
Comment
Question by:pvg1975
8 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 36554544
set connection timeout to max

con2.timeout=0 this will be no time out
0
 

Author Comment

by:pvg1975
ID: 36554559
Im getting

Error      1      'timeout' is not a member of 'System.Data.SqlClient.SqlConnection'.

When using con2.timeout=0
0
 

Author Comment

by:pvg1975
ID: 36554589
I used this:

con2.CreateCommand.CommandTimeout = 0

And it seems to work. Am I doing it right? or createcommand is for something else?

Thanks!
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 15

Accepted Solution

by:
Haris Djulic earned 1000 total points
ID: 36554755
it is beter to use the timeout with the command since:

SqlCommand.CommandTimeout = timeout limit for your SQL query. Means, how much time a (eg: SELECT, UPDATE) query can take for its execution. If it exceeds SqlCommand.CommandTimeout, then it stops execution. A command timeout error will occur.

SqlConnection.ConnectionTimeout = timeout limit for your connection. Means, how much time your connection object can try to connect. If it exceeds the specified time, it stops connecting. A connection timeout error will occur.

you can use it like that

cmd2.CommandTimeout =0
0
 

Author Comment

by:pvg1975
ID: 36554888
Thanks, i tried it but i get an error. I will paste the error once im back home. Thanks.
0
 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 36555928
A sqlConnection has a ConnectionTimeout property. It  is a property which determines the maximum number of seconds your code will wait for a connection to the database to open. It is set in the connectionstring to the database

data source=BROCHIS;initial catalog=Indato;integrated security=SSPI;Connect Timeout=60;

Opening the connection does not do anything in the database yet. That is handled by a SqlCommand, By default this is 30 seconds. When you perform something costly in the database this is the one whose value you’ll have to change.

SqlCommand cmd;

cmd.CommandTimeout = 60;

Hope this will work...
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 1000 total points
ID: 36556778
There is simply no reason to increase the CommandTimeout (let alone set it to 0-Infinte) for such a simple UPDATE.  All you need to do is optimize your query.

Do you have an index on id, location?
0
 

Author Closing Comment

by:pvg1975
ID: 36556815
That worked perfect. I also optimized the table and added an index to the ID field. Thanks guys!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question