Results 1 to 11 of 11

Thread: Script to automatically open SQL Server Management Studio 2005, wait 60 seconds, then press the ALT + C key

  1. #1
    Spin Guest

    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"

  2. #2
    David Portas Guest

    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?

  3. #3
    Dan Guzman Guest

    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

  4. #4
    Spin Guest
    You know how when you open Management Studio you are presented with a
    "Connect to Server" dialog box? Well, the default button is "Connect" and
    you can either press that with your mouse or press ALT + C on the keyboard.
    Well, I am trying to script an ALT + C using my SendKeys VB script. If
    ALT+C selects the "Community" menu, what will select the "Connect" button?

    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"

    Dan, this only launches SSMS a SQL Query window.

  5. #5
    Jeffrey Williams Guest

    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.

  6. #6
    Spin Guest

    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"

  7. #7
    Shay Levi Guest

    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.

  8. #8
    Spin Guest

    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!

  9. #9
    Spin Guest

    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"



  10. #10
    Join Date
    Feb 2011
    Posts
    1

    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"

  11. #11
    Join Date
    Dec 2007
    Posts
    2,291

    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

Similar Threads

  1. Replies: 6
    Last Post: 05-12-2010, 01:47 AM
  2. SQL Server 2005 Management Studio
    By IJAYA in forum Small Business Server
    Replies: 1
    Last Post: 25-06-2009, 02:26 PM
  3. spyware click on advertisement or wait 60 seconds
    By NAEVA in forum Technology & Internet
    Replies: 1
    Last Post: 03-12-2008, 02:21 PM
  4. Replies: 1
    Last Post: 17-10-2008, 03:02 PM
  5. Replies: 3
    Last Post: 17-10-2008, 09:51 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,627,330.84046 seconds with 17 queries