Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I improve my data access classes in ASP.NET
    text
    copied!<p>I've been coding for a while, but still consider myself a beginner. I use very simplistic ADO.NET classes with inbuilt SQL statements. I'd like to hear from the community about what I'm doing wrong and how I can improve, and what the suggested next steps are to take my coding into current standards.</p> <p>I'm really interested in trying EF, although I can't seem to find a tutorial that fits in with the way I do my BLL and DAL classes, so would appreciate a pointer in the right direction.</p> <p>Basically if I have a Gift, I would create a Gift class (BLL\Gift.cs):</p> <pre><code>using MyProject.DataAccessLayer; namespace MyProject.BusinessLogicLayer { public class Gift { public int GiftID { get; set; } public string GiftName { get; set; } public string Description { get; set; } public decimal Price { get; set; } public static Gift GetGiftByID(int GiftID) { GiftDAL dataAccessLayer = new GiftDAL(); return dataAccessLayer.GiftsSelectByID(GiftID); } public void DeleteGift(Gift myGift) { GiftDAL dataAccessLayer = new GiftDAL(); dataAccessLayer.DeleteGift(myGift); } public bool UpdateGift(Gift myGift) { GiftDAL dataAccessLayer = new GiftDAL(); return dataAccessLayer.UpdateGift(myGift); } public int InsertGift(string GiftName, string Description, decimal Price) { Gift myGift = new Gift(); myGift.GiftName = GiftName; myGift.Description = Description; myGift.Price = Price; GiftDAL dataAccessLayer = new GiftDAL(); return dataAccessLayer.InsertGift(myGift); } } } </code></pre> <p>I then have a DAL class which holds my connection string (DAL\sqlDAL.css):</p> <pre><code>namespace MyProject.DataAccessLayer { public class SqlDataAccessLayer { public readonly string _connectionString = string.Empty; public SqlDataAccessLayer() { _connectionString = WebConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString; if (string.IsNullOrEmpty(_connectionString)) { throw new Exception("No database connection String found"); } } } } </code></pre> <p>and then a DAL class (DAL\giftDAL.cs) where I've shown a couple of the methods (Update and Delete):</p> <pre><code>using MyProject.BusinessLogicLayer; namespace MyProject.DataAccessLayer { public class GiftDAL : SqlDataAccessLayer { public bool UpdateGift(Gift GifttoUpdate) { string UpdateString = ""; UpdateString += "UPDATE Gifts SET"; UpdateString += "GiftName = @GiftName"; UpdateString += ",Description = @Description "; UpdateString += ",Price = @Price "; UpdateString += " WHERE GiftID = @GiftID"; int RowsAffected = 0; try { using (SqlConnection con = new SqlConnection(_connectionString)) { using (SqlCommand cmd = new SqlCommand(UpdateString, con)) { cmd.Parameters.AddWithValue("@GiftName", GifttoUpdate.GiftName); cmd.Parameters.AddWithValue("@Description", GifttoUpdate.Description); cmd.Parameters.AddWithValue("@Price ", GifttoUpdate.Price); cmd.Parameters.AddWithValue("@GiftID", GifttoUpdate.GiftID); con.Open(); RowsAffected = cmd.ExecuteNonQuery(); } } } catch (Exception ex) { Utils.LogError(ex.Message, ex.InnerException == null ? "N/A" : ex.InnerException.Message, ex.StackTrace); } return (RowsAffected == 1); } public void DeleteGift(Gift GifttoDelete) { string DeleteString = ""; DeleteString += "DELETE FROM GIFTS WHERE GIFTID = @GiftID"; try { using (SqlConnection con = new SqlConnection(_connectionString)) { using (SqlCommand cmd = new SqlCommand(DeleteString, con)) { cmd.Parameters.AddWithValue("@GiftID", GifttoDelete.GiftID); con.Open(); cmd.ExecuteNonQuery(); } } } catch (Exception ex) { Utils.LogError(ex.Message, ex.InnerException == null ? "N/A" : ex.InnerException.Message, ex.StackTrace); } } } } </code></pre> <p>So looking at that, how would you recommend I improve the code (if I continue to use ADO.NET) and what would my next step be to learn EF - or is there a better alternative?</p> <p>Cheers, Robbie</p>
 

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