Decompose a valid SQLConnection string

  • Thread starter Thread starter A.M
  • Start date Start date
A

A.M

Hi,

What would be the best way to decompose a valid sql server connection
string? I am looking for four items (server name, db name, username,
password)

I guess I can use SqlConnection class to decompose the connection string;
however my concern is that it establishes the actual connection, so it takes
resources.

Thanks,
Ali
 
I guess the most easy way is indeed creating a connection object, passing
the connection string to it and read the properties that you need.
A connection object will only establish a connection after you call the
myConnection.Open() method.
An other solution would be to parse the connection string with the Split()
function, but this is more work.

Regards,

--

Nico Debeuckelaere (MVP VB.NET)

ND-Sign BVBA (Microsoft Certified Partner since 2004)
Pierstraat 135
B-2840 Rumst
URL: http://www.nd-sign.com
== ND-Sign, Designed for you ==
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
A.M said:
What would be the best way to decompose a valid sql server connection
string? I am looking for four items (server name, db name, username,
password)

I guess I can use SqlConnection class to decompose the connection string;
however my concern is that it establishes the actual connection, so it takes
resources.

Surely it'll only establish the connection if you call Open - if you
don't do that, I'd expect everything would be okay.
 
YOu don't need to open the connection to get the information, however
Username and Password aren't provided:

Dim cn As New SqlConnection("data source=xxxxx;initial
catalog=HumanResources;integrated security=SSPI;persist security
info=False;workstation id=SAMEER;packet size=4096")

MessageBox.Show(cn.Database) 'Human Resources

MessageBox.Show(cn.DataSource.ToString) 'xxxx

MessageBox.Show(cn.ConnectionTimeout.ToString)'15

MessageBox.Show(cn.WorkstationId.ToString) 'SAMEER

This is the exact code, I didn't open a connection or anything else



With that siad, you can use String.Split like someone recommended splitting
on the ";" but this will fail if someone uses a semicolon in their password
which is fairly common in the strong password world.



I'd use Regex.Split and match each piece since Pwd and Password for instance
are both valid.


HTH,



Bill
 
Back
Top