0

几天来,我一直在尝试解决 Castle ActiveRecord 异常。每当我尝试访问数据库时,我都会收到一个 InnerException 值为"An attempt to attach an auto-named database for file database.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."

我正在尝试连接到 SQL Server Express 2005 数据库。我没有任何其他数据库选项,因为学校计算机上只有 SQL Express。

我的连接设置

hibernate.connection.driver_class
  NHibernate.Driver.SqlClientDriver

hibernate.dialect

  NHibernate.Dialect.MsSql2005Dialect
hibernate.connection.provider
  NHibernate.Connection.DriverConnectionProvider

hibernate.connection.connection_string
  Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=database.mdf

即使我将 AttachDBFilename 设置为绝对路径,这也无法正常工作。用户实例无效。数据库文件当前位于项目的根目录中,与我的 WinForms 类混合在一起。

使用时

hibernate.connection.connection_string
  Data Source=localhost;Initial Catalog=FinalApp;Integrated Security=True;Pooling=False

我在笔记本电脑上执行此操作没有任何问题,但我无法在学校计算机上使用 SQL Server 2005 开发人员版。

根据要求进行全栈跟踪

Castle.ActiveRecord.Framework.ActiveRecordException was unhandled
  Message="Could not perform FindAll for Book"
  Source="Castle.ActiveRecord"
  StackTrace:
       at Castle.ActiveRecord.ActiveRecordBase.FindAll(Type targetType, Order[] orders, ICriterion[] criteria) in c:\dev\castle\svn\branches\1.0.x\ActiveRecord\Castle.ActiveRecord\Framework\ActiveRecordBase.cs:line 1022
       at Castle.ActiveRecord.ActiveRecordBase.FindAll(Type targetType) in c:\dev\castle\svn\branches\1.0.x\ActiveRecord\Castle.ActiveRecord\Framework\ActiveRecordBase.cs:line 982
       at Castle.ActiveRecord.ActiveRecordBase`1.FindAll() in c:\dev\castle\svn\branches\1.0.x\ActiveRecord\Castle.ActiveRecord\Framework\ActiveRecordBase.Generic.cs:line 333
       at CSharpFinal.InventoryMain.InventoryMain_Load(Object sender, EventArgs e) in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\CSharpFinal\InventoryMain.cs:line 36
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
       at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Control.Show()
       at CSharpFinal.Form1.inventoryToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\CSharpFinal\Form1.cs:line 51
       at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
       at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
       at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
       at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
       at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ToolStrip.WndProc(Message& m)
       at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at CSharpFinal.Program.Main() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\CSharpFinal\Program.cs:line 17
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()


Castle.ActiveRecord.Framework.ActiveRecordException was unhandled
  Message="Could not create the schema"
  Source="Castle.ActiveRecord"
  StackTrace:
       at Castle.ActiveRecord.ActiveRecordStarter.CreateSchema() in c:\dev\castle\svn\branches\1.0.x\ActiveRecord\Castle.ActiveRecord\Framework\ActiveRecordStarter.cs:line 212
       at CSharpFinal.Form1.reInitializeDatabaseToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\CSharpFinal\Form1.cs:line 65
       at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
       at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
       at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
       at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
       at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ToolStrip.WndProc(Message& m)
       at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at CSharpFinal.Program.Main() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\CSharpFinal\Program.cs:line 17
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

InnerException.Message"An attempt to attach an auto-named database for file database.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."

4

2 回答 2

0

嗯,这是一种猜测,但是您需要指定数据库名称吗?(连接字符串中的初始目录)

于 2009-05-11T20:26:14.170 回答
0

这可能是因为您的 SQL Express 连接字符串正在关闭用户实例。如果您打开用户实例,则 SQL 将在需要时附加数据库,如果没有,它只会打开它。如果您将其关闭,则附加是永久性的。任何再次使用相同连接字符串的尝试都将导致错误,因为数据库已经存在。
因为您没有使用该连接字符串附加数据库,所以您需要先分离数据库,然后才能使用正确的连接字符串。如果您还没有,我建议您下载 SQL Management Studio Express。这将让您看到已附加的数据库并轻松将其分离。

好的,如果这不是问题,SQL Server 可以访问数据库文件吗?它有对该文件夹的权限吗?它通常作为网络服务运行,您可以像任何其他用户一样授予对文件的访问权限。您可以通过授予每个人完全控制权来消除文件权限问题,如果可以解决问题,那么您可以将权限调整为所需的权限。我猜想 SQL 服务器服务帐户将需要 .mdf 文件的修改权限。我不知道它在哪里为用户实例数据库创建 .ldf。它可能需要在与 .mdf 相同的文件夹中创建文件权限。

于 2009-05-11T19:01:34.807 回答