简体   繁体   English

c#System.Data.SqlClient.SqlException(0x80131904)

[英]c# System.Data.SqlClient.SqlException (0x80131904)

Using c# .net 4.6.1 I have a program where I'm trying to query a MS SQL Server 2016 database. 使用c#.net 4.6.1,我有一个程序正在尝试查询MS SQL Server 2016数据库。 I have 2 remote servers, Server A and Server B. My c# program runs on Server A with no problems. 我有2个远程服务器,服务器A和服务器B。我的c#程序在服务器A上运行没有问题。 When I run the program on Server BI get the following error: 当我在Server BI上运行程序时,出现以下错误:

System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-s pecific error occurred while establishing a connection to SQL Server. System.Data.SqlClient.SqlException(0x80131904):建立与SQL Server的连接时发生了与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct a nd that SQL Server is configured to allow remote connections. 验证实例名称是否正确以及将SQL Server配置为允许远程连接。 (provider: Named P ipes Provider, error: 40 - Could not open a connection to SQL Server) ---> Syste m.ComponentModel.Win32Exception (0x80004005): Access is denied at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdent ity identity, SqlConnectionString connectionOptions, SqlCredential credential, O bject providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData r econnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTra nsientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOp tions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConn ectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) (提供程序:命名管道提供程序,错误:40-无法打开与SQL Server的连接)---> Syste m.ComponentModel.Win32Exception(0x80004005):在System.Data.SqlClient.SqlInternalConnectionTds..ctor( DbConnectionPoolIdentity身份,SqlConnectionString connectionOptions,SqlCredential凭据,对象providerInfo,String newPassword,SecureString newSecurePassword,布尔型redirectedUserInstance,SqlConnectionString userConnectionOptions,SessionData r econnectSessionData,DbConnectionPool池,字符串accessToken,Boolean applyTra nsProviderProvider SqlManager中的ProviderProvider。 SqlConnectionFactory.CreateConnection(DbConnectionOp tions选项,DbConnectionPoolKey poolKey,对象poolGroupProviderInfo,DbConn ePool池,DbConnection owningConnection,DbConnectionOptions userOptions)

at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConn ectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConne ctionPoolKey poolKey, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owning Object, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection o wningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection ) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow ningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean o nlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& co nnection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow ningObject, TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbCon nectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource 在System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection拥有对象,DbConnectionOptions在userInternals,在DbConnectionowningObject(DbConnection拥有池,DbConnectionOptions用户选项,在DbConnectionOptions用户选项,在DbConnectionOptions用户选项, System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow ningObject,UInt32 waitForMultipleObjectsTimeout,布尔值allowCreate,布尔值,nlyOneCheckConnection,内部,D.在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(nb),在System.Data.ProviderBase.DbCo处的DbConnection ow ningObject,TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbCon nectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbCon nectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource 1 retry, DbConnectionOptions userOptions , DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(Db Connection outerConnection, DbConnectionFactory connectionFactory, TaskCompletio nSource 1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 re try) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1 retry) at System.Data.SqlClient.SqlConnection.Open() at YetAnotherSmallConsoleApp.Program.Main(String[] args) in C:\\Repository\\Pro jects\\YetAnotherSmallConsoleApp\\YetAnotherSmallConsoleApp\\Program.cs:line 28 ClientConnectionId:00000000-0000-00 1 retry, DbConnectionOptions userOptions, DbCon nectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource重试1 retry, DbConnectionOptions userOptions, DbCon nectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource 1,DbConnectionOptions userOptions,DbConnectionInternal oldConnection,DbConnectionInternal&connection)位于System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(Db Connection externalConnection,DbConnectionFactory connectionFactory,TaskComplets nSource 1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1重试,DbConnectionOptions userOptions在System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 re try) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1重试) )在System.Data.SqlClient.SqlConnection.Open()在YetAnotherSmallConsoleApp.Program.Main(String [] args)在C:\\ Repository \\ Projects \\ YetAnotherSmallConsoleApp \\ YetAnotherSmallConsoleApp \\ Program.cs:line 28 ClientConnectionId:00000000-0000- 00 00-0000-000000000000 00-0000-000000000000

Here is my program: 这是我的程序:

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;

namespace YetAnotherSmallConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection con = new SqlConnection("Server = IP_ADDRESS; User Id = USER_NAME; Password = PASSWORD;");
            string[] arrExcludeLines = File.ReadAllLines(FILEPATH_TO_QUERY);
            string strExcludeQuery = default(string);
            foreach (string line in arrExcludeLines)
            {
                strExcludeQuery = strExcludeQuery + line + Environment.NewLine;
            }
            try
            {
                using (con)
                {
                    con.Open();
                    SqlCommand cmdExclude = new SqlCommand(strExcludeQuery, con);
                    SqlDataReader dr1 = cmdExclude.ExecuteReader();
                    while (dr1.HasRows)
                    {
                        while (dr1.Read())
                        {
                            Console.WriteLine(dr1[0].ToString());
                            Console.ReadLine();
                        }
                        dr1.NextResult();
                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }
        }
    }
}

