【问题标题】:Connect string of SQL Server for localhost in Entity Framework在实体框架中为本地主机连接 SQL Server 字符串
【发布时间】:2013-12-08 14:26:01
【问题描述】:

我的系统使用 SQL Server 2012,并使用实体框架,代码优先访问数据库,我的数据库是本地的,但我无法在 ConnectString 中使用 SERVER=localhost; 进行连接

我的App.config(工作中)

  <connectionStrings>
    <add name="SystemContext" connectionString="Server=JOHN-PC\SQLEXPRESS;Database=PizzariaDB;user=sa;password=12345" providerName="System.Data.SqlClient"/>
  </connectionStrings>

但是,如果我将我的系统安装在另一台计算机、其他数据库中,PC 名称不是 JOHN-PC,所以我需要将其更改为通用 ConnectString
但是当我将JOHN-PC\SQLEXPRESS 更改为localhost 时,它不起作用

我可以做些什么来解决这个问题?

【问题讨论】:

  • 对方机器没有sql实例吗?

标签: c# sql-server entity-framework localhost connection-string


【解决方案1】:

如果您在其他远程电脑上也有database,您可以使用dot . 而不是硬编码机器名称。

这里.代表localmachine

试试这个:

<connectionStrings>
    <add name="SystemContext" connectionString="Server=.\SQLEXPRESS;Database=PizzariaDB;user=sa;password=12345" providerName="System.Data.SqlClient"/>
  </connectionStrings>

但是,如果您想将您的机器作为数据库服务器运行,并且希望所有其他电脑与您的电脑进行通信以进行数据库活动,您可以在您的connection string 中使用您机器的IP address

【讨论】:

  • @Lai32290:不客气 :),如果对您有帮助,请将其标记为答案。
  • 好吧,再等 3 分钟 xD
【解决方案2】:

试试这个:

<connectionStrings>
<add name="SystemContext" connectionString="Server=.\SQLEXPRESS;Database=PizzariaDB;user=sa;password=12345" providerName="System.Data.SqlClient"/>

点指向本地机器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2011-09-28
    • 1970-01-01
    相关资源
    最近更新 更多