代码之家  ›  专栏  ›  技术社区  ›  epochwolf

Castle活动记录:SQL Express不工作。SQL开发者是这样做的

  •  0
  • epochwolf  · 技术社区  · 15 年前

    几天来,我一直在尝试解决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 尝试为文件database.mdf附加自动命名数据库失败。存在同名数据库,或无法打开指定的文件,或该文件位于UNC共享上

    2 回复  |  直到 12 年前
        1
  •  0
  •   user31934    15 年前

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

        2
  •  0
  •   pipTheGeek    15 年前

    这可能是因为您的SQL Express连接字符串正在关闭用户实例。如果用户实例处于打开状态,那么SQL将在需要时附加数据库,如果没有,它将直接打开数据库。如果将其关闭,则附加是永久的。再次尝试使用同一连接字符串将导致错误,因为数据库已经存在。
    由于尚未使用该连接字符串连接数据库,因此需要先分离数据库,然后才能使用正确的连接字符串。 如果您还没有SQL management studio express,我建议您下载它。这将使您可以看到已连接的数据库,并轻松地将其分离。

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