Solved

MSSQL Timeout

Posted on 2006-11-24
12
971 Views
Last Modified: 2013-12-25
MSSQL 2000 s/p 4
vb 6 s/p 4
Application run on Win2000  Server

I have a application that goes through a database tables and defrags the indexes.  These are quite large tables some having over 20 million records.


Set conn = New ADODB.Connection
conn.CommandTimeout = 1800 ' wait 30 min. before time out
conn.CommandTimeout = 1800
conn.Open strConnectionString

Set rs = New ADODB.Recordset
rs.Open SQL, conn, adOpenForwardOnly, adLockReadOnly


However I keep getting a
[Microsoft][ODCB SQL Server Driver] Timeout expired
After about 30 sec.


0
Comment
Question by:PhilChapmanJr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 18008652
Hi PhilChapmanJr,

isn't the CommandTimeOut property in milliseconds? You need to multiply the value by 1000.

Cheers!
0
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 18008747
I tried changeing the CommandTimeOut property to 1800000 and still the same error
0
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 18008773
Accouding to everything I have found the CommandTimeOut is in seconds
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 18008953
The conn.ConnectionTimeout = 1800 is also set below the conn.CommandTimeout = 1800
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18009109
For databases this size, I would recommend that you do this from an SQL Agent job instead of VB. ADO is not really geared towards handling processes like this. An alternative is to use the SQLDMO (for SQL 2000 servers and earlier) or the SMO (for SQL 2005) if you want to be able to handle this programatically in your own custom application.
0
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 18009304
I have found sample of code to get the database using SQLDMO but not such things as
Defraging and Rebuilding Indexes using SQLDMO

Could you send me a example of how to defrag and then how to rebuild the
Database: Northwind
Table: Customers
Index: CompanyName

using SQLDMO
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18009310
I would have to write it quickly - please bear with me
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18009346
OK - reference the SQLDMO in your project.

Then with a new Standard EXE project:

Option Explicit

Private Sub Form_Load()
  Dim SQLServer As SQLDMO.SQLServer
  Dim db As SQLDMO.Database
  Dim sSQL As String
  Dim sMessages As String
 
  sSQL = "DBCC INDEXDEFRAG(Northwind, Customers, CompanyName)"


  Set SQLServer = ConnectToSQL(".", "sa", "password")
  Set db = SQLServer.Databases("Northwind")
 
  db.ExecuteImmediate sSQL, SQLDMOExec_ParseOnly
  db.ExecuteWithResultsAndMessages sSQL, , sMessages
 
  Set db = Nothing
  Set SQLServer = Nothing
 
End Sub

Public Function ConnectToSQL(ByVal Server As String, ByVal UID As String, ByVal PWD As String) As SQLDMO.SQLServer
  Dim oSQL As SQLDMO.SQLServer

  On Error GoTo MY_ERR

  Set oSQL = New SQLDMO.SQLServer
  On Error Resume Next
  If Len(UID) = 0 Then
    PWD = ""
    oSQL.LoginSecure = True
    oSQL.Connect Server
    If Err.Number = -2147221504 Then
      On Error GoTo MY_ERR
      oSQL.Start True, Server
    ElseIf Err.Number Then
      GoTo MY_ERR
    End If

  Else
    oSQL.LoginSecure = False
    oSQL.Connect Server, UID, PWD
    If Err.Number = -2147221504 Then
      On Error GoTo MY_ERR
      oSQL.Start True, Server, UID, PWD
    ElseIf Err.Number Then
      GoTo MY_ERR
    End If
  End If

MY_END:
  On Error Resume Next
  Set ConnectToSQL = oSQL
  Exit Function

MY_ERR:
  Set oSQL = Nothing
  Resume MY_END
End Function
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18009349
You can use the same objects and model for http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/VB_Databases/Q_22068362.html where I also recommended using the SQLDMO

Cheers
Night
0
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 18009390
DBCC INDEXDEFRAG
Works great except when the defrag is run in Enterprise and using ADO it returns the pages moved etc.  How can I retrieve this information.



DBCC DBREINDEX:
Go to
DBCC DBREINDEX  and VB6
that I posted a couple of days ago copy the examples you gave me and received 500 more points.
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18009429
Option Explicit

Private Sub Form_Load()
  Dim SQLServer As SQLDMO.SQLServer
  Dim db As SQLDMO.Database
  Dim sSQL As String
  Dim sMessages As String
  Dim x As SQLDMO.QueryResults2
 
 
  sSQL = "DBCC INDEXDEFRAG(Northwind, Customers, CompanyName)"


  Set SQLServer = ConnectToSQL(".", "sa", "883cidev2288")
  Set db = SQLServer.Databases("Northwind")
 
  db.ExecuteImmediate sSQL, SQLDMOExec_ParseOnly
  Set x = db.ExecuteWithResultsAndMessages(sSQL, , sMessages)
  Debug.Print x.ColumnName(1) & " " & x.GetColumnLong(1, 1)
  Debug.Print x.ColumnName(2) & " " & x.GetColumnLong(1, 2)
  Debug.Print x.ColumnName(3) & " " & x.GetColumnLong(1, 3)
 
  Set db = Nothing
  Set SQLServer = Nothing
 
End Sub

Public Function ConnectToSQL(ByVal Server As String, ByVal UID As String, ByVal PWD As String) As SQLDMO.SQLServer
  Dim oSQL As SQLDMO.SQLServer

  On Error GoTo MY_ERR

  Set oSQL = New SQLDMO.SQLServer
  On Error Resume Next
  If Len(UID) = 0 Then
    PWD = ""
    oSQL.LoginSecure = True
    oSQL.Connect Server
    If Err.Number = -2147221504 Then
      On Error GoTo MY_ERR
      oSQL.Start True, Server
    ElseIf Err.Number Then
      GoTo MY_ERR
    End If

  Else
    oSQL.LoginSecure = False
    oSQL.Connect Server, UID, PWD
    If Err.Number = -2147221504 Then
      On Error GoTo MY_ERR
      oSQL.Start True, Server, UID, PWD
    ElseIf Err.Number Then
      GoTo MY_ERR
    End If
  End If

MY_END:
  On Error Resume Next
  Set ConnectToSQL = oSQL
  Exit Function

MY_ERR:
  Set oSQL = Nothing
  Resume MY_END
End Function
0
 
LVL 29

Accepted Solution

by:
Nightman earned 500 total points
ID: 18009436
Note the type QueryResults2
This is really and array results returned by the DMO - you can interrogate it much like a recordset (not quite as intuitively though).

And DBCC DBREINDEX  and VB6 is still open ;)

Cheers
Night
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

617 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