How do I use SQL Server with C# and .NET?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] foreign [Applause] and I'm going to show you how you can connect to and query a SQL database by writing c-sharp code this is going to work whether your database is hosted locally using SQL Server or if it lives in the cloud with Azure SQL in the next five-ish minutes I'll tell you your different options and recommend one for your project then I'll dive into the code and show you how to do it these three different ways so you can decide for yourself I won't go into too much detail because I want to keep the video short and sweet but if you want a closer look at the code there's a link to the GitHub repository in the description below there's a couple different ways you can use C sharp with the SQL database entity from our core will be the most high level option there's less you need to know to get started and you can start querying your data just by using C sharp that means you don't have to write a SQL query unless you really want to as we move towards the right however there's more and more things that you'll have to do yourself with Dapper you will have to write your own SQL queries and an ado.net you will have to connect to the database manually now let's take a look at the code let's say I have a SQL database that contains all my favorite blogs I want to write a console application that connects to that database and gets all the blogs with a certain ID we really recommend using EF core at least to start with but I'll show you how to do it in all three ways on the right you'll see our three separate projects I'll start off with the EF core implementation because they'll give us a really nice high level understanding of how our console app connects to the database the project folder has a pretty straightforward structure we have the model.cs file where our setup code lives and the program.cs file where we get the data from the database we query our data and we read out the results 16 lines of code that's all it takes remember to download The Right nuget package for your project in this case you'll want to download The Entity framework course SQL Server package now let's see what that code looks like in action I'll run the application by going into debug and starting without debugging and looking at my terminal window I'm querying for blogs and I'm getting blog id1 cool in the setup code before I asked Entity framework to use its built-in Locker to show me some extra information specifically I asked for the SQL query that EF core generated for me that I didn't have to write myself let's take a look at the Dapper implementation thank you the model.cs file looks really different from the one we had in EF core and we see that we're writing SQL queries that we then pass into the server and get objects back let's run this application and see what it does debug start without debugging you'll see that it does the same thing that EF core did grab the blog with the ID of one and this URL last but not least let's take a look at ado.net there's no model.cs file here because we're not working with objects anymore you'll see that we have to manually open our connection and send in a command again the SQL query and then we write information back not as an object but rather as strings now that we've gone through three different ways of writing the same application as you can probably tell EF core is what we recommend people start with that's because with ef core you can query your data by writing C sharp so you don't have to learn SQL until you want to you'll also be working with data's objects and have access to power and features that help you scale well over time gapper is a great option if Speed and Performance is your number one you'll still get to work with data as objects but because there is less features you will get your data faster ado.net however we really don't recommend to anyone except for those with scenarios that really need it because let's face it there are some things that EF core and Dapper can't do in conclusion thank you all for watching and if you have any questions any feedback any suggestions for topics for next time please leave a comment below or reach out to us on Twitter Reddit or GitHub if you want to play around with the code you saw the GitHub repository is available in the description below [Music] foreign [Applause]
Info
Channel: dotnet
Views: 17,852
Rating: undefined out of 5
Keywords: .NET
Id: DeegkS0Vf9o
Channel Id: undefined
Length: 6min 13sec (373 seconds)
Published: Tue Apr 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.