A Secure Way of Building Connection Strings - Reviewing properties using an example
(Page 3 of 5 )
Add a command button to the Properties.vb form. Insert the contents of the following listing to the click event of the button as shown. The code is adequately commented to explain the various statements in this listing. An existing connection string provides the argument for the constructor of the class, and then looks at the default values of other related properties of the string. Then a complete string is provided and the constituent key/value pairs are accessed programmatically.
Imports System
Imports System.Data.SqlClient
Imports System.Collections Public Class Properties Private Sub
Button1_Click(ByVal sender As System.Object, _ ByVal e As System.
EventArgs)
Handles Button1.Click 'Instantiate a SQL connection string builder
'called() 'myBuilder' with an argument that is
'provided by the function, GetConnString() Dim myBuilder
As New _
SqlConnectionStringBuilder(GetConnString()) 'print out the
connection string Debug.Print(myBuilder.ConnectionString) ' provide
an existing connections string to
' SqlConnectionStringBuilder and you can retrieve and
' modify any of the elements. myBuilder.ConnectionString = _
"server=(local); user id=sa;" & _
"password=XXXXXXX; initial catalog=Northwind"
Debug.Print ("Password: " & _
myBuilder.Password & vbCrLf)
Debug.Print ("Security Info Persist: " & _
myBuilder.PersistSecurityInfo & vbCrLf)
Debug.Print ("Connection Timeout: " & _
myBuilder.ConnectTimeout & vbCrLf)
Debug.Print ("Current Language: " & _
myBuilder.CurrentLanguage & vbCrLf)
Debug.Print ("Initial Catalog: " & _
myBuilder.InitialCatalog & vbCrLf)
Debug.Print ("Asynchronous Processing(yes/no): " & _
myBuilder.AsynchronousProcessing & vbCrLf)
Debug.Print ("Data Source: " & _
myBuilder.DataSource & vbCrLf) End Sub Private Function
GetConnString() As String
Return "Server=(local); Integrated Security=SSPI;" & _
"Initial Catalog=Northwind" End Function
End Class
The coding is made easier since you have complete intellisense support by way of a drop-down listing of object related information as shown in the next picture.

The Debug.print output from the code is run when the button is clicked, as shown in the next paragraph. The password came out in clear text, which has been replaced by 'XXXXX' in this document.
Data Source=(local);Initial Catalog=Northwind; Integrated Security=True
Password: XXXXXX Security Info Persist: False Connection Timeout: 15
Current Language: Initial Catalog: Northwind Asynchronous
Processing(yes/no): False Data Source: (local)
Next: Building a ConnectionString using the class >>
More MS SQL Server Articles
More By Jayaram Krishnaswamy