0

I'm need some guidance on how to connect .NET MAUI with my local SQL Server database. I've been using C# WinForms and SQL Server for a while now, but I'm not sure how to proceed with .NET MAUI.

My goal is to perform some CRUD operations from an Android emulator to my local SQL Server database. I'd really appreciate any advice on how to achieve this. If I need to write an API for this scenario, I'd love some help with that as well.

Thank you in advance for your help!

I have tried connect the SQL Server database directly using Microsoft.Data.SqlClient; but it works only on windows machine, but not in android emulator.

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • 4
    You should never connect directly to a db server from a mobile client. Use a web service as an intermediate layer to protect your db. That said, please search before posting. This topic has been discussed many times – Jason May 15 '23 at 01:24
  • 1
    @Jason Never is a strong word :) But in the general case I 100% agree. – ProgrammingLlama May 15 '23 at 01:25
  • 1
    The word "local" has different meaning when speaking in the context of android emulators. The deal is that your windows machine itself has the server, and your android is machine inside that machine. If you posted some code... Anyway, what Jason told you is right. If you are not writing server browser app, this is serious mistake. Make new ASP WEB API, and edit the Weather Forecast code to get data from sql server. As a starting point... – H.A.H. May 15 '23 at 06:20

1 Answers1

0

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.

CorrieJanse
  • 2,374
  • 1
  • 6
  • 23
  • Probably won't work - one has to specify to use TCP and may need to configure one's firewall. See the following: https://stackoverflow.com/a/71199793/10024425 – Tu deschizi eu inchid May 15 '23 at 03:10
  • Isn't there some additional setup step needed, so that the Android emulator (which is on a virtual machine) can reach the PC? – ToolmakerSteve May 15 '23 at 21:59
  • @ToolmakerSteve yes and no. If the server name is correct nothing more needs to be done. But when you connect locally yo ucannot use `localhost` or `127.0.0.1` You have to use a special alias IP for local connections, which is `10.0.2.2` – CorrieJanse May 17 '23 at 23:49
  • @Tudeschizieuinchid `SqlConnection` is an TCP/IP connection. As mentioned on the other comment and updated post, you should be able to connect to any database with just that if the IP is correct - but have to use `10.0.2.2` instead of `localhost` which is a special alias port for android emulators. – CorrieJanse May 17 '23 at 23:52