first Build the. NET Core Api-empty that's not going to be said.
and then create a new Controller
Remember to add a route
[Route ("Api/users")]
and then in Nuget Packages Install the required installation package
Here is MySQL so download the following Mysqlsugarcore
(Remember not to forget to install Mysql.data)
Create an instantiated class file DbText.cs
Used to connect to the database, initialize
Private dbtext () { } publicstaticstringgetset;} Public Static sqlsugarclient getinstance () { varnew sqlsugarclient (ConnectionString); return db; }
Of course, remember to quote Mysqlsugar's library.
then create the connection string in Appsetting.json
Click Startup.cs in the configureservices function:
Public void configureservices (iservicecollection services) { services. Addmvc (); = Configuration.getconnectionstring ("defaultconnection"); }
then create the model layer here I create from the fields in the database
Ok Next pre-work completed, the use of Sqlsugar for basic additions and deletions to check the operation
Select
Create A select interface
[HttpPost ("Select")] Public stringSelect (Users user) {using(vardb =dbtext.getinstance ()) { //Query the first five of all data and convert to JSON format varTOP5 = db. Queryable<users> (). By-it (it =it.username). ToJson (); //simple conditional query, query the names of all the girls in the table varGirl = db. Queryable<users> (). Where (It =>it.sex = =0). Select ("name"). ToJson (); returngirl; } }
Debug with postman after clicking Run to see the first 5 data fetched from the database returned in JSon format or the name of the girl in the table
Insert
Next try to implement the added action with Sqlsugar
[HttpPost ("Add")] Public stringRegister (Users user) {//This adds a student's data directly without judgment . using(vardb =dbtext.getinstance ()) { varStatus =db. Insert (user); //query If the sex you just inserted has a value varSex = db. Queryable<users> (). Single (it = It.userid = =status. Objtoint ()). sex; return "Status:"+status. ToString () +", Sex:"+sex; } }
Results
Discovery Database Rollup It's true that a single piece of data was inserted successfully
By the way, look at this db. what the return value of Insert () is.
I swapped a set of values to post again
You can see that status changed from 7 to 8 . I observed the database found this is the value of my primary key auto-increment field ID (Say why it is not a bool value)
Delete
Then you can delete the operation
[HttpPost ("Delete")] Public BOOLremoveuser (Users user) {using(vardb =dbtext.getinstance ()) { //Delete based on primary key//db. Delete<users, int> (10); //primary KEY Bulk Delete//db. Delete<users, string> (new string[] {"100", "101", "102"}); //Fake Delete//db. Falsedelete<school> ("Is_del", 100); //equivalent to update school set is_del=1 where ID in (+)//db. Falsedelete<school> ("Is_del", it=>it.id==100); //Conditional Delete is also met BOOLStatus = Db. Delete (NewUsers () {username ="issa2018", password ="123457"}); returnstatus; } }
Run postman after found to return false am I going to do this like that??
Then I change the code
bool status = db. Delete (new8});
This returns to true so this method can only be used for the primary KEY parameter!!!
The most silent thing is that I changed the code to the following ( the password is wrong ) thought he was going to have to meet the conditions to delete
bool status = db. Delete (new Users () {userid =7"123445"});
but the return result is true (that is, the column with UserID 7 is still deleted)
Finally found to meet the multi-conditional delete so come on, feel a little bit of trouble ah
bool status = db. Delete<users> ("[email protected] and [email protected]"new {username = User.username,password = User.password});
I might as well have done that.
bool status = db. Delete<users> ("username=""+user.username+"' and password= ' "+user.password+");
Update
first look at how to use the sqlsuagr how to modify it
[HttpPost ("Update")] Public BOOLeditinfo (Users user) {using(vardb =dbtext.getinstance ()) { //support for dictionary updates, for dynamic permissions//var dic = new dictionary<string, string> (); //DiC. ADD ("name", "13th"); //DiC. ADD ("Areaid", "1"); //db. Update<users, Int> (DIC, 13); //This update will put all the values except for the update into empty, because it's the entire user.//var updatestatus = db. Update<users> (new Users {name = User.Name, remark = User.remark}, it = It.username = User.username & IT.P Assword = = User.password); //Update the value of name and remark varUpdateStatus = db. Update<users> (New{name = User.Name, remark = User.remark}, it = It.username = = User.username & it.password = =User.password); returnUpdateStatus; } }
Postman passed data Update successfully
The next time you'll be sorting out more useful sqlsugar features.
. NET core +mysqlsugar (easiest to delete and change)