connection string builder help

dsk96m

Well-known member
Joined
Jan 11, 2013
Messages
173
Programming Experience
1-3
So i have a connection string saved in my visual studio application settings. It does not include a user name or password. It is an odbc oracle connection.

In my code, i need to do some in code construction. I need to append the user id and pw to the connection string from the login form. I have seen examples of how to do this with sql, but can find any examples with odbc. I know it is part of the dbconnectionstringbuilder class, but my research has been with no luck.

For the sqlserver it is like this:
VB.NET:
dim sqlbuilder as new sqlclient.sqlconnectionstringbuilder (my.settings.mysavedconnectionstring)
sqlbuilder.userid= me.username
sqlbuilder.password=me.password

dim sqlcon as new sqlclient.sqlconnection
sqlcon.connectionstring=sqlbuilder.connectionstring
sqlcon.open()

Can someone help me do this using an odbc connection instead of an sqlserver?

Thank you
 
What I would do in this case is simply use String.Format on the connection string itself and fill in the UN & PW that way.
Example:
Dim ConnectionStringBase As String = "Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}"
Dim Server As String = "SomeDBServer"
Dim DBName As String = "SomeDatabase"

Dim UN As String = "SomeUserName"
Dim PW As String = "SomePassword"

Dim ConnectionString As String = String.Format(ConnectionStringBase, Server, DBName, UN, PW)
MessageBox.Show(ConnectionString)
 
The OdbcConnectionsStringBuilder has no properties that correspond directly to user name and password but any connection string builder can add an attribute with any name via its Add method and then access it via its Item property, e.g.
VB.NET:
Dim builder As New Odbc.OdbcConnectionStringBuilder

builder.Add("UserName", "theUserName")
builder.Add("Password", "thePassword")

MessageBox.Show(builder.ConnectionString)
MessageBox.Show(CStr(builder("UserName")))
MessageBox.Show(CStr(builder("Password")))
 
Back
Top