连接SQL Server文件集锦

在新建SqlConnection对象,连接资料库时连接失败,出现“常规网络错误。请检查您的网络文档。”

DataSource直接写IP的话,通信协议是按照TCP/IP协议连接的,如果服务器端TCP/IP服务没有开启,则会报错“不允许远程连接”

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)

DataSource 不能写出local 或者127.0.0.1 直接写电脑名字,这个和SQL 服务器端配置有关,localhost和127.0.0.1是等价的

SQL Server服务器没开也会发生不允许远程连接这个错误信息~~~

SQL2005的配置在   Configuration   Tools   目录里

<!--

Server=(local);Database=DotNetNuke;Trusted_Connection=True
--Uses   named   pipes
Server=localhost;Database=DotNetNuke;Trusted_Connection=True
--Uses   a   TCP   port   negotiated   on   port   1434   udp,   which   defaults   to   1433

-->

3ZQ%MOO42L@EC4RYCZ1N%]I

用户 'sa' 登录失败。原因: 未与信任 SQL Server 连接相关联

解决办法如下:1.首先要确认你的sql server 2005 “服务器身份验证”为 “sql server 和windows 身份验证模式(s)”如图:

UT~(3%B(H[~LM()3~MVKD}2

2.要保证你的sa帐号启用,默认是禁用的方法如下:

在对象资源管理器点击服务器名称 点击安全性 点击登录名 如图:

MFR$MS2Q9T74XIL2T_8V@3D

这些都设置好了之后,SQL服务器要重启的,问题即可解决!

连接字符串的两种方式:

Windows验证:

Data Source=ITWX-03;Initial Catalog=Chat;Integrated Security=True

Integrated Security这个必须要设为True,默认是false(Login failed for user ''. The user is not associated with a trusted SQL Server connection.)

混合验证:

Data Source=ITWX-03;Initial Catalog=Chat;User ID=sa;Password=’xxxxxx’

Integrated Security(集成安全)/Trusted Connection(受信连接):表示Windows认证是否被用来连接数据库。它可以被设置成真、伪或者是和真对等的SSPI,其缺省值为伪。

如果Integrated Security=’SSPI’,则会发生下面的错误信息,原因是SQL登录用户里面没有域账号ITWX-03\ASPNET,添加进去就ok了。

SSPI虽然和true是对等的,但有什么区别吗?暂时不知道。如果设为true则不会有下面的错误信息了。

HFR8C%7MD(BT{9]_JN8_R0W

原文地址:https://www.cnblogs.com/zqstc/p/1696568.html