I am trying to establish a connection with sql server but i am not able to do so. Can some one help me out?

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 http://connectionstrings.com/sql-server-2008


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

Opening braces was missed . Corrected it


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.