创建数据库后无法连接到Microsoft SQL Server数据库(“系统找不到指定的文件”)

我有一个脚本,可以为需要数据库连接的测试运行准备环境(创建一些表并写入一些值,等等)

我有一个PowerShell脚本,可以通过以下方式创建DB_NAME:

& sqllocaldb create DB_NAME
& sqllocaldb start DB_NAME
& sqllocaldb info DB_NAME

输出为:

LocalDB instance "DB_NAME" created with version 13.1.4001.0.
LocalDB instance "DB_NAME" started.

Name:               DB_NAME
Version:            13.1.4001.0

Shared name:        

Owner:              fv-az604\VssAdministrator
Auto-create:        No
State:              Running

Last start time:    11/7/2019 11:47:43 PM

Instance pipe name: np:\\.\pipe\LOCALDB#24C8D765\tsql\query

通过以下代码进行连接

using (SqlCommand cmd = new SqlCommand(sql))
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        cmd.Connection = connection;
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
    }
}

其中的连接字符串如下所示:

Application Name=MyApp;Connect Timeout=60;Data Source=localhost;Initial Catalog=master;Integrated Security=sspi;Connection Reset=false;Min Pool Size=1;Max Pool Size=200;Pooling=true;MultipleactiveResultSets=true;Enlist=false

数据库名称:DB_NAME

完整的堆栈跟踪为:

[Error]: 
Type: System.ComponentModel.win32exception
Message: The system cannot find the file specified
Source: 
Stack:

Type: System.Data.SqlClient.SqlException
Message: A network-related or instance-specific error occurred while establishing a connection to 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. (provider: Named Pipes Provider,error: 40 - Could not open a connection to SQL Server)
Source: .Net SqlClient Data ProviderHelpLink.ProdName: microsoft SQL Server
HelpLink.Evtsrc: MSSQLServer
HelpLink.EvtID: 2
HelpLink.BaseHelpUrl: http://go.microsoft.com/fwlink
HelpLink.LinkId: 20476

Stack:
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity,SqlConnectionString connectionOptions,SqlCredential credential,Object providerInfo,String newPassword,SecureString newSecurePassword,Boolean redirectedUserInstance,SqlConnectionString userConnectionOptions,SessionData reconnectSessionData,DbConnectionPool pool,String accessToken,Boolean applyTransientFaultHandling,SqlAuthenticationProviderManager sqlAuthProviderManager)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options,DbConnectionPoolKey poolKey,Object poolGroupProviderInfo,DbConnection owningConnection,DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool,DbConnection owningObject,DbConnectionOptions options,DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject,DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.Trygetconnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,Boolean allowCreate,Boolean onlyOneCheckConnection,DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.Trygetconnection(DbConnection owningObject,TaskCompletionSource`1 retry,DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.Trygetconnection(DbConnection owningConnection,DbConnectionInternal oldConnection,DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection,DbConnectionFactory connectionFactory,DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection,DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at MyClass.MyFuncABC(String connectionString,String databaseName) in D:\a\1\s\App\MyClass.cs:line 333
   at MyClass.MyFuncAB() in D:\a\1\s\App\MyClass.cs:line 444
   at MyClass.MyFuncA() in D:\a\1\s\App\MyClass.cs:line 555
   at App.Main(String[] args) in D:\a\1\s\App\Main.cs:line 1337
zgfjwzwxb 回答:创建数据库后无法连接到Microsoft SQL Server数据库(“系统找不到指定的文件”)

有关丢失文件的第一条错误消息只是水中的泥泞。真正的一个是第二个,对于每个SQL Server DBA来说都是众所周知的:

  

消息:与网络相关或特定于实例的错误发生在   建立与SQL Server的连接。找不到服务器或   无法访问。验证实例名称正确,并且   SQL Server配置为允许远程连接。 (提供者:命名   Pipes Provider,错误:40-无法打开与SQL Server的连接)

如消息所示,无法建立到SQL Server的连接。客户端库尝试通过TCP / IP,共享内存和命名管道进行连接。由于管道是最后一个选项,并且它也会失败,因此错误包含管道消息。

LocalDB has it's own,相当peculiar syntax,用于connection strings。您所使用的是连接到真正的SQL Server。

也许像

Server=np:\\.\pipe\LOCALDB#24C8D765\tsql\query

会更好。另外,find out您的localdb实例名称,并尝试类似

Server=(localdb)\MyInstance;Integrated Security=true;
本文链接:https://www.f2er.com/3140726.html

大家都在问