none
Problem in Connection String with using database. RRS feed

  • Question

  • Hi,

     

    I have the problem with the connection string that i am writing in the code.

    I use the following code:

     

    Sqlconnection con=new Sqlconnection("CONNECTION STRING");

     

    Now here when i change my databse's location I have to change the connection string.

     

    What can I do for that?

     

    I want that even if I change the location of my database, I don't change the connection string.

     

    Reply as early as possible.

     

    Friday, August 3, 2007 9:38 AM

All replies

  • The connection string is the only way your program can find your database.

    Friday, August 3, 2007 9:55 AM
  •  

    Hi, jrboddie

     

    I want to know that each time I have to use this connection string in my programme when i am using the database.

     

    I want to remove the connection string from my programme

     

    OR

     

    make the logic that database find itself the path.

     

    Reply..

     

     

    Friday, August 3, 2007 10:00 AM
  • hi manish

    its better to right the connection string code in app config file and if u change the datasource just you have to change that name in app config file rest everything will be same you dont have to crate any new connection string for every time you change your database

     

    this code you have to write in app.config file

    <connectionStrings>

    <add name="myconString" connectionString="Data Source =database(instance of your local sql)  ; Initial Catalog = database name ; Integrated Security = True;"/>

    </connectionStrings>

     

    and in conole you have to write like this

     

    SqlConnection myCon = new SqlConnection();

    string conString = ConfigurationManager.ConnectionStrings["myconString"].ConnectionString;

    myCon.ConnectionString = conString;

     

    and then conitnue doing you operations

     

    the only chage u have to manke is in app.config file  when you change the database name or database source thats it

     

    i think it will help solving your querry

     

    regards

    Kunal

     

    Friday, August 3, 2007 11:11 AM