2

I'm sorry,my English is not good.

How to connect SQL Server with .Net Maui? I tried so many ways in the internet,but all fail.

I tried to import Ado.net,but it shows errors. The error shows "The project's target framework does not contain Entity Framework runtime assembles"

SpiderEgg
  • 31
  • 1
  • 4
  • 2
    It is a really bad idea to connect directly to a db server from a mobile app – Jason Dec 13 '22 at 00:28
  • If you could import `Ado.net`, then you would have the needed functionality. HOWEVER, that might need to be done for each platform, because the drivers are platform-specific. Lets start with Ado.Net problem. **"it shows errors."** Add to question the errors. (Paste or type text; not as an image; people need to be able to search the text of the error.) (If the errors are not in English, please use a translator to convert them to English.) – ToolmakerSteve Dec 13 '22 at 04:13
  • Thanks @Jason , I know it's a bad idea. So I want to know how to use. – SpiderEgg Dec 13 '22 at 06:07
  • Thanks @ToolmakerSteve , I add question the errors on it. – SpiderEgg Dec 13 '22 at 06:07
  • **Googling that error message,** I see [Adding ADO.Net Entity Framework gives ...](https://stackoverflow.com/q/70580916/199364). Try answer there. Or look at other links listed when google that error message. – ToolmakerSteve Dec 13 '22 at 06:16
  • Thanks @ToolmakerSteve, but unfortunately It's all not what I need. – SpiderEgg Dec 13 '22 at 06:59

3 Answers3

1

As jason said in a comment - it is usually not a good idea to directly connect your application directly to the database. Instead, you should create an abstraction layer in the middle, as Data Access Layer (DAL) that will allow you to do the infrastructure and database work, without directly affecting the application. Here is a link with information about DAL

If you still wish to go ahead and access directly. You will need to do something along the lines of the following:

public void ConnectToDatabase()
        {
            // Replace with your connection string
            string connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;";

            // Create a new connection to the database
            SqlConnection connection = new SqlConnection(connectionString);

            // Open the connection
            connection.Open();

            // Do some work with the connection
            // ...

            // Close the connection
            connection.Close();
        }

Once again though, not the best approach.

  • Thank you for applying. I know how to create a DAL now , but I want to know how to connect this DAL with .Net Maui. Do I miss anything? – SpiderEgg Dec 13 '22 at 00:58
1

First, you should configure app constants. Configuration data, such as database filename and path, can be stored as constants in your app.

Then, you can create a database access class, and this is the solution for connecting DAL to .NET Maui. A database wrapper class abstracts the data access layer from the rest of the app. This class centralizes query logic and simplifies the management of database initialization, making it easier to refactor or expand data operations as the app grows. The SQLite.NET library provides a simple Object Relational Map (ORM) that allows you to store and retrieve objects without writing SQL statements.

Finally, you can access data smoothly. You can register your pages and the database access class as services on the IServiceCollection object, in MauiProgram.cs, with the AddSingleton and AddTransient methods.

For more details about these steps, you can refer to the document: https://learn.microsoft.com/en-us/dotnet/maui/data-cloud/database-sqlite?view=net-maui-7.0.

1

Use the following package from NuGet and use it as same as System.Data.SqlClient

Microsoft.Data.SqlClient

Example:

Microsoft.Data.SqlClient.SqlConnection sqlconnection;
sqlconnection = new Microsoft.Data.SqlClient.SqlConnection(StrConn);

Microsoft.Data.SqlClient.SqlCommand comm = new Microsoft.Data.SqlClient.SqlCommand(query, sqlconnection);
comm.ExecuteNonQuery();

Tested on Android and Windows .NET MAUI