Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
bill writes "OS: Win NT, SP4SQL: Sql7 or Sql 2kI'm trying to determine if Sql server is running on a given machine. I turned off the Sql Server on "tower2" using the SQL Server Service Manager, emulating a failure.In the enlosed code, since Sql Server is not running, the line "oSQLServer.Connect "tower2"" generates an error. Because of "On Error Resume Next", code execution falls into (if(oSQLServer.Status = SQLDMOSvc_Running))Now tooltips shows that SQLDMOSvc_Running = 1 and oSQLServer.Status = <SQL-DMO[Service Controller Error] The RPC Server is unavaliable>. The funny part is that VB regards them as being equal and displays my message box "sql running".I find thie question hard. I hope you don't!TIA,BillPrivate Sub Command1_Click()On Error Resume NextDim oSQLServer As SQLDMO.SQLServerSet oSQLServer = CreateObject("SQLDMO.SQLServer")oSQLServer.LoginSecure = TrueoSQLServer.Connect "tower2"If (oSQLServer.Status = SQLDMOSvc_Running) Then MsgBox "sql running!"Else MsgBox "sql NOT running!"End IfSet oSQLServer = Nothing ' DoneEnd Sub"
Merkin
Funky Drop Bear Fearing SQL Dude!
4970 Posts
Posted - 2002-06-11 : 10:22:00
pffft tooltipshere is a bit of code I use
Function ConnectToServer(sName, sUser, sPass) Dim RetVal Dim oSQL Set oSQL = CreateObject("SQLDMO.SQLServer") On Error Resume Next Call oSQL.Connect(sName, sUser, sPass) If Err.Number = 0 Then RetVal = 0 Else RetVal = 1 End If oSQL.DisConnect Set oSQL = Nothing ConnectToServer = RetValEnd Function
Damian
jasper_smith
SQL Server MVP & SQLTeam MVY
846 Posts
Posted - 2002-06-11 : 16:08:57
You don't need to connect to a server to see its status i.e.Set oSQLServer = CreateObject("SQLDMO.SQLServer") oSQLServer.Name ="tower2"If (oSQLServer.Status = 1) Then MsgBox oSQLServer.Status & "|sql running!" Else MsgBox oSQLServer.Status & "|sql NOT running!" End If Set oSQLServer = Nothing HTHJasper Smith