无法建立 SQL 连接
我的 SQL 连接代码遇到问题 -
SqlConnection con = new SqlConnection("Data Source=D:\\MyDocuments\\Desktop\\WorkHours\\WorkHours\\App_Data\\Database1.sdf;" + "Trusted_Connection=true;");
con.Open();
我只浏览了这两行,只是试图让连接继续运行,但我收到了此错误:
建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。找不到服务器或无法访问服务器。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供程序:SQL 网络接口,错误:26 - 定位指定的服务器/实例时出错)
我 100% 确定我的数据库 URL 是正确的。帮助将不胜感激
Having trouble with my SQL connection code -
SqlConnection con = new SqlConnection("Data Source=D:\\MyDocuments\\Desktop\\WorkHours\\WorkHours\\App_Data\\Database1.sdf;" + "Trusted_Connection=true;");
con.Open();
Im only Going through those 2 lines, just trying to get the connection going and im getting this 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)
i am 100% certain my database URL is correct. Help will be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(2)
http://www.connectionstrings.com/ 是一个很棒的网站,我很抱歉忘记了正确的连接字符串语法也是如此。
http://www.connectionstrings.com/sql-server-2008
如果您使用紧凑版 -
http://www.connectionstrings.com/sql-server-2005-ce
Data Source =MyData.sdf; Persist Security Info =False;
或...
Data Source =" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\MyData.sdf; Persist Security Info =False;
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您使用了错误的连接类型/提供商。
您的数据库文件具有 *.sdf 扩展名,这意味着您使用的是 Sql Server Compact Edition,而 Compact Edition 需要
System.Data.SqlCe
命名空间和SqlCeConnection
对象。修复此问题后,您的代码仍然存在两个问题:
You are using the wrong connection type/provider.
Your database file has a *.sdf extension, which means you are using Sql Server Compact Edition, and Compact Edition needs the
System.Data.SqlCe
namespace andSqlCeConnection
object.Once you fix that, you still have two issues with your code: