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

Jolo Roxas
  • 21
  • 1
  • 3

5 Answers5

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.

Fredrik Rudberg
  • 158
  • 1
  • 6
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;

Community
  • 1
  • 1
sheepiiHD
  • 421
  • 2
  • 16
  • tried using 'localhost' for PC1.. worked fine.. but with PC2 i didn't. I get this error. [see img](http://imgur.com/a/544qZ) – Jolo Roxas Feb 21 '17 at 10:10
  • 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. – sheepiiHD Feb 22 '17 at 01:35
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

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>
Micael
  • 155
  • 6
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;
ASH
  • 20,759
  • 19
  • 87
  • 200