简体   繁体   English

mdf文件的ASP.NET connectionString不起作用

[英]ASP.NET connectionString for mdf file not working

I am using I am using Visual Studio 2017. I just created a brand new mdf file in my project (Right click on App Data -> Add New Item -> SQL Server Database) and I called the file TestDatabase.mdf 我正在使用Visual Studio2017。我刚刚在项目中创建了一个全新的mdf文件(右键单击App Data-> Add New Item-> SQL Server Database),并将其命名为TestDatabase.mdf

Now I am trying to connect to it via a connection string: 现在,我尝试通过连接字符串连接到它:

<add name="TestConnection" connectionString="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|TestDatabase.mdf;Database=TestDatabase; Trusted_Connection=Yes;" providerName="System.Data.SqlClient" />

Next I created a Model and then Created a EntityFramework Controller, but now when I goto the controller index method I get this error: 接下来,我创建了一个模型,然后创建了一个EntityFramework控制器,但是现在当我进入控制器索引方法时,出现以下错误:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (提供者:SQL网络接口,错误:26-指定服务器/实例时出错)

What am I doing wrong? 我究竟做错了什么?

Make sure you testDatabase.mdf available in App_Data folder. 确保您在App_Data文件夹中可以找到testDatabase.mdf。

<connectionStrings>
  <add name="ConnectionName"
    connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|DatabaseName.mdf;Integrated Security=True;User Instance=True"
    providerName="System.Data.SqlClient" />
</connectionStrings>

This connection string that you have written is incorrect. 您编写的此连接字符串不正确。

<add name="TestConnection" connectionString="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|TestDatabase.mdf;Database=TestDatabase; Trusted_Connection=Yes;" providerName="System.Data.SqlClient" />

I assume that you have SQL Express installed in your system. 我假设您在系统中安装了SQL Express。 This will provide a local instance of SQL to run on your machine. 这将提供要在您的计算机上运行的SQL的本地实例。

As you said you are using Visual Studio 2017, you can follow these steps to get the correct connection string 正如您所说的那样使用Visual Studio 2017,您可以按照以下步骤获取正确的连接字符串

  1. Open Server Explorer 打开服务器资源管理器
  2. Right Click on your Database and click Properties 右键单击数据库,然后单击属性
  3. A property window will open, copy the ConnectionString and make sure it looks like this: 将打开一个属性窗口,复制ConnectionString并确保它看起来像这样:

    <add name="YourDbName" connectionString="Data Source=YourComputerName\\SQLEXPRESS;Initial Catalog=YourDbName;Integrated Security=True" />

And now talking about this: 现在谈论这个:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (提供者:SQL网络接口,错误:26-指定服务器/实例时出错)

You are getting this error because it is not getting the instance name to connect to the database. 您收到此错误,是因为它没有获得实例名称以连接到数据库。 In my example, SQLEXPRESS serves as an instance name. 在我的示例中,SQLEXPRESS用作实例名称。 If you don't have SQLEXPRESS you can download it from here: https://www.microsoft.com/en-in/sql-server/sql-server-editions-express You can follow the prompt and install this package in your system. 如果您没有SQLEXPRESS,则可以从此处下载它: https : //www.microsoft.com/zh-cn/sql-server/sql-server-editions-express您可以按照提示进行操作,并将此软件包安装在您的计算机中系统。

Once you install this, your instance will get activated. 安装此组件后,您的实例将被激活。 Connect your Db once again.I am sure it will work. 再次连接您的Db。我确定它可以正常工作。

In case, if this error comes again, you can follow these steps: 万一再次出现此错误,您可以按照以下步骤操作:

  1. Hit Windows + R and type services.msc and hit OK 按Windows + R,然后键入services.msc,然后按OK。
  2. From the given lists, search for SqlServer (SQLEXPRESS) and click start the service . 从给定的列表中搜索SqlServer(SQLEXPRESS) ,然后单击启动服务 This will activate your instance once again and your database is now ready to connect to your application. 这将再次激活您的实例,数据库现在可以连接到您的应用程序了。

Hope this helps. 希望这可以帮助。

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

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