Background
My C# application is a Windows service which allows a user to configure SQL queries that involve linked servers to move data around similar to an ETL (Extract Transform Load) application. The queries have been tested directly in SSMS and work without any problems.
Below is an example query inserting data from the LocalDB database on the local server to the RemoteDB database on the RemoteServer. The RemoteServer configuration is listed with it:
EXEC master.dbo.sp_addlinkedserver @server = N'RemoteServer', @srvproduct=N'SQL Server'
INSERT INTO RemoteServer.RemoteDB.dbo.tst_Address (Address, FirstName, LastName, DateOfBirth)
SELECT t1.Address, t1.FirstName, t1.LastName, t1.DateOfBirth
FROM LocalDB.dbo.tst_Address t1
LEFT JOIN RemoteServer.RemoteDB.dbo.tst_Address t2 ON t2.Address = t1.Address
WHERE t2.Address IS NULL
Problem
When executed directly from SSMS there is no issue, but when executed from the c# application using SqlCommand.ExecuteNonQuery() I find various errors depending on which computer I am running the application from. For example, one exception I have on hand is
System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out
Another computer gives:
Named Pipes Provider: Could not open a connection to SQL Server [5]. OLE DB provider "SQLNCLI11" for linked server "RemoteServer" returned message "Login timeout expired". OLE DB provider "SQLNCLI11" for linked server "RemoteServer" returned message "A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.".
Named pipes is enabled and the timeouts are occurring in about 5-10 seconds.
The login is a windows user that is in the sysadmin role on both servers, but I have also tried setting an SQL user via sp_addlinkedsrvlogin but it makes no difference to the exceptions thrown.
I cannot see why there should be any difference as I expect SqlCommand to be sending the SQL to the SQL Server service to process. What am I missing?




sauser. You need to usesp_addlinkedsrvloginto add a user mapping for the SQL Login that your C# application uses.SELECT suser_name() as [LOGIN], user_name() as [USER]) and executing it via the linked server (EXEC RemoteServer.RemoteDB.dbo.sp_getlogininfo). For the local server the login and user are confirmed to be the same so I don't think this is a login issue.