want to get a connection of local db (.mdf file) in visual studio12. but want to give referrence path RRS feed

  • Question

  • i have made a connection of local db in my project. here is my code.

    class Connection { public SqlConnection GetConnection() { string mycon = @"data source=(LocalDB)\v11.0;" + @"attachdbfilename=G:\distributor_Portal\distributor_Portal\App_Data\DistributorPortal.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True"; SqlConnection con = new SqlConnection(mycon); return con; } public static String GetConnectionString() { return ""; } }

    but my problem is that i do not want to give complete address, like G:\distributor_Portal\distributor_Portal\App_Data\DistributorPortal.mdf iwant to give referrence path for this. can anyone help me out. i will be very thankful to him for this . thanks in advance
    Saturday, February 22, 2014 9:57 AM


  • You don't want to directly connect to the mdf file.  Instead use the service that is running on the PC by either IP address or server name.  The SQL service when a database is shared (used by more than one person) will handle conflict when multiple users try to access the same data.

    Use the connection string below.  Change the Initial Catalog to the database name.  Also change the instance of the database "(LocalDB)\v11.0" to match the name that appears in the login window when you use SQL Server Management Studio (SSMS).  LocalDB should be the PC name and V11.0 is the instance.  You can have multiple instances on SQL Server install on the same PC and depending on version of SQL Server could be Express, Standard, or what ever name that was used when SQL Server was install.

    New connection string

    string mycon = @"data source=(LocalDB)\v11.0;Initial Catalog=myDataBase;integrated security=True;connect timeout=30;MultipleActiveResultSets=True";


    Saturday, February 22, 2014 10:55 AM