What Should I Know About OdbcConnection Connection Strings?

0
0
Asked By CuriousCactus72 On

Hey everyone! I'm diving into some SQL work with PowerShell and I've put together a connection string for my ODBC connection, but I could really use some help understanding it better. Here's the snippet of my code:

`$conn = new-object System.Data.Odbc.OdbcConnection`
`$conn.connectionstring = "DRIVER={MySQL ODBC 9.4 ANSI Driver};Server=$MyServer;Database=$MyDatabase;UID=$($MRDB_TestCred.Username);PWD=$($MRDB_TestCred.GetNetworkCredential().Password);Option=3;MULTI_HOST=1; charset=UTF8; timeout=30;"`

I have a few questions:
1. Are there additional attributes I should know about for the "connectionstring"?
2. Is there a way to use a credential attribute instead of just "UID" and "PWD"?
3. What does "MULTI_HOST=1" do, and why can't it connect without it?
4. Lastly, is there a better method for making this connection? I've tried using "Invoke-Sqlcmd" and "System.Data.SqlClient.SqlConnection," but I've run into errors I can't resolve since my DBA is out for a while. Thanks for any insights!

2 Answers

Answered By TechSavvy101 On

You might want to start by running `$conn | Get-Member -Force` to see the available properties and methods for the OdbcConnection object. This will help you understand what else you can do with your connection string! But just a heads up, this won't show specific attributes for the connection string itself, which you'll need to look up online for more detailed formatting info.

HelpfulHank93 -

True, but keep in mind that the connection string format is documented online. You can refer to Microsoft’s docs for a comprehensive list. Just remember that for credentials, you usually have to stick with clear text for UID and PWD unless you're using SSO.

Answered By DatabaseNinja On

Here's a neat trick! Create a text file and change its extension to .udl. Open it, and you'll get a GUI where you can set the server name, credentials, and database name, then test the connection. After you’ve set everything up, save it and open it as text. You'll snag a properly formatted connection string with all parameters included!

ScribeSquad -

Oh, I love that! You could automate it with PowerShell too. Just create the .udl file programmatically, open it to edit, and retrieve your connection string afterward. That's super handy!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.