连接到C#中的数据库 [英] connecting to data base in c#

查看:100
本文介绍了连接到C#中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此连接连接到我的.sdf数据库:
数据源= c:\\ Database1.sdf"

但是当我想通过"connection.open"打开连接时,会引发此异常:

建立与SQL Server的连接时发生了与网络有关或特定于实例的错误.找不到服务器或无法访问该服务器.请验证实例名称正确并且将SQL Server配置为允许远程连接.(提供者: SQL网络接口,错误:26-错误定位服务器/实例指定).

那我就无法连接到数据库.
为了解决这个问题,我用了:
带有SqlCeConnection的SqlConnection
带有SqlCeCommand的SqlCommand

现在我需要提供对System.Data.SqlServerCe的引用(在system.data.sqlserverce.dll中)
而且我不知道怎么做
请帮助我

I use this connection to connect to my .sdf database:
"Data Source = c:\\Database1.sdf"

but when i want to open the connection by "connection.open", it throws this exception:

"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)".

then I can''t connect to the database.
for solving this problem i used:
SqlConnection with SqlCeConnection
SqlCommand with SqlCeCommand

now i will need to provide a reference to System.Data.SqlServerCe (in system.data.sqlserverce.dll)
and i donot know to how do it
please help me

推荐答案

您必须将连接字符串声明为以下内容:
http://www.connectionstrings.com/sql-server-2005-ce [ ^ ]

You must declare your connectionstring to the following:
http://www.connectionstrings.com/sql-server-2005-ce[^]

ProviderName="System.Data.SqlServerCe.SqlCeConnection"



希望对您有所帮助.



Hope it helps.


首先要确保SQL Server服务正在运行.其他原因可能是错误的连接字符串或某些防火墙阻塞.

在这里,请查看此博客文章以解决问题:异常-错误26-错误定位了指定的服务器实例 [ ^ ]
提供程序:SQL网络接口,错误:26-错误定位服务器/实例... [ ^ ]
未找到指定的SQL服务器" o .. [^ ]
First and foremost make sure that the SQL Server services are running. Other reasons could be, incorrect connection string or some Firewall blockage.

Here, have a look at this blog post to troubleshoot the issue : MSDN Blogs: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified[^]

If needed, look at these one:
Exception - error 26 - Error Locating Server Instance Specified[^]
provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance...[^]
"Specified SQL server not found" o..[^]


在您的服务集合中启动SQL Server服务.(右键单击我的电脑,选择服务和应用程序,然后选择服务,您将获得所有服务列表.在这里选择SQL Server(SQLEXPRESS)服务,右键单击并选择开始)
Start the SQL server service in ur service collection.(right click on the my computer,select service and applications then services,there u will get all service list. there select the SQL Server(SQLEXPRESS)service,right click and select start)


这篇关于连接到C#中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