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.


LVL 2
Phil ChapmanAsked:
Who is Participating?
 
NightmanCTOCommented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
Hi PhilChapmanJr,

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

Cheers!
0
 
Phil ChapmanAuthor Commented:
I tried changeing the CommandTimeOut property to 1800000 and still the same error
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Phil ChapmanAuthor Commented:
Accouding to everything I have found the CommandTimeOut is in seconds
0
 
Phil ChapmanAuthor Commented:
The conn.ConnectionTimeout = 1800 is also set below the conn.CommandTimeout = 1800
0
 
NightmanCTOCommented:
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
 
Phil ChapmanAuthor Commented:
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
 
NightmanCTOCommented:
I would have to write it quickly - please bear with me
0
 
NightmanCTOCommented:
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
 
NightmanCTOCommented:
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
 
Phil ChapmanAuthor Commented:
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
 
NightmanCTOCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.