SQL精简版,无法连接到本地sdf文件
我的本地目录 C:\myDatabase.sdf 中有一个 .sdf 文件。
在 Visual Studio 2010 asp.NET 中,我添加了表和值等。我现在有一个页面,需要在默认情况下连接到本地数据库。 aspx.cs 页面。
我的代码是:
SqlConnection conn = new SqlConnection("Data Source=C:\\myDatabase.sdf;");
conn.Open();
代码在尝试运行 conn.Open(); 时失败;调试器说无法连接。错误如下:
“建立与 SQL Server 的连接时发生网络相关或特定于实例的错误。找不到服务器或无法访问服务器。请验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供者:SQL 网络接口,错误:26 - 指定服务器/实例错误)”
现在在连接字符串中,我几乎尝试了一切!我尝试添加数据库名称、用户名、密码、server=localhost、mode=exclusive 等等。它仍然不起作用...我不明白。我不认为我没有安装 SQL Server。我只有 Visual Studio 2010 和 sdf 文件。
我认为它正在尝试连接到服务器...但数据库文件是本地的?我不明白。 “server=localhost”不起作用。我不知道如何检查文件是否允许远程连接,因为我不知道如何在 Visual Studio 之外访问该文件!
有人可以告诉我发生了什么事吗!
I have a .sdf file in my local directory C:\myDatabase.sdf
In Visual Studio 2010 asp.NET, I added the tables and values etc. I now have a page where I need to connect to the local database on the Default.aspx.cs page.
the code I have is:
SqlConnection conn = new SqlConnection("Data Source=C:\\myDatabase.sdf;");
conn.Open();
The code fails when it tries to run the conn.Open(); The debugger says unable to connect. Heres the error:
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
Now in the connection string, ive tried almost EVERYTHING! ive tried adding the database name, username, password, server=localhost, mode=exclusive, and many more. it still doesnt work... i cant figure it out. i dont have SQL Server installed i dont think. i just have visual studio 2010 and an sdf file.
I think its trying to connect to a server... but the database file is local? i dont understand. the "server=localhost" didnt work. I dont know how to check wheather the file allows remote connections or not because i dont know how to access the file other than in visual studio!
Can someone please tell me what's going on!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试使用 SqlCeConnection 而不是 SqlConnection。
http://msdn。 microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection(v=VS.100).aspx
Please try with an SqlCeConnection rather than with an SqlConnection.
http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection(v=VS.100).aspx
您使用了错误的 API。 SqlConnection 是 System.Data.SqlClient 的一部分,用于访问运行 SQL Server 的数据库。要访问 Sql Server Compact 数据库,您必须使用 System.Data.SqlServerCe。您可能必须引用 dll 才能访问此命名空间。
You are using the wrong api. SqlConnection is part of System.Data.SqlClient which is used to access databases running SQL Server. To access a Sql Server Compact db you have to use System.Data.SqlServerCe. You might have to reference a dll to get access to this namespace.