0

I am querying a SQL Server database with the following C# code:

SqlConnectionStringBuilder sqlCSB = new SqlConnectionStringBuilder();
sqlCSB["Data Source"] = @"SERVER\MSSQL";
sqlCSB.Remove("User ID");
sqlCSB.Remove("Password");
sqlCSB["integrated Security"] = true;
sqlCSB["Initial Catalog"] = "InitCatalog";

using (SqlConnection connection = new SqlConnection(sqlCSB.ConnectionString))
{
    using (SqlCommand cmd = connection.CreateCommand())
    {
        cmd.CommandText = "SELECT * FROM public.v_Data;";
        MessageBox.Show(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
        // this shows the correct (same) user regardless from which path (remote or local) the App is running from
        connection.Open();

        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            int ord = reader.GetOrdinal("ColumnA");

            while (reader.Read())
            {
               returnedData.Add(reader.GetString(ord).Substring(2));
            }
         }
      }
   }

   return returnedData;
}

When the app is started from a local path (e.g. C:\myapp.exe), everything works just fine, but when I try to run it from e.g. Netdrive (\\\filer) (Z:), I get the following error:

System.Data.SqlClient.SqlException (0x80131904): (provider: SQL Network Interfaces, error: 26)

at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at Main.refresh() in Main.cs:Line 397. // connection.Open();

ClientConnectionId:00000000-0000-0000-0000-000000000000 (Error Number):-1,(State):0,(Class):20

What's the root cause? I'm not sure if some interaction between "integrated security" and starting the app from a network directory is causing the error.

As stated above it's always the same Windows user and the account has access to the network drive.

What's the best approach for debugging here?

6
  • The following may be helpful: stackoverflow.com/questions/71198996/… Commented Jul 1, 2022 at 20:38
  • Not sure if it matters or not, but it looks like you have a lower-case i for Integrated Security. Commented Jul 1, 2022 at 20:42
  • 1
    Are you running it directly from the UNC share path, or from a mapped network drive? If the former then perhaps it's not finding its .config file which could be introducing a multitude of issues. Commented Jul 2, 2022 at 0:25
  • @user9938 thanks for the reply but there is no issue on the server side because the query / connection works if the app is started after it's copied to a local path Commented Jul 4, 2022 at 9:42
  • @user9938 thanks for the reply but the code works as stated in my other reply Commented Jul 4, 2022 at 9:44

1 Answer 1

0

The answer to my question can be found here: SqlConnection Error if EXE is executed from network path

"Finally I found the problem: in the server with the shared folder, SMBv2 is disabled (I don't know why) so only SMBv1 is active; the same program executed from the same client in the same network but located on a server with SMBv2 enabled works fine. So the problem is about SMBv1 share, deprecated starting from Windows 10 1803"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.