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 | Show 2 more comments1 Answer
Reset to default 0I 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
版权声明:本文标题:c# - Connect .NET Maui application to SQL Server 2005 in Android - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741296834a2370865.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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