As someone said, usually you don't call it directly for security reasons. But If yo are just looking to test then you can do it just like you do it locally:
Write a service for retrieving an object:
public static class UserService
{
private const string server = "";
private const string database = "";
private const string username = "";
private const string password = "";
private static string connectionString => $"Data Source={server};Database={database};User Id={username};Password={password}";
public static async Task<UserDto> GetUserAsync(string username, string password, CancellationToken cancellationToken)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "SELECT * FROM MyTable WHERE Column = @value";
return await connection.QueryFirstOrDefaultAsync<UserDto>(sql).ToList();
}
}
}
Then in your button call just call the service:
var user = UserService.GetUserAsync("John Doe", "abcd1234");
Note
If you are running an app on an anroid emulator you either have to provide the actual server IP plus SQL instance name you are trying to connect to.
However, if you are trying to connect to a local database you CANNOT use localhost
or 127.0.0.1
. These simply will not work. What you CAN do however, is use the special alias port specifically for android emulators 10.0.2.2
to connect to the computer the emulator is running on.