admin管理员组

文章数量:1279217

I'm having a problem between my .NET Maui project and my SQL Server when connecting to server running my project in android phone:

A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught).

But if I run my project using 'windows machine' the code works.

Here's my code

sqlServerConnection1 = "Server=172.30.20.4;Database=HK_UDSdb;User Id=sa;Password=strongpassword;Encrypt=False;TrustServerCertificate=True;";
sqlServerConnection2 = "Server=172.30.20.4;Database=HK_UDSorderdb;User Id=sa;Password=strongpassword;Encrypt=False;TrustServerCertificate=True;";

builder1 = new SqlConnectionStringBuilder(sqlServerConnection1);
builder2 = new SqlConnectionStringBuilder(sqlServerConnection2);

Server = builder1.DataSource;
Username = builder1.UserID;
Password = builder1.Password;
Database1 = builder1.InitialCatalog;
Database2 = builder2.InitialCatalog;

public bool ServerConnection()
{
    try
    {
        bool item;
        Ping ping = new Ping();
        PingReply reply = ping.Send(Server, 1000);
        item = reply != null && reply.Status == IPStatus.Success;
        return item;
        //return true;
    }
    catch(Exception ex)
    {
        Application.Current.MainPage.DisplayAlert("Error", $"{ex.Message}", "OK");
        return false;
    }
}

In this code running in android and windows machine ping the server.

bool serverConnection = _databaseHelper.ServerConnection();
if (!serverConnection)
{
    throw new ApplicationException("Database connection failed. Unable to fetch users.");
}

try
{
    using (var connection = _databaseHelper.SQLConnection())
    {
        await connection.OpenAsync();

        string query = "SELECT user as EmpID, name, pass FROM users WHERE active = 1";

        using (var command = new SqlCommand(query, connection))
        using (var reader = await command.ExecuteReaderAsync())
        {
            while (await reader.ReadAsync())
            {
                users.Add(new UserModel
                {
                    EmpID = reader.GetString(0),
                    Name = reader.GetString(1),
                    Pass = reader.GetString(2)
                });
            }
        }
    }
}
catch (Exception ex)
{
    throw new ApplicationException($"Failed to fetch users: {ex.Message}", ex);
}

While on this code await connection.OpenAsync(); failed when running on android gets:

A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught)

My laptop and my android is connected to the same network through wifi but my android failed to finish the code.

What is the problem on my code? I already added Encrypt=False;TrustServerCertificate=True; to my connection string.

What I want is to run my project to my android phone that connects to my SQL Server without getting error.

Is there anyone can help me about that.

Thank you.

I'm having a problem between my .NET Maui project and my SQL Server when connecting to server running my project in android phone:

A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught).

But if I run my project using 'windows machine' the code works.

Here's my code

sqlServerConnection1 = "Server=172.30.20.4;Database=HK_UDSdb;User Id=sa;Password=strongpassword;Encrypt=False;TrustServerCertificate=True;";
sqlServerConnection2 = "Server=172.30.20.4;Database=HK_UDSorderdb;User Id=sa;Password=strongpassword;Encrypt=False;TrustServerCertificate=True;";

builder1 = new SqlConnectionStringBuilder(sqlServerConnection1);
builder2 = new SqlConnectionStringBuilder(sqlServerConnection2);

Server = builder1.DataSource;
Username = builder1.UserID;
Password = builder1.Password;
Database1 = builder1.InitialCatalog;
Database2 = builder2.InitialCatalog;

public bool ServerConnection()
{
    try
    {
        bool item;
        Ping ping = new Ping();
        PingReply reply = ping.Send(Server, 1000);
        item = reply != null && reply.Status == IPStatus.Success;
        return item;
        //return true;
    }
    catch(Exception ex)
    {
        Application.Current.MainPage.DisplayAlert("Error", $"{ex.Message}", "OK");
        return false;
    }
}

In this code running in android and windows machine ping the server.

bool serverConnection = _databaseHelper.ServerConnection();
if (!serverConnection)
{
    throw new ApplicationException("Database connection failed. Unable to fetch users.");
}

try
{
    using (var connection = _databaseHelper.SQLConnection())
    {
        await connection.OpenAsync();

        string query = "SELECT user as EmpID, name, pass FROM users WHERE active = 1";

        using (var command = new SqlCommand(query, connection))
        using (var reader = await command.ExecuteReaderAsync())
        {
            while (await reader.ReadAsync())
            {
                users.Add(new UserModel
                {
                    EmpID = reader.GetString(0),
                    Name = reader.GetString(1),
                    Pass = reader.GetString(2)
                });
            }
        }
    }
}
catch (Exception ex)
{
    throw new ApplicationException($"Failed to fetch users: {ex.Message}", ex);
}

While on this code await connection.OpenAsync(); failed when running on android gets:

A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught)

My laptop and my android is connected to the same network through wifi but my android failed to finish the code.

What is the problem on my code? I already added Encrypt=False;TrustServerCertificate=True; to my connection string.

What I want is to run my project to my android phone that connects to my SQL Server without getting error.

Is there anyone can help me about that.

Thank you.

Share Improve this question edited Feb 24 at 4:14 Dale K 27.5k15 gold badges58 silver badges83 bronze badges asked Feb 24 at 2:35 RaffyRaffy 297 bronze badges 7
  • 5 First, it’s a terrible idea to connect your mobile app directly to a DB server. Use a web service layer instead. However, if you must, look at the server logs to determine what the “internal exception” is – Jason Commented Feb 24 at 2:57
  • 6 Sql server 2005 is pretty old, you might not be able to use newer sqlclient libraries to connect to it what version are you using – siggemannen Commented Feb 24 at 3:35
  • 1 The reason you need to look at the server logs is: for security reasons the server never divulges the actual login failure reason to clients. If you have access to the target SQL Server's ERRORLOG then look for instances of Error 18456 Login failure, take note of the state number(s), and then compare them against those listed at MSSQLSERVER_18456 to determine the actual reason for the login failure. – AlwaysLearning Commented Feb 24 at 4:16
  • 4 Also, SQL Server 2005 went out of mainsteam support back in 2011. You're unlikely to find any currently maintained drivers that will work with it. – AlwaysLearning Commented Feb 24 at 4:17
  • 1 You definitely need to use a more modern version of SQL Server. 2005 doesn't support TLS1.2, and I suspect your Android device request TLS1.2+ (assume it's a recent device). Not to mention, already, 2005 has been end of life for many years. – Thom A Commented Feb 24 at 9:10
 |  Show 2 more comments

1 Answer 1

Reset to default 0

I Got my answer, Currently I'm using MSSQL 2022 and It's working fine. The solution was changed the MSSSQL 2005 to Higher version. 2019 to 2022 I think.

本文标签: cConnect NET Maui application to SQL Server 2005 in AndroidStack Overflow