以本地系统用户身份运行时,尝试使用C#SqlConnect连接后,LocalDB实例消失了

时间:2019-07-05 15:04:40

标签: c# sql-server sql-server-express localdb

我们正在考虑将LocalDB用于生产中的应用程序。由于我们的服务和Web应用程序以LOCAL_SYSTEM服务的身份运行,因此也有可能以LOCAL_SYSTEM身份运行localdb实例。

我已经测试了一段时间,并且遇到了多个问题。在多台机器上进行了测试,仅x64 OS。

使用x64安装程序的SQL Server v12 / 2014 Express LocalDB。 v14 / 2017提供了相同的结果。没有在x64操作系统上运行的x86安装程序。

C:\Temp>echo %username%
lan

C:\Temp>psexec.exe -s cmd.exe

PsExec v2.2 - Execute processes remotely
Copyright (C) 2001-2016 Mark Russinovich
Sysinternals - www.sysinternals.com


Microsoft Windows [Version 10.0.18362.175]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Windows\system32>cd c:\Temp

c:\Temp>echo %username%
TSTWCD03$

c:\Temp>SQLLocalDB create MyInstance
LocalDB instance "MyInstance" created with version 12.0.2000.8.

c:\Temp>SQLLocalDB start MyInstance
LocalDB instance "MyInstance" started.

c:\Temp>sqlcmd -S "(localdb)\MyInstance" -Q "SELECT GETDATE()"

-----------------------
2019-07-05 16:37:00.043

(1 rows affected)

因此实例现在已经清楚地在运行。.让我们尝试使用我的x64 C#构建应用程序进行连接:

c:\Temp>ODBCTest_x64
Connect using SQL: Data Source = (localdb)\MyInstance; Integrated Security = True; Connect Timeout = 10;
SQLCONN State: Open

c:\Temp>SQLLocalDB info MyInstance
Name:               MyInstance
Version:            12.0.2000.8
Shared name:
Owner:              NT AUTHORITY\SYSTEM
Auto-create:        No
State:              Running
Last start time:    05/07/2019 16:36:36
Instance pipe name: np:\\.\pipe\LOCALDB#F3826724\tsql\query

c:\Temp>dir "C:\Windows\System32\config\systemprofile\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\MyInstance"
 Volume in drive C has no label.
 Volume Serial Number is BC82-AA93

 Directory of C:\Windows\System32\config\systemprofile\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\MyInstance

05/07/2019  16:36    <DIR>          .
05/07/2019  16:36    <DIR>          ..
05/07/2019  16:36                 0 error.log
05/07/2019  16:36            12.030 error1.log
05/07/2019  16:36                 0 error2.log
05/07/2019  16:36         1.048.576 log.trc
05/07/2019  16:36                 0 log_1.trc
05/07/2019  16:36         4.194.304 master.mdf
05/07/2019  16:36         2.097.152 mastlog.ldf
05/07/2019  16:36         2.293.760 model.mdf
05/07/2019  16:36           524.288 modellog.ldf
05/07/2019  16:36        13.434.880 msdbdata.mdf
05/07/2019  16:36           524.288 msdblog.ldf
05/07/2019  16:36            73.728 system_health_0_132068109699630000.xel
05/07/2019  16:36                 0 system_health_0_132068109978530000.xel
05/07/2019  16:36         2.293.760 tempdb.mdf
05/07/2019  16:36           516.096 templog.ldf
              15 File(s)     27.012.862 bytes
               2 Dir(s)  59.358.416.896 bytes free

使用x64 SqlConnection连接成功。

但是我们的服务作为x86应用程序运行...

c:\Temp>ODBCTest_x86
Connect using SQL: Data Source = (localdb)\MyInstance; Integrated Security = True; Connect Timeout = 10;
  

未处理的异常:System.Data.SqlClient.SqlException:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (提供者:SQL网络接口,错误:50-发生本地数据库运行时错误。指定的LocalDB实例不存在。)

     在System.Data.SqlClient.SqlInternalConnectionTds..ctor中的

