代码之家  ›  专栏  ›  技术社区  ›  Neil Barnwell

dataContext.createDatabase()表示文件已经存在-但它不存在

  •  2
  • Neil Barnwell  · 技术社区  · 15 年前

    这可能是Windows 7问题,但调用

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

    导致以下错误:

    system.data.sqlclient.sqlException是 未处理的消息=数据库 'C:\temp\smallbusinessmanager.mdf' 已经存在。选择不同的 数据库名称。源=.NET sqlclient 数据提供程序错误代码=-2146232060 等级=16行号=1编号=1801 过程=“”
    服务器=\.\pipe\952fca9d-b4b6-4c\tsql\query 状态=3 stacktrace: at system.data.sqlclient.sqlconnection.onerror(sqlException 异常,布尔断开连接) 在system.data.sqlclient.sqlInternalConnection.onError(sqlException)上 异常,布尔断开连接) 在system.data.sqlclient.tdsarser.throwExceptionAndWarning()处 at system.data.sqlclient.tdsarser.run(运行行为 运行行为,sqlcommand命令命令处理程序, sqldatareader数据流, 批量复制单纯形结果集 BulkCopyHandler,tdSparserStateObject STATEOBO) 在system.data.sqlclient.sqlcommand.runexecutenonqueryds(字符串 方法名,布尔异步) 在system.data.sqlclient.sqlcommand.internalExecuteOnQuery(dbasyncResult 结果,字符串方法名,布尔值 森托佩 在system.data.sqlclient.sqlcommand.executeNonQuery()处 at system.data.linq.sqlclient.sqlprovider.executeCommand(字符串 命令) 在system.data.linq.sqlclient.sqlprovider.system.data.linq.provider.iprovider.createDatabase()上 在System.Data.Linq.DataContext.CreateDatabase()上

    但是我已经删除了数据库文件。是什么导致的?这些文件真的存在但不知怎么看不见吗?和交叉点有关吗?

    3 回复  |  直到 13 年前
        1
  •  3
  •   Josh    15 年前

    抓住 Process Monitor 您将能够看到文件IO操作以及它们失败的原因。我发现这通常是找到问题根源的最好和最快的方法。这是一个很好的实用工具。

        2
  •  3
  •   Community CDub    8 年前

    跟踪错误

    所以我也一直在尝试创建一个基于文件的数据库(名为 C:\Y\bjdatamig\intermediatedata.mdf )通过运行 DataContext.CreateDatabase() 并得到相同的错误。

    我在Vista SP2上使用了SQLExpress 2008和VS2008,并将进程监视器用作 Josh 建议在 his answer 在这里。这让我看到了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.
    

    我的SQL日志文件位于:

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

    重要的是,对于我的情况,它表明用户实例不支持“功能”,似乎是关于流式处理文件,当然 创建数据库 正在尝试写入文件系统…

    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的更改导致了它。所以,尽管修复了,它仍然有点神秘。

        3
  •  2
  •   Chuck Norris Rohidas Kadam    13 年前

    您需要删除以下文件夹并重新启动计算机才能删除旧的用户实例。如果声明文件正在使用时出错,可以通过以下方式“关闭句柄” 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用户实例”的文章。