DataContext.CreateDatabase() 报告文件已存在,但实际上并不存在。

3

这可能是一个Windows 7问题,但调用该程序的方法可能需要一些修改。

using (var context = new DataClassesDataContext())
{
    if (!context.DatabaseExists())
    {
        context.CreateDatabase();
    }
}

以下错误信息:

以下是错误结果:

System.Data.SqlClient.SqlException was unhandled Message=数据库'C:\Temp\SmallBusinessManager.mdf'已存在,请选择不同的数据库名称。 Source=.Net SqlClient Data Provider ErrorCode=-2146232060 Class=16 LineNumber=1 Number=1801 Procedure=""
Server=\.\pipe\952FCA9D-B4B6-4C\tsql\query State=3 StackTrace: at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Data.Linq.SqlClient.SqlProvider.ExecuteCommand(String command) at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.CreateDatabase() at System.Data.Linq.DataContext.CreateDatabase()

然而,我已经删除了数据库文件。这是什么原因?这些文件真的存在但看不见吗?是否与联接有关?


我的代码以前可以运行,但现在开始出现相同的错误 - 可能是系统更新或其他原因。无论如何,我找到了解决这个问题的方法,并将其作为答案发布在下面,以防有所帮助。 - John K
3个回答

4

追踪错误

我也试图通过运行DataContext.CreateDatabase()命令创建一个基于文件的数据库(名为C:\y\bjdatamig\IntermediateData.mdf),但是遇到了同样的错误。

我使用的是Vista SP2,SQLExpress 2008和VS2008,并像Josh在这里所建议的那样使用Process Monitor。 这让我看到SQL Server正在写入其error.log文件,从而揭示了以下相关信息块(在此提供全部内容,以防其他人在消息中发现有用的内容):

2010-06-23 17:04:10.50 Server      SQL Server is starting at normal priority base (=7). This is an informational message only. No user action is required.
2010-06-23 17:04:10.50 Server      Detected 2 CPUs. This is an informational message; no user action is required.
2010-06-23 17:04:10.55 Server      Perfmon counters for resource governor pools and groups failed to initialize and are disabled.
2010-06-23 17:04:10.55 Server      Using dynamic lock allocation.  Initial allocation of 2500 Lock blocks and 5000 Lock Owner blocks per node.  This is an informational message only.  No user action is required.
2010-06-23 17:04:10.65 Server      Node configuration: node 0: CPU mask: 0x00000003 Active CPU mask: 0x00000003. This message provides a description of the NUMA configuration for this computer. This is an informational message only. No user action is required.
2010-06-23 17:04:10.69 spid7s      Starting up database 'master'.
2010-06-23 17:04:10.79 spid7s      1 transactions rolled forward in database 'master' (1). This is an informational message only. No user action is required.
2010-06-23 17:04:10.91 spid7s      0 transactions rolled back in database 'master' (1). This is an informational message only. No user action is required.
2010-06-23 17:04:10.91 spid7s      Recovery is writing a checkpoint in database 'master' (1). This is an informational message only. No user action is required.
2010-06-23 17:04:11.12 spid7s      Error: 5598, Severity: 16, State: 2.
2010-06-23 17:04:11.12 spid7s      FILESTREAM feature is not supported on user instances.
2010-06-23 17:04:11.12 spid7s      FILESTREAM: effective level = 0, configured level = 0, file system access share name = 'SQLEXPRESS'.
2010-06-23 17:04:11.25 spid7s      SQL Trace ID 1 was started by login "sa".
2010-06-23 17:04:11.31 spid7s      Starting up database 'mssqlsystemresource'.
2010-06-23 17:04:11.33 spid7s      The resource database build version is 10.00.2531. This is an informational message only. No user action is required.
2010-06-23 17:04:11.90 spid9s      Starting up database 'model'.
2010-06-23 17:04:11.91 Server      Server local connection provider is ready to accept connection on [ \\.\pipe\95B6C915-3DB7-46\tsql\query ].
2010-06-23 17:04:11.94 Server      Dedicated administrator connection support was not started because it is disabled on this edition of SQL Server. If you want to use a dedicated administrator connection, restart SQL Server using the trace flag 7806. This is an informational message only. No user action is required.
2010-06-23 17:04:11.95 spid7s      Server name is 'JOHN-W500\95B6C915-3DB7-46'. This is an informational message only. No user action is required.
2010-06-23 17:04:12.22 spid7s      Starting up database 'msdb'.
2010-06-23 17:04:12.27 Server      The SQL Server Network Interface library could not register the Service Principal Name (SPN) for the SQL Server service. Error: 0x5, state: 4. Failure to register an SPN may cause integrated authentication to fall back to NTLM instead of Kerberos. This is an informational message. Further action is only required if Kerberos authentication is required by authentication policies.
2010-06-23 17:04:12.27 Server      SQL Server is now ready for client connections. This is an informational message; no user action is required.
2010-06-23 17:04:12.60 spid9s      Clearing tempdb database.
2010-06-23 17:04:15.49 spid9s      Starting up database 'tempdb'.
2010-06-23 17:04:16.27 spid12s     The Service Broker protocol transport is disabled or not configured.
2010-06-23 17:04:16.27 spid12s     The Database Mirroring protocol transport is disabled or not configured.
2010-06-23 17:04:16.36 spid12s     Service Broker manager has started.
2010-06-23 17:04:16.37 spid7s      Recovery is complete. This is an informational message only. No user action is required.
2010-06-23 17:04:17.84 Logon       Error: 15350, Severity: 16, State: 1.
2010-06-23 17:04:17.84 Logon       An attempt to attach an auto-named database for file C:\y\bjdatamig\IntermediateData.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
2010-06-23 17:31:29.89 Logon       Error: 15350, Severity: 16, State: 1.
2010-06-23 17:31:29.89 Logon       An attempt to attach an auto-named database for file C:\y\bjdatamig\IntermediateData.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
2010-06-23 17:33:07.09 Logon       Error: 15350, Severity: 16, State: 1.
2010-06-23 17:33:07.09 Logon       An attempt to attach an auto-named database for file C:\y\bjdatamig\IntermediateData.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
2010-06-23 17:49:53.53 Logon       Error: 15350, Severity: 16, State: 1.
2010-06-23 17:49:53.53 Logon       An attempt to attach an auto-named database for file C:\y\bjdatamig\IntermediateData.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
2010-06-23 18:01:02.74 Logon       Error: 15350, Severity: 16, State: 1.
2010-06-23 18:01:02.74 Logon       An attempt to attach an auto-named database for file C:\y\bjdatamig\IntermediateData.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
2010-06-23 18:02:50.92 Logon       Error: 15350, Severity: 16, State: 1.
2010-06-23 18:02:50.92 Logon       An attempt to attach an auto-named database for file C:\y\bjdatamig\IntermediateData.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
2010-06-23 18:04:06.31 Logon       Error: 15350, Severity: 16, State: 1.
2010-06-23 18:04:06.31 Logon       An attempt to attach an auto-named database for file C:\y\bjdatamig\IntermediateData.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
2010-06-23 18:06:41.22 Logon       Error: 15350, Severity: 16, State: 1.
2010-06-23 18:06:41.22 Logon       An attempt to attach an auto-named database for file C:\y\bjdatamig\IntermediateData.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

