Showing posts with label connection string. Show all posts
Showing posts with label connection string. Show all posts

Thursday, September 11, 2008

Connecting to a Data Provider

Problem
You are writing an application that interacts with a database, and you need to connect to it to run some queries.

Solution
Use a Connection object and a "connection string" to establish the connection you will use for queries and updates.

Discussion
The following set of statements establishes a connection to a SQL Server Express database named MyDatabase running on the system named MySystem, using the active Microsoft Windows login account for its security access:

Dim theDatabase As System.Data.SqlClient.SqlConnection
Dim connectionString As String = _
"Data Source=MySystem\SQLEXPRESS;" & _
"Initial Catalog=MyDatabase;Integrated Security=true"

theDatabase = New SqlClient.SqlConnection(connectionString)
theDatabase.Open( )
' ---- Perform database processing here, then…
theDatabase.Close( )
theDatabase.Dispose( )