Link to home
Start Free TrialLog in
Avatar of Phil Chapman
Phil ChapmanFlag for United States of America

asked on

MSSQL Timeout

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.


Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Hi PhilChapmanJr,

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

Cheers!
Avatar of Phil Chapman

ASKER

I tried changeing the CommandTimeOut property to 1800000 and still the same error
Accouding to everything I have found the CommandTimeOut is in seconds
The conn.ConnectionTimeout = 1800 is also set below the conn.CommandTimeout = 1800
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.
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
I would have to write it quickly - please bear with me
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
You can use the same objects and model for https://www.experts-exchange.com/questions/22068362/DBCC-DBREINDEX-and-VB6.html where I also recommended using the SQLDMO

Cheers
Night
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.
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
ASKER CERTIFIED SOLUTION
Avatar of Nightman
Nightman
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial