1

Is there a way/code where I can use that won't require me to change my connection string everytime I move my project to another computer?

been using 2 computers with different server names, but with the same database.

PC1:

static string ConnStr = "Server=DESKTOP-Q0BI1S3;Database=ISPROJ2;Trusted_Connection=True;";

PC2:

static string ConnStr = "Server=DESKTOP//SEGUERRA;Database=ISPROJ2;Trusted_Connection=True;";

tried using: Server=(localdb)

Update: used localhost and (local) with PC1 worked fine, but these won't work with PC2 see img

2
  • 1
    config file? registry value? That's the way 99% of applications work. Commented Feb 19, 2017 at 6:14
  • 1
    Use localhost instead of the PC name. Commented Feb 19, 2017 at 6:23

5 Answers 5

3

I am not sure if this will work for you, but where I work everyone has their own local instance of sql server and each developer are using the db on localhost. We solve this problem by referencing the database as a dot (localhost).

"Server=.;Database=ISPROJ2;Trusted_Connection=True;"

This solution only works if all developers have their db installed as the default instance.

Sign up to request clarification or add additional context in comments.

Comments

2

See here.

This may be the solution you are looking for. Use the hostname and append it to the connection string.

I also believe you may be able to use server=localhost;

2 Comments

tried using 'localhost' for PC1.. worked fine.. but with PC2 i didn't. I get this error. see img
Have you tried appending the hostname? The hostname should copy what you have above such as Server=DESKTOP//SEGUERRA; if not, print it out and paste it in the comments. We'll get it figured out. =) On a side note, you should wrap your code in a try{}catch(SQLException e){} that way the program doesn't blow up when you try to connect and there's nothing to connect to.
0

You could make one computer work as a server and connect to it everytime with same connection string.

This might help you: https://technet.microsoft.com/en-us/library/ms175483(v=sql.105).aspx

Comments

0

There are probably lots of libraries to solve this, but the simplest way you can do interim is to within the software identify which computer the software is run on:

static string GetConnectionString()
{
    switch(System.Environment.MachineName)
    {
        case "PC1": //TODO:Change this to the actual machine name!
            return "Server=DESKTOP-Q0BI1S3;Database=ISPROJ2;Trusted_Connection=True;";            
        case "PC1": //TODO:Change this to the actual machine name!
            return "Server=DESKTOP//SEGUERRA;Database=ISPROJ2;Trusted_Connection=True;";
    }
}

You can also make it more dynamic by reading it from the web.config/app.config:

static string GetConnectionString()
{
    return ConfigurationManager.AppSettings["ConnectionString_" + System.Environment.MachineName];
}

This makes it more dynamic, and you can simply add a new connection string onto the web.config/app.config once it needs to run on a new environment.

<appsettings>
    <add key="connectionstring_pc1" value="[pc1connectionstringhere!]"/>
    <add key="connectionstring_pc2" value="[pc2connectionstringhere!]"/>
</appsettings>

Comments

0

You will find all SQL Server connection string options here.

https://www.connectionstrings.com/sql-server/

Standard Security
Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;

Trusted Connection
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

Connection to a SQL Server instance
The server/instance name syntax used in the server option is the same for all SQL Server connection strings.

Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.