Solved

Time out in a SQL Query VB.NET

Posted on 2011-09-17
8
306 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
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 250 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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 

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 250 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now