5

I want to start using Databases in my C# Applications, unfortunately there appears to be lots of different ways of going about this.

Which is the 'best' way from a learning perspective? (IE: What is likely to be used in a business setup)?

If I want to download mySQL/Postgres and connect to that from C#, is that ok?

8
  • 3
    C# works best with SQL server,it sucks,but it's the truth,and if you want to get started learning about databases,I suggest following a SQL tutorial first,before diving into any specifics Commented Jan 25, 2011 at 12:59
  • @Phobia: C# works the same with MySQL. The MySQL Connector is basically System.Data for MySQL. Commented Jan 25, 2011 at 14:28
  • but you can't connect MySQL to VS.NET through the GUI,you have to write code to make it work Commented Jan 25, 2011 at 16:30
  • @Phobia: For newbies, that's what I'd recommend (writing code). That way when the GUI fails (and it will fail one day) they'll understand the magic that makes it work, and how it can be fixed. Commented Jan 25, 2011 at 16:37
  • 1
    Yes,writing code is indeed better for a beginner,but sometimes you need quick access to data,so you could see the databases and their contents before making requests against tables inside a database,it also helps you figure out which data is coming out from a certain table Commented Jan 25, 2011 at 18:58

7 Answers 7

6

Hmmm.... a lot of people have jumped straight from databases to ORM tools.

First choose the tech.... in a Microsoft stack that may mean Sql Server as people have suggested, so download Sql Server Express, a lightweight version of the full thing.

If you have never worked with databases before, I would suggest learning the underlying concepts first eg. tables, joins, indexes etc.

Once you have figured those out and written some queries against a test database, then consider ORM technologies.

2
  • 3
    Someone who is not well familiar with databases and database programming should never consider using an ORM, in my opinion. These are tools for people who know what they are doing, not amateurs.
    – HLGEM
    Commented Jan 25, 2011 at 14:26
  • 1
    +1 I fully agree! ORM shouldn't come until basic SQL is well understood. Commented Jan 25, 2011 at 15:21
4

A business setup (on a microsoft stack) will likely be SQL Server and an ORM tool. From a learning experience, you can just use SQL Express.

I would create a database using the Database Management tool to get to know the DBMS itself.

The .Net framework comes with a query language called, LINQ. This is good to learn. You can use Entity framework to create a model from the database you created (Visual studio will make it easy for you). As the name says, EF creates entities from you database model and you can use LINQ to perform queries on your entities, which the framework will translate to sql.

You are using an abstraction layer in this way, but it's just the way applications are build, so I see no harm in that

2
  • I would seriously not recommend using LINQ until the OP understands enough SQL to manage fine without it. Commented Jan 25, 2011 at 15:19
  • A valid point. However, he's getting started with c# and databases, not just databases. c# + databases = LINQ. That's why I mentioned it. But ofcourse it's very important to understand what's actually going on at the DB level.
    – Syg
    Commented Jan 25, 2011 at 16:16
3

Honestly, everyone is suggesting an ORM, but there's a lot of value in knowing how to call to the DB directly using SQLConnection/SQLCommand first. Before going straight to an abstraction, figure out what's being abstracted.

Shouldn't take too long as it's fairly straight forward, then you can move on to ORMs etc.

Just my $0.02

1

You can use any database with C# that you can find an ODBC or OLEDB connector for (so basically everything). I would recommend that you look at sqlexpress and use either linq 2 sql or entity framework. (Entity Framework has much better support while is much more complex.)

8
  • 1
    Linq2Sql will only work with SQL Server. And Entity Framework is still "work in progress".
    – user8685
    Commented Jan 25, 2011 at 12:43
  • 1
    Yet if you are just learning or on a windows stack then none of that matters. (And both are much easier to learn that the other monsters from the open source world.) Commented Jan 25, 2011 at 12:44
  • Microsoft is investing way more time in Entity framework. @Develop Art: I think Linq to SQL will die a slow death eventually (it's two frameworks solving the same problem). In what way is Entity framework 'work in progress'? Works fine!
    – Syg
    Commented Jan 25, 2011 at 12:58
  • EF is not really "work in progress" anymore, just version 4 is new, and linq2sql has been "discontinued", replaced by EF, so I wouldn't use that. If you're wanting MS product, EF is the way to go, otherwise pick one of the ORMs out there and go with it.
    – BlackICE
    Commented Jan 25, 2011 at 13:00
  • Linq2SQL is dead! So if you are going to do a solid application you may consider it. Commented Jan 25, 2011 at 13:02
1

Code-First Development with Entity Framework 4.

Take a look at this. It is a bolt on for C# which allows you to build your objects in code ("Models"), auto create the tables in a real SQL database, and handle your connections.

It isn't easy to just pick up and play but, if you get your head around the initial set up using a bit of google and links from the blog above, it can allow you to get up and running with data driven apps without a lot of the donkey work.

7
  • Mmm, this is still CTP and has a number of things that just don't work right yet? So not a good plan to get started I would say. Database first Entity model would a better plan. If you want to learn, you have to know what's going on the in the backend anyways
    – Syg
    Commented Jan 25, 2011 at 12:56
  • I don't recommend EF if performance is considered. Commented Jan 25, 2011 at 13:02
  • @Amir: As he is starting I doubt this is the case and I would be surprised if he could write more optimized SQL than is being generated by the framework
    – Syg
    Commented Jan 25, 2011 at 13:04
  • @Syg I agree! But I know too many small hacks becoming large application in companies. Commented Jan 25, 2011 at 13:12
  • @Amir This is just a prototype people! .. one year later... Why the hell is this now production code?
    – Syg
    Commented Jan 25, 2011 at 13:23
1

Others have mentioned EF, there are a number of other ORM frameworks, here are some of the big ones:

1

It all depends on how big your application is and what architecture is used. ORM may be considered for larger application.

EDIT:

  • Which is the 'best' way from a learning perspective? (IE: What is likely to be used in a business setup)?

In business sense many companies use ORM. So get familiar with concept why it is there and what problem it solves.

  • If I want to download mySQL/Postgres and connect to that from C#, is that ok?

You can simply use ADO.NET. However in business case companies use proper architecture to connect to databases. So learn the concept of layered architecture.

Check out this link for ORM compare.

enter image description here

LINQ Implementation Scorecard enter image description here

8
  • Nice chart! However, from a 'Business setup' perspective you have to take the Clients whishes into account as well. They might not want U to use anything other then microsoft supported software.
    – Syg
    Commented Jan 25, 2011 at 13:22
  • Agreed. But here you have a document that you can show if you don’t agree with your client decision. Commented Jan 25, 2011 at 13:24
  • I really don't think this applies. OP wants to get started learning SQL. They haven't mentioned that they have any specific business need, so this is probably major overkill for someone who's just getting started. Commented Jan 25, 2011 at 15:20
  • The question was about learning databases, ORM is a layer on top of a database.
    – ozz
    Commented Jan 25, 2011 at 15:32
  • @james and I typed "ORM may be considered for larger application." Commented Jan 25, 2011 at 15:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.