Web API list data using ADO.net in .Net Core | Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today I'm going to show you how we can use web API with ado.net to fetch records from our database and show it on the API okay so uh to begin with we will select asp.net core web API this one and we will go ahead with next name the application so let's say all right so next dot net 6 uh not using for https uh I'll keep it simple okay open API support uh minimal no we will just use the normal API if we uncheck that that's going to take me for uh minimal apis well many my apis is something which we would do in future but as of now we will just proceed with the general API so the normal apis okay so as the project opens up so let's check uh the default weather forecast.cs is already there let's fix up with our app settings Let's uh add the connection string over here now you can see the connection string is added here so that is referring to my database in SQL Server which is uh this one Student Assignment is the database and Select Staff from students is the table so these are the records that we are trying to fetch through our API uh okay let's add a controller make this student controller guys you can fast forward this video if you want to because I know I'm a little bit slow so if you want you can just fast forward the video uh to the number of times you want okay so now we have to add references for the um connection string and uh fetch our data from there let's first of all check our program.cs if it has everything no it does not so I have to make some changes here okay give me a few seconds okay so after installing the couple of nougats uh let's try to do a run and see if things are running fine if I'm trying to fetch the weather records yes it's working okay so weather records are working as of now I think everything is good okay guys so I think I made one mistake because this was supposed to be an API controller but this one is not so we will have to remove this and I will add another API controller all right so you need to select this API controller okay guys so this is the API controller edit foreign looks good okay it's one thing to mention here we need the model classes because we need to refer to the table columns in that case without a model we would be having a problem so I'll have to add the models in that case I'll have to add the folder named as models I have to add the tables over here let me add the models and then I'll show you up so the model that we created for student now we are going to use that The Constructor we have to use the configuration string here configuration equals to the configuration that we passed in the parameter okay guys so uh you can look up this that uh we have created two objects for the connection string and for the command and uh here we are passing the model class the model class that we created here uh so uh yeah we are creating a list of type uh the model class and then we are refreshing the connection using a data table DT uh wait I'm sorry data ability won't be used here in the commands command oh I'm sorry yeah data table is used for uh binding the SQL adapter after finding binding it with the adapter then we will have to use a forage Loop in order to uh in order to populate our model class and then we are passing the model value with a Json string so certain things happened with the Json because there is uh there is no Newton soft uh in our nougats and also we haven't mentioned that in the program.cs that's why okay so let me just fix that one I will get back to you yeah add controllers with views dot add Newton soft I'm sorry okay so uh we added this one the builder.services.adcontroller with views uh dot add Newton soft options and in options we provided that serializer settings dot contract resolver this one is required to um like we sometimes get an error of serialization DC relation that's why we put this one in here and um hope everything should be fine with our okay still let me see okay I think I haven't used the okay guys so I find out the issue so what happened is this thing uh Json is not working because it's inheriting from controller base it's actually from controller class instead of instead of controller Base Class you have to use controller class that's right now you can see if everything is good okay don't need all of these all right so now you can see that everything is fixed uh we made some changes in the program.cs let's have a look at that we added this one Building Services dot at newtonsoft uh nothing more here in the Nuggets package uh we instead we installed uh these few things code generation is probably not required uh you may not use this one I used that's if you want to really uh what you say scaffold some codes then you might I mean you have to generate some codes like we right click and add a view or something in that case we would use this one but probably it's not used because here we are not using any kind of views uh remaining Newton software is required at some point Entity framework 4 is also not required here I might uninstall them okay so these are the few things that you can have a look at this uh these are the nugets that we installed and uh return Json is working now uh so as of this everything is everything should be fine and you should be able to get uh records from our database in this index okay uh model class is already here new gets you've already saw program.cs has this much of I forgot to raise the fonts okay I'm sorry so now you can see the text inside the program.cs if we go to our students controller then this is where what I have here in the app settings you can see that the connection string is mentioned at the bottom and in the mod class we have the column names uh student name it's required when I mean I put it the because we would be using this one to create a record in future so this is here so that's all in this program if we run this but try with API students execute and here is the data okay so it's able to fetch the data if you want to use the same thing in Postman also you can use this one in the postman without Swagger ah sorry if it's able to fetch the records here Postman took a while and it came up so we are going to put that in the get so you got your names from the database thank you we don't need to put anything in the body because this is just a guest request and we are not passing any parameters to them so once we try to create a record in that case we will have to add elements in the body uh the body text as of now we don't need that okay so guys uh key point to remember while you are creating our API is uh you have to inherit the controller class instead of the controller base and uh that's going to give you the access to return only a Json data but the returning data table okay so that's it for video this video and stay tuned and keep watching until then Happy coding thank you
Info
Channel: Fired Developer
Views: 745
Rating: undefined out of 5
Keywords: ado.net in dotnet core, webapi in dotnet core, web api using ado.net
Id: vSooNXbOM8Q
Channel Id: undefined
Length: 13min 14sec (794 seconds)
Published: Tue Aug 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.