将项目从 USB 传输到 c:\ 时出现 sql 异常

发布于 2024-08-24 07:15:19 字数 781 浏览 4 评论 0原文

我正在使用 Visual Studio 2008 开发 C# Windows 程序。通常,我在学校直接在 USB 驱动器上工作。但是,当我将文件夹复制到家里的硬盘上时,每当我尝试写入数据库时​​,都会出现 sql 异常未处理的情况。它在 conn.Open() 处未处理;线。这是未处理的异常

数据库“L:\system\project\the_project\the_project\bin\Debug\PatientMonitoringDatabase.mdf”已存在。选择不同的数据库名称。无法将文件“C:\Documents and Settings\Administrator\My Documents\system\project\the_project\the_project\bin\Debug\PatientMonitoringDatabase.mdf”附加为数据库“PatientMonitoringDatabase”。

这很奇怪,因为我的连接字符串是 |DataDirectory|,所以它应该可以在任何驱动器上工作...这是我的连接字符串:

string connStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PatientMonitoringDatabase.mdf; " +
                "Initial Catalog=PatientMonitoringDatabase; " +
                "Integrated Security=True";

I'm working on a C# windows program with Visual Studio 2008. Usually, I work from school, directly on my usb drive. But when I copy the folder on my hard drive at home, an sql exception is unhandled whenever I try to write to the database. it is unhandled at the conn.Open(); line. here's the exception unhandled

Database 'L:\system\project\the_project\the_project\bin\Debug\PatientMonitoringDatabase.mdf' already exists. Choose a different database name. Cannot attach the file 'C:\Documents and Settings\Administrator\My Documents\system\project\the_project\the_project\bin\Debug\PatientMonitoringDatabase.mdf' as database 'PatientMonitoringDatabase'.

it's weird, because my connection string says |DataDirectory|, so it should work on any drive... here's my connection string:

string connStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PatientMonitoringDatabase.mdf; " +
                "Initial Catalog=PatientMonitoringDatabase; " +
                "Integrated Security=True";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笨死的猪 2024-08-31 07:15:19

我认为这里的抱怨是您本地的 SQL Server Express 安装已经有一个
PatientMonitoringDatabase 已连接并正在运行。

使用 SQL Server Management Studio Express 连接到 localhost (如果未安装,请在此处下载)并删除/分离现有的 PatientMonitoringDatabase 数据库。无论它是持久数据库还是仅在正在运行的应用程序中处于活动状态,您都不能将 2 个同名数据库同时附加到一个 SQL Server 实例。

I think the complaint here is that your local SQL Server Express install already has a
PatientMonitoringDatabase attached and running.

Connect to localhost with SQL Server Management Studio Express (if not installed, download here) and remove/detach the existing PatientMonitoringDatabase database. Whether it's a persistent database or only active within a running application, you can't have 2 databases with the same name at the same time attached to a SQL Server instance.

蓝眼睛不忧郁 2024-08-31 07:15:19

您收到的第二个异常是因为 SQL Server Express 实例没有打开数据库文件的权限。该文件的权限是什么?SQL Server 在什么环境下运行?

The second exception you get because the SQL Server Express instance doesn't have permission to open the database file. What are the permissions on the file, and what is SQL Server running under?

内心荒芜 2024-08-31 07:15:19

问题解决了。在那件事上很幸运。我通过浏览微软论坛得到了提示。提示引导我打开“SQL Server 配置管理器”,我必须对“SQL Server (SQLEXPRESS)”做一些事情。所以我双击它,并将“内置帐户”从“网络服务”更改为“本地系统”。现在它可以工作了......我猜我很幸运......

但是,你必须分离数据库。为此,谢谢 Nick Craver

problem solved. got lucky on that one. I got a hint by browsing microsoft forums. The hint led me to open my "SQL Server Configuration Manager", and I had to do something with "SQL Server (SQLEXPRESS)". So I double clicked on it, and changed the "Built-in Account" from "Network Service" to "Local System". and now it works.... I got lucky i guess...

but, you do have to detach the database. So for that, thx Nick Craver

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文