Connecting to SQL DB


I am writing a script in WQL (WMI for SQL) and I need to first connect to a machine running SQL and then specify the database to use. Of course, I can use the "USE" command to specify database to use but I am having an issue knowing which statement(s) I need to sue to establish the actual connection to the SQL DB instance name. Since the WQL query will be run locally on the machine the same SQL box, I am not sure if I need to specify credentials in the connection attempt but if that option is available, I would be interested in this as well.
Any help is appreciated.

Thank You
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Yes you need to. As it is an application, it has to pass the credentials.
Daniel_PLDB Expert/ArchitectCommented:

If windows account under which context application will be running has apropriate SQL Server access you don't need to provide credentials.

Microsoft states as following:

To access the WMI Provider for Server Events, the client on which a WMI management application originates must correspond to Windows authenticated login or group in the instance of SQL Server specified in the application's connection string of the application.

Working with the WMI Provider for Server Events
Understanding the WMI Provider for Server Events

Some samples:
PowerShell and SQL Server Events – Monitoring SP Recompilations
Event Notifications using Powershell, WMI, WQL and SQL Server 2008
SQL Server Administration with Powershell - google books online sample chapters

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EugeneZ ZhitomirskySQL SERVER EXPERTCommented:
There are many SQL (some for sql server,  some for windows)
WQL is sql to query Windows "database" not sql server


Windows Management Instrumentation Query Language (WQL) is Microsoft's implementation of the CIM Query Language (CQL),

if you are really wish to query sql server check above posts links

if you are local windows admin - -with WQL you can check system level of sql server : if service is running , etc
you can try  the below code -- no need for your login info:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" _
    & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_NTLogEvent " _
    & "WHERE Logfile = 'Application",,48)

Charlie_MelegaAuthor Commented:
Thanks Guys
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.