通过TCP / IP连接到SQL服务器,而不是NP

时间:2014-10-07 22:19:47

标签: c# sql-server sql-server-2008 ado.net connection-string

正如我发现的那样,我的SQL服务器已禁用命名管道,但启用了TCP / IP(并且它将保持这种状态)。现在我正在尝试从visual studio连接到数据库,但没有运气。 到目前为止我已经尝试过了:

string connstring = "Data Source=192.168.1.1:1433 ;Initial Catalog=np-sparcsn4-custom;Persist Security Info=True;User ID=xxxx;Password=/*****/";

string connstring = "Data Source=192.168.1.1:1433 ; Network Library=DBMSSOCN; Initial Catalog=np-sparcsn4-custom;Persist Security Info=True;User ID=xxxx;Password=/*****/";

我试图用名字替换IP地址 - 没有运气。当我尝试使用表适配器进行连接时,我可以预览数据,如果我使用ie网格也是如此 - 但在代码中我只是无法打开它的连接:我总是有网络相关或服务器特定的错误,通常是

Named Pipes Provider: Could not open a connection to SQL Server [5] 

provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server

服务器设置为接受远程连接,并且可以访问。

更新

我已经尝试了

string connstring = "Server=tcp:192.168.1.1,1433 ;Initial Catalog=np-sparcsn4-custom;Persist Security Info=True;User ID=xxx;Password=/****/";

"Server=tcp:ponln4report,1433 ....

我错了:

provider: TCP Provider, error: 0 - A non-recoverable error occurred during a database lookup**strong text**

enter image description here

enter image description here

EDI2:我发现了一台旧的SQL Server 2005,我可以毫无问题地连接它。这有点痛苦,因为它缺少日期和时间数据类型,但我想现在必须这样做。

2 个答案:

答案 0 :(得分:1)

对于C#,请使用此连接字符串

connectionString="Data Source=192.168.1.1,1433;Initial Catalog=np-sparcsn4-custom;Integrated Security=false;User ID=your_username;Password=your_password"

如果您需要修改web.config文件,请添加此节点:

<connectionStrings>
        <remove name="LocalSqlServer" />
        <add name="LocalSqlServer" connectionString="Data Source=192.168.1.1,1433;Initial Catalog=np-sparcsn4-custom;Integrated Security=false;User ID=your_username;Password=your_password" providerName="System.Data.SqlClient" />
    </connectionStrings>

答案 1 :(得分:0)

确保在SQL Configuration Manager -> Protocols中为 IPALL 设置端口1433 screenshot