,SqlAuthenticationProviderManager sqlAuthProviderManager)
  在System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions选项,DbConnectionPoolKey poolKey,对象poolGroupProviderInfo,DbConnectionPool池,DbConnection owningConnection,DbConnectionOptions userOptions)
  在System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection处(DbConnectionPool池,DbConnection owningObject,DbConnectionOptions选项,DbConnectionPoolKey poolKey,DbConnectionOptions userOptions)
  在System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection)
  在System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection)
  在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,布尔值allowCreate,仅布尔值OneCheckConnection,DbConnectionOptions userOptions,DbConnectionInternal&连接)
  在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
1重试,DbConnectionOptions userOptions,DbConnectionInternal oldConnection,DbConnectionInternal&连接)处
  在System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection externalConnection,DbConnectionFactory connectionFactory,TaskCompletionSource 1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1重试,DbConnectionOptions userOptions)处
  在System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1重试)
  在System.Data.SqlClient.SqlConnection.Open()处
  在ODBCTest.Program.Main(String [] args)

c:\Temp>SQLLocalDB info MyInstance
Printing of LocalDB instance "MyInstance" information failed because of the following error:
LocalDB instance "MyInstance" doesn't exist!

c:\Temp>dir "C:\Windows\System32\config\systemprofile\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\MyInstance"
 Volume in drive C has no label.
 Volume Serial Number is BC82-AA93

 Directory of C:\Windows\System32\config\systemprofile\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\MyInstance

05/07/2019  16:36    <DIR>          .
05/07/2019  16:36    <DIR>          ..
05/07/2019  16:36                 0 error.log
05/07/2019  16:36            12.030 error1.log
05/07/2019  16:36                 0 error2.log
05/07/2019  16:36         1.048.576 log.trc
05/07/2019  16:36                 0 log_1.trc
05/07/2019  16:36         4.194.304 master.mdf
05/07/2019  16:36         2.097.152 mastlog.ldf
05/07/2019  16:36         2.293.760 model.mdf
05/07/2019  16:36           524.288 modellog.ldf
05/07/2019  16:36        13.434.880 msdbdata.mdf
05/07/2019  16:36           524.288 msdblog.ldf
05/07/2019  16:36            73.728 system_health_0_132068109699630000.xel
05/07/2019  16:36                 0 system_health_0_132068109978530000.xel
05/07/2019  16:36         2.293.760 tempdb.mdf
05/07/2019  16:36           516.096 templog.ldf
              15 File(s)     27.012.862 bytes
               2 Dir(s)  59.358.351.360 bytes free

发生了WTF吗?与我的x64 SQL LocalDB的x86 SqlConnection刚刚杀死了我的实例……在一个实例中!

c:\Temp>SQLLocalDB create MyInstance
LocalDB instance "MyInstance" created with version 12.0.2000.8.

c:\Temp>SQLLocalDB start MyInstance
LocalDB instance "MyInstance" started.

c:\Temp>SQLLocalDB info MyInstance
Name:               MyInstance
Version:            12.0.2000.8
Shared name:
Owner:              NT AUTHORITY\SYSTEM
Auto-create:        No
State:              Running
Last start time:    05/07/2019 16:53:13
Instance pipe name: np:\\.\pipe\LOCALDB#23C9D1D8\tsql\query

c:\Temp>ODBCTest_x64
Connect using SQL: Data Source=np:\\.\pipe\LOCALDB#23C9D1D8\tsql\query; Integrated Security=True; Connect Timeout=10;
SQLCONN State: Open

c:\Temp>ODBCTest_x86
Connect using SQL: Data Source=np:\\.\pipe\LOCALDB#23C9D1D8\tsql\query; Integrated Security=True; Connect Timeout=10;
SQLCONN State: Open

使用实例管道名称进行连接确实可行!

当LocalDB实例作为LOCAL_SYSTEM运行时,事件日志向我显示此错误:

  

LocalDB实例注册表项中缺少“ DataDirectory”注册表值:{F3917303-37C0-418C-8551-390A6E46EEC3}

使用管道名称可能会起作用,但是我真的更喜欢使用实例名称。关于使用x86库调用时为何失败的任何提示?

0 个答案:

没有答案