Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to stick with ADD.NET then why don't you look at <a href="http://msdn.microsoft.com/en-us/library/ff664408%28v=pandp.50%29.aspx" rel="nofollow">Data Application Block</a> from Microsoft Enterprise Library (current version is 5.0 May 2011) - it will allow you write vendor (MS-SQL/Oracle etc) neutral code easily and most of the boiler-plate coding get wrapped.</p> <p><a href="http://www.kps-fl.com/blog/?p=40" rel="nofollow">This</a> is probably a simplest/shortest tutorial that I could find to get you started. However, MSDN link has plenty of information and see key scenario sections to jump-start.</p> <p>Another suggestion is to use <code>TransactionScope</code> for managing transactions (instead of working directly with DbTransaction object).</p> <p>Said all that I will recommend using Entity Framework (or any similar OR mapper tool - e.g. check NHibernet) because then you don't have to write typical code for basic CRUD operations. As far as your Dilemma goes, here is basic code snippet to get you started - I am using EF 4.1 with Code-First approach, POCO entities and Fluent API:</p> <p>Entity:</p> <pre><code>public class Gift { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public decimal Price { get; set; } } </code></pre> <p>Data Access Layer:</p> <pre><code>public class MyDbContext : DbContext { public DbSet&lt;Gift&gt; Gifts { get; set; } public MyDbContext () : base("name=[ConnStringName]") {} protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Fluent API to provide mapping - you may use attributes in entity class var giftConfig = modelBuilder.Entity&lt;Gift&gt;(); giftConfig.Property(p =&gt; p.Id).HasColumnName("GiftID"); giftConfig.Property(p =&gt; p.Name).HasColumnName("GiftName"); giftConfig.Property(p =&gt; p.Description).HasColumnName("Description"); giftConfig.Property(p =&gt; p.Price).HasColumnName("Price"); giftConfig.HasKey(p =&gt; p.Id); base.OnModelCreating(modelBuilder); } } </code></pre> <p>Business Layer:</p> <pre><code>public static class GiftManager { public static Gift GetById(int id) { using(var db = new MyDbContext()) { return db.Gifts.Find(id); } } public static void Add(Gift gift) { using(var db = new MyDbContext()) { // do validation ... db.Gifts.Add(gift); // do auditing ... db.SaveChanges(); } } public static void Update(Gift gift) { using(var db = new MyDbContext()) { // do validation ... var entity = db.Sessions.Find(gift.Id); entity.Name = gift.Name; entity.Description = gift.Description; entity.Price = gift.Price; // do auditing ... db.SaveChanges(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload