views:

101

answers:

5

Hello, I am new to .NET and have heard about several different ways of querying a SQL Server databse such as ADO.NET and the entity framework.

Can anyone give me some advise about the best way to go for new applications?

Thanks for any help or suggestions.

+2  A: 

NHibernate is the way to go. See http://nhforge.org and http://sf.net/projects/nhibernate for more information.

The main difference between Entity Framework and NHibernate is that Entity Framework is only for Microsoft SQL Server (Oracle is kind of supported, but support is not ideal). NHibernate supports many many many databases.

Pieter
Also consider getting Fluent NHibernate if you do go with this option.
Andre Luus
+2  A: 

Entity Framework is the easiest, and its built in.

All you have to do is important your model, and then just update simple classes, that are automatically mapped to your db.

Its just like updating normal objects, but at the end you call SaveChanges.

Nix
If you are starting project which will last for few years don't use EF. It will pollute your domain objects with state information. Will force you to use specific style of coding... VisualStudio tools for EF are half backed. For long projects I'll recommend to use NHibernate.
Darius Kucinskas
Such BS, out of the box you get entities with state. But there are other very simple options. Have you even looked at EF recently? Let me help you.. (http://channel9.msdn.com/Blogs/wriju/POCO-Template-in-ADONET-Entity-Framework)
Nix
+1  A: 

LINQ to SQL is pretty easy to work with. You can drag and drop your database tables into the designer and write pretty simple queries in a few minutes.

    NorthwindDataContext db = new NorthwindDataContext();

    var products = from p in db.Products
                   select p;

Which would basically translate into SELECT * FROM Products

A few other select samples:

    var products = from p in db.Products
                   where p.Category.Name == "Beverages"
                   select p;

    var products = from p in db.Products
                   orderby p.Name
                   select p;
Dismissile
`var products = db.Products;`
SLaks
Trying to show the LINQ syntax...which is somewhat similar to SQL
Dismissile
For a general-purpose ORM solution on a new project I'd go with NHibernate over Linq-to-sql but this will probably be easier to work with out of the box.
flatline
+1  A: 

Here is an example using EF with code generation from the database (for a real app you probably want to generate your DB from the code, instead):

  1. Right click on your project >> Add >> New Item >> ADO.NET Entity Data Model.
  2. Pick a name for your entities i.e. MyEntities.edmx, click Next
  3. Select "Generate from database"
  4. Configure a 'New Connection' if there is not one already there. Next.
  5. Select the Tables, Views, and SPROCs you want included in the entities. Finish.

You will see a file MyEntities.edmx added to your project. You can open it in design view to see a diagram of your entities and relationships. Note that each entity should have a primary key - the easiest way to do this is to add an ID - auto increment field to each table, or a GUID column. Anyway now you can query your db like this:

// assuming a "Product" table, which has an entity pluralized to "Products"

MyEntities db = new MyEntities();

var cheapProducts = db.Products.Where(p => p.Price > 30); // var is IEnumerable<Product>
Alex
A: 

In my opinion the best solution is to create intermediate class between the db and application (some type of data layer), which a number of methods for manage queries. Description below base on SQLite as an analogy to SQLServer (ADO connector)

Mentioned class can by singleton, and you can call it instance wherever you want in your application - for SQLite it can looks like that:

private static SQLite instance;

public static SQLite getInstance()
{
   if(instance == null)
   {
    instance = new SQLite();
    thread = Thread.CurrentThread.ManagedThreadId;
   }
   return instance;
}

You can get the instance this way:

SQLite db = SQLite.getInstance();

This class can contain multiple methods for data manipulation for example:

public SQLiteCommand prepareQuery(string sql)
{
   cmd = new SQLiteCommand(sql, conn);
   return cmd;
}

public SQLiteDataReader executeReader()
{   
    return cmd.ExecuteReader();
}

public int executeInt()
{
   object obj = cmd.ExecuteScalar();
   return (int)obj;
}

but also transaction manage and diagnostoc methods. So now - you can use this-like class in you application if you have other db sources or even db types, you can create next data-layer (for example for Oracle or MSSQL, MySQL ...) each of which has implements the same interface for example:

IDataBase

and now, you have some sort of facade, which you can replace as needed dynamicly. From this time using db in application is concentrated in one place, and it is pure pleasure for programmer to use it - that's my suggestion.

UGEEN
Probably easier to use a preexisting DB abstraction rather than spinning up your own, unless the OP's goal is purely educational.
Jimmy
It's difficult in such situation to obtain uniform layer compatible with all subscribers
UGEEN