Script to automatically open SQL Server Management Studio 2005, wait 60 seconds, then press the ALT + C key
I am trying to open SQL Management Studio 2005, wait 60 seconds, then press
the ALT + C key. The reason for this is Microsoft reduced the functionality
of SQL 2005 (those darn Microsoft programmers!!) whereby one has to manually
connect to an instance whereas in SQL 2000 Enterprise Manager it just opened
up automatically - I really miss that. Anyway, I digress...
Anyway, while looking for the classic "Send Keys" program, I came across a
VB script which does some automatic "key pressing". What I need this one to
do is simply open
"C:\Progra~1\Micros~3\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe", wait
60,000 milliseconds (60 seconds), then press ALT + C for me (hit the ALT
then the C button). My draft version below throws an error on line 3, char
1. Can't seem to figure out what I am doing wrong. The OS is Windows
Server 2003.
'Opens SQL 2005 Management Studio and presses the ALT + C button after 60
seconds
Set Wshell = WScript.CreateObject("WScript.Shell")
Wshell.Run "C:\Program Files\Microsoft SQL
Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe", 1, FALSE
WScript.Sleep 60000
Wshell.SendKeys "%C"
Re: Script to automatically open SQL Server Management Studio 2005, wait 60 seconds, then press the ALT + C key
ALT+C selects the "Community" menu. It doesn't connect to anything.
I'm not sure what you are trying to achieve. You are incorrect about
Enterprise Manager I think. It does poll the registered servers on startup
but as far as I'm aware it doesn't do much else until you select a menu
option or a server. Exactly what problem are you having with Management
Studio?
Re: Script to automatically open SQL Server Management Studio 2005, wait 60 seconds, then press the ALT + C key
No need for a VBScript. You can connect automatically in SSMS by speciying
command-line arguments. For example.
"C:\Program Files\Microsoft SQL
Server\90\Tools\binn\VSShell\Common7\IDE\SqlWb.exe" /S "MyServer" /d
"MyDatabase" /E
Re: Script to automatically open SQL Server Management Studio 2005,wait 60 seconds, then press the ALT + C key
Not sure what you mean by that - when I do this, SSMS opens a query
window connected to the specified server and database, with the
Registered Servers window open and Object Explorer open.
Yes, it does not connect Object Explorer for you - but once the query
window is open you can right-click in the query window and select Open
Server in Object Explorer and it will connect to the server for you.
Re: Script to automatically open SQL Server Management Studio 2005, wait 60 seconds, then press the ALT + C key
I am getting the same behavior as you. What I am looking
for it to do is connect Object Explorer for me. That's why I am looking for
help with my error in my SendKeys script. Here it is again below. Does any
scripting guru lnow why I am getting the error?
'Opens SQL 2005 Mgmt Studio and presses ALT+C button after 60 seconds
Set Wshell = WScript.CreateObject("WScript.Shell")
Wshell.Run "C:\Program Files\Microsoft SQL
Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe", 1, FALSE
WScript.Sleep 60000
Wshell.SendKeys "%C"
Re: Script to automatically open SQL Server Management Studio 2005, wait 60 seconds, then press the ALT + C key
When I execute the code I get an error due to a space characters in the path:
The system cannot find the file specified
So I had to pad the string with additional set of quoets.
'Opens SQL 2005 Mgmt Studio and presses ALT+C button after 60 seconds
Set Wshell = WScript.CreateObject("WScript.Shell")
Wshell.Run """C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe""",
1, FALSE
WScript.Sleep 2000
Wshell.SendKeys "%C"
Now it's working. BTW, the 'connect' button is the default button in focus
so you can just send ENTER:
Wshell.SendKeys "~"
If you want more granular control (don't want to wait forever - 60 sec) and
make sure that you can connect as soon as the connect window appears,
then try AutoIT (http://www.autoitscript.com/autoit3/downloads.shtml):
Here's a small script to do the same without waiting so long :)
Run("C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe")
WinWaitActive("Connect to Server")
WinActivate("Connect to Server")
; click the 'connect' button
ControlClick ("Connect to Server", "", "WindowsForms10.BUTTON.app.0.378734a1")
; send the enter key
;Send("{ENTER}")
; send ALT+C
;Send("!C")
Finally, using AutoIT you can compile the script to exe file.
Re: Script to automatically open SQL Server Management Studio 2005, wait 60 seconds, then press the ALT + C key
Shay, you are a gentleman and a scholar. I will try this later!
Re: Script to automatically open SQL Server Management Studio 2005, wait 60 seconds, then press the ALT + C key
Shay, you're a GENIUS! It worked. Working version below.
'Opens SQL 2005 Mgmt Studio and presses ALT+C button after 60 seconds
Set Wshell = WScript.CreateObject("WScript.Shell")
Wshell.Run """C:\Program Files\Microsoft SQL
Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe""",
1, FALSE
WScript.Sleep 60000
Wshell.SendKeys "%C"
Re: Script to automatically open SQL Server Management Studio 2005, wait 60 seconds, then press the ALT + C key
Awesome, now I'm Tring to connect to more than one database.
Any help?
I've use the MS SQL 2008
And The script as fallow:
'Opens SQL 2005 Mgmt Studio and presses ALT+C button after 60 seconds
Set Wshell = WScript.CreateObject("WScript.Shell")
Wshell.Run """C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Ssms.exe""",1, FALSE
WScript.Sleep 10000
Wshell.SendKeys "%C"
Re: Script to automatically open SQL Server Management Studio 2005, wait 60 seconds, then press the ALT + C key
Hi mcorujao,
You can try the below vbscript that is yet untested, as I don't have SQL Server:
HTML Code:
Dim cn, rs
Dim fout
Dim i
Dim line
Const Server = "yourServer"
Const DB = "yourDatabase"
Const Uid = "yourUsername"
Const Pwd = "yourPassword"
Const Proc = "yourProcedure"
Const outFile = "out.csv"
'Open output file
With CreateObject("Scripting.FileSystemObject")
Set fout = .OpenTextFile(outFile, 2, True)
End With
'Connect to database
Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "SQLNCLI"
.ConnectionString = "Server=" & Server & _
";Database=" & DB & _
";Uid=" & Uid & _
";Pwd=" & Pwd & ";"
.CursorLocation = 3
.Open
End With
'Run procedure
Set rs = CreateObject("ADODB.Recordset")
rs.Open Proc, cn, 0, 1, 4
'Dump results to file
Do Until rs.EOF
line = ""
For i = 0 To rs.Fields.Count - 1
If Not IsNull(rs.Fields(i).Value) Then
line = line & ",""" & rs.Fields(i).Value & """"
Else
line = line & ","
End If
Next 'i
fout.WriteLine Mid(line, 2)
rs.MoveNext
Loop
'Clean up
out "#[EoS]#"
rs.Close
cn.Close
fout.Close
Sub out(s)
On Error Resume Next
WScript.StdOut.WriteLine s
End Sub