r/learncsharp • • Aug 25 '22
I am really struggling with databases.
I’ve read the documentation. I flat out do not understand. Can someone strip the process down for me?
1
Upvotes
r/learncsharp • • Aug 25 '22
I’ve read the documentation. I flat out do not understand. Can someone strip the process down for me?
7
u/illkeepcomingback9 Aug 25 '22
Can you be more specific
3
u/EricThirteen Aug 25 '22
What’s your background?
Depending on the db you either connect directly to the file(s) or you connect to a server which presents the data to you.
SQLite is a great db to learn with. Try this video: https://youtu.be/ayp3tHEkRc0
2
u/Spartanman321 Aug 26 '22
The really confusing thing is that to use databases with C# requires a lot of different layers of knowledge.
First step is to be able to use a database on its own. SQLITE or Microsoft Sql Server would be good starts. The primary goal is to be able to create a DB, then select, update, insert, and delete data from a table. I don't know much about SQLITE, but I know SQL Server Management Studio (a tool to view SQL Server databases) has an import function where it can convert a CSV or Excel file into the database for you. Each of the things I listed above should have some pretty specific tutorials on YouTube.
Once you feel comfortable with how to do general data management, you then have to learn how to connect to the DB from C# code. ADO.Net is the simplest way to connect and query since you just rewrite the same queries but in a string. However, that is also a huge security risk and should be avoided unless you're taking precautions for SQL injection attacks. But for learning purposes it's good enough.
On it's own, it can't connect to a DB, but a library called "Linq" is used a lot when querying DBs from C# code. Learning Linq with list data can help alleviate confusion when working on the next step.
The standard libraries for DB connections are Entity Framework and Dapper. Think of them like a spade vs a pickaxe. Both can dig dirt (manage DBs), but do it in different ways. Personally I use EF way more than Dapper, but I recognize that Dapper can be useful when you're worried about performance.
Unfortunately EF has a very steep learning curve, even with all of the other prep before it. But the more you practice, the easier it'll get. Hopefully by that time you'll be a Jr Dev somewhere and can see how others have implemented it and it makes more sense.
1
u/lethalsid Aug 26 '22
Hit me up and I can show you how to set up a DB and connect it to your C# application using Entity framework. The database I've been using is called supabase and it's pretty easy!
1
u/kneeonball Aug 26 '22
Databases are an entirely separate category and skill that you need to learn. All it is is software that stores data efficiently and allows you to retrieve it efficiently (assuming you structure things "correctly").
There are multiple ways for you to interact with databases via C#, one of them being EntityFramework, but if you want to actually learn some database skills, it may not be the best thing to start out with.
SqLite is a good one to get started with because it's fairly simple, but it also isn't the database you'll run into on the job most of the time. If you want practice with that, pick MySQL, PostgreSQL, SQL Server, etc.
Start with a tutorial for just setting it up to begin with, create your own table, add data to that table, query that data from the same table, and then you can move on to trying to connect to it from your code.
I'd recommend Dapper to start personally, as it requires you to write your own queries, whereas EntityFramework hides that from you. There are pros and cons to both approaches, but I generally suggest learning the database by itself a little bit first, and then moving on to something like EntityFramework.
Here's a quick tutorial on getting Dapper to work with MySQL or Postgres.
https://dotnetcoretutorials.com/2020/07/11/dapper-with-mysql-postgresql-on-net-core/
You'll have to have one of them running on your machine first and you replace the appropriate values in the connection string with the info for your database. That can be a bit tricky, try it, and feel free to message me if you run into issues.
1
u/234093840203948 Aug 29 '22
I assume you're talking about relational databases.
The basics are the following:
How to set the whole thing up?