Solved

MSSQL Timeout

Posted on 2006-11-24
12
924 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
  • 6
  • 5
12 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
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
Comment Utility
I tried changeing the CommandTimeOut property to 1800000 and still the same error
0
 
LVL 2

Author Comment

by:PhilChapmanJr
Comment Utility
Accouding to everything I have found the CommandTimeOut is in seconds
0
 
LVL 2

Author Comment

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

Expert Comment

by:Nightman
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 29

Expert Comment

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

Expert Comment

by:Nightman
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

15 Experts available now in Live!

Get 1:1 Help Now