我的 MySQL 日志文件位置在:

C:\Users\John\AppData\Local\Microsoft\Microsoft SQL Server Data\SQLEXPRESS\error.log

重要的是,对于我的情况,它表明用户实例不支持“功能”,似乎是关于流式传输文件的,当然CreateDatabase正在尝试写入文件系统...

2010-06-23 17:04:11.12 spid7s      Error: 5598, Severity: 16, State: 2.
2010-06-23 17:04:11.12 spid7s      FILESTREAM feature is not supported on user instances.

另外,我的 APP.CONFIG 中的连接字符串指定了一个“用户实例”...

<add connectionString="Data Source=JOHN-W500\SQLEXPRESS;AttachDbFilename=C:\y\bjdatamig\IntermediateData.mdf;Integrated Security=True;User Instance=True" name="IntermediateData" providerName="System.Data.SqlClient" />

解决方案

我将连接字符串从以下内容进行了更改:

;Integrated Security=True;

为了

;Integrated Security=False;

这个数据库是在文件系统上创建的。

为什么我不需要改变“用户实例”选项呢?嗯,我不确定。事实上,在我开始出现这个错误之前,代码是可以运行的,但是我已经一个月没有尝试过了——我的猜测是可能是系统更新或SQL Server的更改导致的。所以虽然已经修复了,但还是有点神秘。


调查分析方面的努力很不错! - code4life
除了在解决方案中出现故障(不是自然的流程)之外,它可能会帮助人们追踪类似的路径,在他们自己的系统上找到解决方案。 - John K

3

下载Process Monitor,您就能够查看文件IO操作以及它们失败的原因。我发现这通常是找到问题根源的最佳和最快速的方法。它是一个非常好用的工具。


+1. ProcMon是在处理晦涩的文件和注册表问题时的第一道防线。 - codekaizen

2

为了删除旧的用户实例,您需要删除以下文件夹并重新启动计算机。如果出现声称文件正在使用的错误,您可以通过Process Explorer“关闭句柄”。

对于XP:

C:\Documents and Settings\username\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS

对于Vista:

C:\Users\username\AppData\Local\Microsoft\Microsoft SQL Server Data\SQLEXPRESS

关于用户实例的技术细节,请阅读题为“SQL Server 2005 Express Edition User Instances”的文章。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接