I haven't found a great answer this so I wanted to try my hand at getting an answer. What is the best way to use Microsoft SQL Server with Dart? I need it to be able to use it from essentially any OS Web and Mobile. I feel the best way may be GraphQL but I am unexperienced in it. I have seen plenty of things saying a .NET API but I am unsure as to how to use the .NET API with Dart. Thank you in advance.
2 Answers
"The best way": there is no silverbullet, you'll have to decide what frameworks and tools to use. But i'll give a simple example how a client application typically connects to a database. Most basic example:
Client application
The client application can be any application the user typically uses. Some examples:
- Mobile app (written in native, Dart, Xamarin, ...)
- Desktop app (Electron, WPF, ...)
- Website app (Angular, React, Vue, ...)
- ...
API
The API is there to retrieve data, and change data. But It will also handle authentication, authorization, logging, doing business logic, ...
The API can be any technology you want. In your example .net core. Whats important is that your client application can communicate with your api. Using GraphQL, REST (JSON, XML, ...), ...
Dart has packages for GraphQL and REST
Database
Your API will then execute queries, inserts, updates, deletes, execute stored procedures on the Database of your choice. In your example SQL Server.
There are many possibilities on how to set this up, depending on your skills, knowledge of frameworks, how you want to deploy things.
How you want to deploy this will limit your choices as well. For your API:
- Serverless API (Via Azure Functions, AWS Lambda, ...)
- Cloud Website (Azure Web Apps, ...)
- Website hosted on premise
- Docker container
- ...
In real life scenarios this often gets more complex with Firewalls, Application Gateways, Virtual networks, clusters, ...
Comments
You can install a Sql Server Socket on your server:
https://github.com/nippur72/SqlServerSocket
Install and execute SqlServerSocket.exe in the background on the server machine where SQL Server is installed.
Also, you need a client:
https://github.com/nippur72/SqlServerSocket/tree/master/DartClient
And you can try some connections and queries directly to your DDBB:
// creates a connection
var conn = new
SqlConnection("SERVER=localhost;Database=mydb;Trusted_connection=yes");
// open connection
await conn.open();
// runs a query returning a single value
var howmany = await conn.queryValue("SELECT COUNT(*) FROM Customers");
// runs a query returning a single row
var myFirstCustomer = await conn.querySingle("SELECT name,age FROM Custormers");
print(myFirstCustomer["name"]);
// runs a query returning all rows
var customers = await conn.query("SELECT TOP 10 name,age FROM Custormers");
for(var customer in customers)
{
print(customer["name"]);
}
// execute a command, returning the number of rows affected
var n = await conn.execute("UPDATE Customers SET age=0");
print("zeroed $n customers");
// disconnect
await conn.close();
