vs2008自带数据库连接不上,C#

发布时间:2024-05-15 09:07 发布:上海旅游网

问题描述:

我用的vs自带的数据库,代码是:
string source = "Data Source=./SQLEXPRESS;Initial Catalog=userdatabase;Integrated Security=True";
SqlConnection conn = new SqlConnection(source);
conn.Open();
//userdatabase是我建的数据库名

显示错误:
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

是不是source的字符串不对?还是远程登录问题?
怎么设置?

问题解答:

把SQLEXPRESS去掉看看,我之前也是连不上,把SQLEXPRESS去掉就好了,要不行你试试我下面的语句看看

String connectionString = @"Server=(local);Integrated Security=True;Database=userdatabase";
SqlConnection thisConnection = new SqlConnection(connectionString);

40 错误是最典型的错误, SQL 服务没有启动
或者是你的连接配置不正确, 系统默认实例, 可以直接连接
安装的时候可以安装多实例, 这时候连接需要指定实例名称

请查看系统服务
SQL 2000 的服务是以 MSSQL$ 开头
SQL 2005 的服务是以 SQL Server 开头

例如: SQL Server (实例名)
默认实例 192.168.1.2
指定实例 192.168.1.2\实例名

string source = @"Data Source=./SQLEXPRESS;Initial Catalog=userdatabase;Integrated Security=True";
掉了一个转义符@

你的数据库是sqlserver2005么

热点新闻