Start New Discussion within our Software Development Community
public void connect() 
    SqlConnection connection = new SqlConnection("connectionString");

    try {
    catch {
         // Show error
    finally {

Don't forget to add


reference to project.

You can create a new connection from Server Explorer and copy connection strings from properties in your project. Following link contains steps to create connection from Server Explorer

You can try following Connectionstring

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

You can also get list of connection string from


Do you know what is Connection string to database?

I have gave you all the information required in post above. You canot simply set the path to the database, and use it as a connection string. Connection string is consisted of more then one parameters. This is an example:

connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\MyProject\dbRoomBooking.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"

If you are in C#, you can create an applicaion configuration file (app.Config), which will be having the connection string, with its name:

(Example of app.Config):

<?xml version="1.0" encoding="utf-8" ?>
    <add name="myConnString"
        connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\AppsTest\Nov17RoomBooking\dbRoomBooking.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
        providerName="System.Data.SqlClient" />

So the code shoudl look like:

private static string sqlConnectionString = ConfigurationManager.ConnectionStrings["myConnString"].ConnectionString;
        private void DeletingDataBase()
            using (SqlConnection sqlConn = new SqlConnection(sqlConnectionString))
                using (SqlCommand cmd = new SqlCommand())
                    string sqlQuery = "SELECT, INSERT, UPDATE, DELETE";
                    cmd.Connection = sqlConn;
                    cmd.CommandText = sqlQuery;
                    catch { }

ATTENTION: To use ConfigurationManager class, you have to add new Reference (right click on References in Solution explorer, and Add, go down to select System.Configuraton - and dont forget to add a new nameSpace on the top of the class, where you have the upper code I gave you:
using System.Configuration;

This tutorial explains all what you have to know to get your code working.


try this code..

        connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" 
        cnn = new SqlConnection(connetionString);
            MessageBox.Show ("Connection Open ! ");
        catch (Exception ex)
            MessageBox.Show("Can not open connection ! ");

Source...SQL Server Connection


Connection = ConfigurationManager.ConnectionStrings["ContentTestConnection"].ToString();

Here ContentTestConnection is the name of the connection string whose values is the information about the the database connectivity.

using (MySqlConnection objconnection = new MySqlConnection(Connection))
                   using (MySqlCommand cmd = new MySqlCommand(query, objectconnection))

I hope this helps.

Feel free to ask questions
This article has been dead for over six months. Start a new discussion instead.