简体   繁体   English

C#中的数据库连接

[英]Database Connection in C#

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: PC1:

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

PC2: PC2:

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

tried using: Server=(localdb) 尝试使用:Server =(localdb)

Update: used localhost and (local) with PC1 worked fine, but these won't work with PC2 see img 更新:与PC1一起使用localhost和(local)可以正常工作,但是这些不适用于PC2 参见img

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. 我不确定这是否对您有用,但是我在哪里工作,每个人都有自己的sql server本地实例,每个开发人员都在localhost上使用db。 We solve this problem by referencing the database as a dot (localhost). 我们通过将数据库引用为点(localhost)来解决此问题。

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

This solution only works if all developers have their db installed as the default instance. 仅当所有开发人员都将数据库安装为默认实例时,此解决方案才有效。

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; 我也相信您可以使用server=localhost;

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 这可能会帮助您: https : //technet.microsoft.com/zh-cn/library/ms175483(v=sql.105).aspx

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: 您还可以通过从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. 这使它更具动态性,并且只需在web.config / app.config需要在新环境中运行时,就可以将新的连接字符串添加到web.config / app.config中。

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

You will find all SQL Server connection string options here. 您将在此处找到所有SQL Server连接字符串选项。

https://www.connectionstrings.com/sql-server/ 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM