Solved

MSSQL Timeout

Posted on 2006-11-24
12
951 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 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

831 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