How to connect C# to SQL (the easy way)


Learn how to connect to SQL from C# the easy way, using a micro-ORM called Dapper (built by the Stack Overflow team). See how easy it is to get data in and out of SQL without complicated generated code or messy DataTables.

Source Code:
To sign up for announcements about new videos and exclusive discounts, fill out this form: (your email will be kept safe and you will not be spammed).


Xem Thêm Bài Viết Khác:


  1. Hey Tim, I am leaving this comment here as a last resort, because I have found no solutions to my issue. 🙁
    I am doing everything advised in the video. I am returning the correct amount of rows from my database, but they are all null. I have tried sending back just one int among others, but can't get anything other than null values.

    Do you have any advice?

  2. Thank you Tim. I have been bouncing between edmx, Dao and using this structure for years. Weighing up one against the other. Liking the full control of everything, but also loving the telesense of edmx, however as you say, the black box debugging aspect kills me often. Thank you microsoft, for windows updates! ?

    You have now settled my mind, that I was right I am right to ignore edmx and stick to this dapper scenario, for the mix between ease of use and control.

    Thank you. Your vids are brilliant. The best out there!

  3. Hello, thank you for your video
    I have a question about at, how will be look like an exception to connection.Query when it doesn't find records? It returns null

  4. at 33:00, is that best practice to create the FullInfo property inside the model just to be able to get that combination of properties? wouldn't it be better to place the logic that gets the properties and then strings them together inside the dashboard, or maybe inside the data access class? Reason I'm in doubt is, what if later on I want to get different combinations of data from a model? Doesn't seem right to me that I would need to create a bunch of additional properties that just mix and match the existing properties of the model, but I'm probably missing something

  5. 31:39 It's better to override default ToString(), instead of different property. So, you wouldn't need to specify any .DisplayMember.

  6. Hi Tim – first of all, great thanks for your tutorials in general and this one in particular. I really enjoy them. I have struggled a little making this one work with a Docker container, but figured out that trusted connection was not possible, so I used the standard security string and it worked like charm. One doubt I have is what is the point of calling UpdateBinding() from both the Dashboard constructor and searchButton_Click event? It seems to work from the latter anyway, but I must be missing something. Thanks again!

  7. hi 🙂 first of all, this is great work that you are doing on your channel. regarding this video I have a question. at the beginning you said that EF is great when it works and it tends to break and be problematic. does this (opinion) still stands? does EF core has same problems? would you still prefer dapper? thnx 🙂 and keep up the great work 🙂

  8. I've tried this tutorial from the start 3 times and I get this error as soon as I try and connect to the database. I have followed everything to the exact letter. What is this error doing? Why is it showing up? Can you please help me and tell me which part of the connection string or SQL connection is not happy and why would it have this error?

    System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.

  9. Hey, I read your article on Entity Framework, would you say that writing your own queries and sending them through SqlCommand is the best way of working with a database in C#? Instead of let's say, using DataSets or EF, I'm curious to know your opinion about this. Thanks

  10. How do you use this Dapper method to connect to SQL Server when using the MVC format? There is no app.config file by default.
    I tried adding one by creating a new html file under the main project but cannot get a connection to work. Any suggestions?

  11. I completely agree, Dapper is the way to go for data access and mapping the data to a model as you have demonstrated. I especially like the idea of using SQL, vs SQL generated by EF/Linq for example.
    When a query is assigned to a Dapper instance, will Dapper recognize and match aliased column names?

  12. In watching the video again, at the 1 hour and 20 second or so mark you explain dapper will delete column data that is not matched in the application data model. Would it be more concise to say that unmatched, by name, columns are ignored?

  13. That's a to simplistic way to work… in the real world, this framework lack in many ways….

    1- If you work with many records and you want to track changes (inserted, modified, deleted, …): you will have to do it by hand, your simplistic example will grow a lot!
    2- The PK (primary keys) are not returned by your Execute, you will have to modify all your stored procedures to get them back…! 🙁
    3- The relations: in this kind of models, relations are not handled, SQL is all around relations… To handle them, will will have to spend more work again…
    4- …. and many others…
    Simple examples do simple works, but in the real world again, it is rare that is works in the way you show it…. Entity framework give you many super useful features who save you a lot of time. For example, using the lazy loading on relations will give you all the addresses of a client only by following the relation as client.addresses.tolist() Implementing it in your dapper will do the reverse of what you show, multiplicate the lines instead of saving time…
    Simple examples…. >>> simple works….

  14. im sitting here listening to all this amazing knowledge and cant help the fact that there are pure losers out there and had the nerve to hit the dislike button? Come on man, on a side note beautiful tutorials you have Tim as a new developer your knowledge and help is truly helpful.

  15. Concise intro, thanks for publishing!
    Read up a bit on Dapper and can see why it is recommended for database application IO.
    I’ll be incorporate it into a tool I am writing.

  16. Hi Tim, I would like to make an inquiry.

    I want to make a C # desktop application but with azure database, in the cloud.

    Based on your great experience, what type of connection should I use?

    Any suggestions to make my programming as efficient as possible.

    Thank you

  17. Thanks for the very useful video. I have 2 questions.
    1. is it applicable for SQLite (specially StoredProcedures)?
    2. Another method for extracting data from the database is Filling datatables. could you please compare using list<T> vs using tables?


Please enter your comment!
Please enter your name here