将 C# 可执行文件连接到远程计算机上的数据库文件时遇到问题
我使用 VC# 创建了一个员工管理应用程序,首次运行时,预计会向用户查询驻留在远程计算机上的 (.mdf) 数据库的路径。生成的路径可能类似于
string dbPath = @"P:\remoteComputer\public\StaffTool\ExamplePersonnelDatabase.mdf";
然后我将此字符串放入连接字符串模板中,如下所示:
string dbConnectTemplate = @"Data Source=.\SQLEXPRESS;AttachDbFilename={0};Integrated Security=True;Connect Timeout=30;User Instance=True";
string dbConnectionString = String.Format(dbConnectionTemplate,dbPath);
然后我尝试连接到数据库 LINQ to SQL 样式
ManagementDBDataContext db = new ManagementDBDataContext(
dbConnectionString);
此时,会弹出一个错误,指出
文件“P:\remoteComputer\public\StaffTool\ExamplePersonnelDatabase.mdf”所在的网络路径不支持数据库文件。
尝试为文件 P:\remoteComputer\public\StaffTool\ExamplePersonnelDatabase.mdf 附加自动命名数据库失败。存在同名数据库,或无法打开指定文件,或位于 UNC 共享上。
由于我对数据库比较陌生,我完全不理解这个消息。显然,该文件不在 UNC 共享上。
有什么想法吗?
Jim Lamb 建议我连接到远程运行的 SQL Server 实例。考虑到我正在使用 LINQ to SQL,我需要进行哪些重构才能实现这一点?其他想法仍然受欢迎 - 特别是“按下这个按钮,一切都会起作用”的解决方案。
另一个线索:一位同事说,曾经有某种方法可以通过“控制面板”->“管理工具”->“数据源(ODBC)”进行工作,以便可以从我的计算机上查看远程数据库,就像在本地一样。除此之外,该同事不知道更多细节。
Using VC# I've created a staff management app that, upon its first run, is expected to query the user for the path to a (.mdf) database which will reside on a remote computer. A resulting path may be something like
string dbPath = @"P:\remoteComputer\public\StaffTool\ExamplePersonnelDatabase.mdf";
Then I'm placing this string into a connection string template as so:
string dbConnectTemplate = @"Data Source=.\SQLEXPRESS;AttachDbFilename={0};Integrated Security=True;Connect Timeout=30;User Instance=True";
string dbConnectionString = String.Format(dbConnectionTemplate,dbPath);
Then I'm attempting to connect to the database LINQ to SQL style
ManagementDBDataContext db = new ManagementDBDataContext(
dbConnectionString);
At this point, an error pop's up stating that
The file "P:\remoteComputer\public\StaffTool\ExamplePersonnelDatabase.mdf" is on a network path that is not supported for database files.
An attempt to attach an auto-named database for file P:\remoteComputer\public\StaffTool\ExamplePersonnelDatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
As I am relatively new to databases, I completely do not understand this message. And obviously, the file is not on a UNC share.
Any ideas?
Jim Lamb recommended that I connect to an instance of SQL server running remotely. Considering that I'm using LINQ to SQL, what refactoring do I have to do to make this happen? Other ideas still welcome - especially "push this button and everything will work" solutions.
Another clue: A coworker said that there used to be some way to work through Control Panel->Administrative Tools->Data Sources(ODBC) so that a remote database could be viewed from my computer as if it was local. The coworker didn't know any more details besides this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试通过网络连接连接到另一台计算机上的数据库文件,SQL Express 不支持该连接。您需要制作本地副本并附加到该副本,或者连接到与 MDF 文件在同一台计算机上运行的 SQL 实例。
You are attempting to connect to a database file on another machine over a network connection, which isn't supported by SQL Express. You'll need to make a local copy and attach to that, or connect to an instance of SQL that's running on the same machine as the MDF file.