Use Dapper
ooh, nice. See Stackoverflow is using it so it must be good.
South Africa’s biggest forum. Discuss, discover, and connect with thousands of members.
Use Dapper
ooh, nice. See Stackoverflow is using it so it must be good.
Dapper will change your fu king life. I have someone on MyBB to thank for turning me onto it and I will be eternally grateful to them.
You picked up that? Besides the rest of that abomination called code.
Use Dapper
Code:using Dapper; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; namespace Konsole { internal class Program { private static void Main(string[] args) { var customerNumbers = GetCustomerNumbers(DateTime.Now, "abc"); Console.ReadKey(); } public static List<string> GetCustomerNumbers(DateTime transactionDate, string submissionId) { string myConnString = ConfigurationManager.ConnectionStrings["Connection"].ToString(); using (IDbConnection conn = new SqlConnection(myConnString)) { string sql = "select distinct CustomerNumber from tblCustDetails where transaction_date = @transactionDate and submission_id = @submissionId"; var param = new { transactionDate = transactionDate, submissionId = submissionId }; var results = conn.Query<string>(sql, param, commandType: CommandType.Text); return results.ToList(); } } } }
How about using a SqlDataAdapter to fill a datatable then do this:
dataTable.AsEnumerable().Select(r => r.Field<string>(columnName)).ToList();
Or even better, use EntityFramework or LinqToSql:
db.YourTable.Where(w => w.SomeColumn == "WhatEva").Select(s => s.YourColumn).ToList();
Yeah, you're better off using an ORM instead of writing SQL commands by yourself (for a large percentage of an average project). I've worked with both EntityFramework and LinqToSQL and you'll need to learn some Linq to use them, but it's not that hard and once you do it's also very handy for easily manipulating non DB list based data (such as lists, arrays, text etc).
There are others such as nHibernate and netTiers that I've used on projects, but I'm not a fan.
I'll go have a look at Dapper when I get a chance.
I seriously do not like entity framework. It's an incredibly slow, bloated mess.
I really don't believe that your code should be changing your DB schema so code first doesn't sit well with me and the models it generates from the DB aren't the best. I won't mention the fact that it doesn't support a number of MS SQL data types.
Dapper is the sweetspot for simplifying DB access while still requiring you to design the models you need.
Yeah, you're better off using an ORM instead of writing SQL commands by yourself (for a large percentage of an average project).
IMO ORMs like nHibernate should die and be forgotten and developers should stop being lazy.
Hehe, I would. Although I do see the appeal for smaller projects like prototyping or something that updates user records etc. with repetitive CRUD functions.I wouldn't go that far. Linq and Entity Framework have their place for rapid prototyping and PoCs.