The error is thrown from the line that reads con.Open(); 从读取con.Open();的行引发错误con.Open(); . Here's a few facts about the servers: 以下是有关服务器的一些事实:

  1. Both Server A and Server B are on the same network, and this network is not same network my MS SQL Server 2016 database is on. 服务器A和服务器B都在同一网络上,并且该网络不是我的MS SQL Server 2016数据库所在的网络。
  2. Server A has an instance of MS SQL Server 2012 installed on it, Server B does not have MS SQL Server installed on it. 服务器A上已安装MS SQL Server 2012实例,服务器B上未安装MS SQL Server。
  3. Even though my program runs correctly on Server AI can't ping the IP Address of the MS SQL Server 2016 database server from either Server A nor Server B. 即使我的程序在Server AI上正常运行,也无法从Server A或Server B ping MS SQL Server 2016数据库服务器的IP地址。
  4. There are no firewalls on Server A or Server B. 服务器A或服务器B上没有防火墙。
  5. Both Server A and Server B are running MS Server 2012 R2. 服务器A和服务器B都在运行MS Server 2012 R2。

These are the only things I can think of to compare between Server A and Server B. Obviously there is something different between Server A and Server B that is allowing the program to run successfully on Server A. I don't know if there is something configured differently on Server A that is configured differently on Server B or if there is something configured on my MS SQL Server 2016 database server that allows Server A to connect to the database successfully when the program is ran but prevents the program from connecting to the database and running successfully on Server B. If someone could give me any suggestions on how to troubleshoot this to be able to get the program to run on Server BI would greatly appreciate it. 这些是我可以想到的唯一在服务器A和服务器B之间进行比较的事情。显然,服务器A和服务器B之间存在一些差异,它允许程序在服务器A上成功运行。我不知道是否有东西在服务器A上配置了不同的服务器A,在服务器B上配置了不同的配置,或者在我的MS SQL Server 2016数据库服务器上配置了某些配置,该配置允许服务器A在程序运行时成功连接到数据库,但阻止了程序连接到数据库并且可以在Server B上成功运行。如果有人可以给我有关如何解决此问题的任何建议,以便能够使程序在Server BI上运行,将不胜感激。 Thanks in advance. 提前致谢。

EDIT: when I try to connect to the MS SQL Server 2016 database from Server B using the same credentials I use to connect to the database from Server A using SSMS I get the following error: 编辑:当我尝试使用相同的凭据从服务器B连接到Server B的MS SQL Server 2016数据库时,遇到了以下错误:

在此处输入图片说明

EDIT: one other thing: I've tried to run the program with the computer name for the MS SQL Server 2016 database server in the connection string as well as the actual IP address for the database server in the connection string, both ways result in being able to run the program successfully on Server A but I get the error on Server B. 编辑:另一件事:我试图使用连接字符串中的MS SQL Server 2016数据库服务器的计算机名称以及连接字符串中的数据库服务器的实际IP地址运行程序,两种方式都导致能够在服务器A上成功运行程序,但在服务器B上却收到错误消息。

Try this: 尝试这个:

  • Right click on cmd 右键单击cmd
  • chose run as administrator 选择以管理员身份运行
  • then type in the command prompt: 然后在命令提示符下键入:

mofcomp "%programfiles(x86)%\\Microsoft SQL Server\\140\\Shared\\sqlmgmproviderxpsp2up.mof" mofcomp“%programfiles(x86)%\\ Microsoft SQL Server \\ 140 \\ Shared \\ sqlmgmproviderxpsp2up.mof”

140 is the version of your sql server, change this for the correct version you are trying to use. 140是您的sql server的版本,请将其更改为您尝试使用的正确版本。

This works for me but I'm not secure that's be your problem 这对我有用,但我不确定这是您的问题

This type of error must surely be resulting from a wrong connection string. 这种类型的错误必定是由错误的连接字符串引起的。 Confirm to make sure the connection strings are the same for the servers, 确认以确保服务器的连接字符串相同,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于C#服务的数据库错误System.Data.SqlClient.SqlException(0x80131904) - C# Service Based Database error System.Data.SqlClient.SqlException (0x80131904) SqlException:System.Data.SqlClient.SqlException(0x80131904) - SqlException: System.Data.SqlClient.SqlException (0x80131904) MVC 应用程序 System.Data.SqlClient.SqlException (0x80131904): - MVC App System.Data.SqlClient.SqlException (0x80131904): System.Data.SqlClient.SqlException (0x80131904)) 从字符到字符串的转换数据或时间 - System.Data.SqlClient.SqlException (0x80131904)) conversion data or time from character to string 错误:System.Data.SqlClient.SqlException(0x80131904):将数据类型nvarchar转换为数值时出错 - Error:System.Data.SqlClient.SqlException (0x80131904): Error converting data type nvarchar to numeric System.Data.SqlClient.SqlException (0x80131904): '@med_data' 附近的语法不正确 - System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '@med_data' 不规则:SQL连接错误:System.Data.SqlClient.SqlException(0x80131904) - Irregular: SQL Connection Error: System.Data.SqlClient.SqlException (0x80131904) 包失败,因为 System.Data.SqlClient.SqlException (0x80131904): 's' 附近的语法不正确 - Package fails because System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near 's' 尝试将数据发送到数据库时出现System.Data.SqlClient.SqlException(0x80131904) - System.Data.SqlClient.SqlException (0x80131904) when trying to send data to Database “重置连接”是什么意思? System.Data.SqlClient.SqlException (0x80131904) - What does “Resetting the connection” mean? System.Data.SqlClient.SqlException (0x80131904)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM