Today's README file explains how to create connection strings to SQL Server databases using Visual Studio's built-in tools.
To provide an example using real source, I created a simple Winforms application named ITworld, written in C#.
Although my example app is written in C#, the techniques and methodologies used in this piece will work for applications written in other languages such as VB.Net.
So let's get started -- click Project, application Properties.
Select the Settings tab, and create a new setting entry. For my simple app, I added a new setting named ITworldConn, changed the Type to (Connection String), and Scope to Application.
To finish your new connection setting, click the small ellipsis button. You'll need to do this to specify: server, login, database, and other connection properties.
The Connection Properties dialog opens. For my sample ITworld application, I changed the data source to Microsoft SQL Server (SQLClient), and entered the necessary Server, Log On, and database values.
To validate your connection, use the Test Connection feature, and click OK if you receive a "test connection succeeded" notification.
But...you may get an error message, which means you'll probably need to double-check the server name, log on credentials, or authentication entries. If you click Test Connection again and receive another error message, you may wish to read my piece on How to fix common SQL Server Management Studio 'Connect to Server' errors.
However if everything works as it should, don't forget to click the Save toolbar icon to keep your new connection string setting. When the project is saved, Visual Studio updates setting information in the app.config XML file.
Next, open app.config to examine its contents. Look for values inside the connectionStrings tag.
name="ITWorldApp.Properties.Settings.ITWorldConn" entry in the example image below.
This is the connection string name, and we can use this to create a connection object in source:
//Retrieve Connection String By Name
string sConn = "ITWorldApp.Properties.Settings.ITworldConn";
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings[sConn];
SqlConnection conn = new SqlConnection(settings.ConnectionString);
Alternatively, you can access the same setting value, reading the Application's properties:
string sConnection = Properties.Settings.Default.ITWorldConn;
And of course, the connection can be used to create SqlCommand or other data objects like a SqlDataReader.
This source creates a SQLcommand using the conn connection object created above.
//Command and Data Reader
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Person.Person";
SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.Default);
Finally, close and dispose of the objects when finished...
In the near future, README file will cover connections to other kinds of servers and databases.
This article is published as part of the IDG Contributor Network. Want to Join?