0

Using c# .net 4.6.1 I have a program where I'm trying to query a MS SQL Server 2016 database. I have 2 remote servers, Server A and Server B. My c# program runs on Server A with no problems. When I run the program on Server B I get the following error:

System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-s pecific 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 a nd that SQL Server is configured to allow remote connections. (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)

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, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbCon nectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions , DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(Db Connection outerConnection, DbConnectionFactory connectionFactory, TaskCompletio nSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 re try) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 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-0000-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(); . 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.
  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.
  3. Even though my program runs correctly on Server A I can't ping the IP Address of the MS SQL Server 2016 database server from either Server A nor Server B.
  4. There are no firewalls on Server A or Server B.
  5. Both Server A and Server B are running 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 B I would greatly appreciate it. 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:

enter image description here

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.

Gharbad The Weak
  • 1,541
  • 1
  • 17
  • 39
  • On the machine that has the issue, load up SQL Management Studio and try and connect to the database it is failing to connect to. Can it connect to it? – mjwills Nov 29 '18 at 20:43
  • @mjwills , I just updated my OP with the error I get when I try to connect to the database from Server B using SSMS and the same credentials that work when I connect to the database using SSMS on Server A – Gharbad The Weak Nov 29 '18 at 20:51
  • If SSMS fails, this is nothing to do with C#. Voting to close, this should be on http://serverfault.com . – mjwills Nov 29 '18 at 23:13
  • @mjwills Thanks for the suggestion, I didn't know about serverfault.com . I'll put it up over there. – Gharbad The Weak Nov 30 '18 at 02:39
  • Does this answer your question? [System.Data.SqlClient.SqlException: Login failed for user](https://stackoverflow.com/questions/22415319/system-data-sqlclient-sqlexception-login-failed-for-user) – Michael Freidgeim Aug 18 '21 at 07:51

2 Answers2

0

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,

Monycell
  • 21
  • 2
  • are you referring to a connection string that is stored on the server I'm trying to connect from, in this case Server A and Server B? I've never heard of saving a connection string on the server before. I checked and neither Server A nor Server B have an ODBC data source configured on them that is being used to connect to the database. As far as the connection string I'm defining in the c# program, I'm not changing anything in the program when I try to run it on Server A or Server B. I get the error using the exact same version of the program when I run it on Server B. – Gharbad The Weak Nov 29 '18 at 20:53
  • You can easily check connection string server name by opening the sql server database and checking the first highlighted name. So, check if the servers A and B are bearing the same name in the sql server database. – Monycell Nov 29 '18 at 21:40
  • sorry, I'm not following what you are saying. I don't change the connection string that is stored in my c# program, it stays the same. Using the connection string works on Server A but doesn't work on Server B. Are you saying that the connection string I would use in my c# program should be different on Server B from what runs successfully on Server A? Or are you saying there is some configuration in the database that is allowing Server A to connect successfully when the program runs but is not letting Server B connect when it runs the same program? – Gharbad The Weak Nov 29 '18 at 21:52
  • If to say I can add picture I would have explained it better. The connection string on server A maybe different from server B... Just open the sql server database on both Servers and watch the highlighted name, if they are the same then the connection string should work on both Servers, but if not, then that's the problem. – Monycell Dec 01 '18 at 00:16
0

Try this:

  • Right click on cmd
  • chose run as administrator
  • then type in the command prompt:

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.

This works for me but I'm not secure that's be your problem

Lemon
  • 148
  • 10
  • what server are you saying I should run this on? Server A, Server B or the server where the database is housed? What exactly is this command supposed to do? – Gharbad The Weak Nov 30 '18 at 02:38