Last active
August 9, 2021 00:53
-
-
Save duraz0rz/4592183 to your computer and use it in GitHub Desktop.
C# example with and without ORM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Very simple example using LINQ to SQL and based on the MSDN LINQ to SQL page. | |
Make sure you include System.Data.Linq and System.Data.Linq.Mappings in your usings. | |
*/ | |
// Table you want to associate with the entity | |
[Table(Name="Customer")] | |
public class Customer | |
{ | |
// Columns in the database. Should have the same name between database and class variable | |
[Column(IsPrimaryKey = true)] | |
public int row_id { get; set; } | |
[Column] | |
public string Name { get; set; } | |
[Column] | |
public int Age { get; set; } | |
} | |
// This is essentially the connection to your database. It handles generating SQL statements and | |
// creating objects out of those SQL statements (if they are queries) | |
public class MyDatabase : DataContext | |
{ | |
public Table<Customer> customerTable; | |
public MyDatabase(string connection) : base(connection) { } | |
} | |
// This actually retrieves the data for you. Look how short and sweet it is after setting up the above! | |
public static class DBHelper | |
{ | |
public static List<Customer> RetrieveAllCustomers() | |
{ | |
MyDatabase db = new MyDatabase(GetConnectionString()); | |
var customers = db.customerTable.Select(row => row); | |
/* Alternatively (This translates to the above statement): | |
* var customers = from row in db.customerTable | |
* select row; | |
*/ | |
return customers.ToList(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Customer | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
public int row_id { get; set; } | |
} | |
public static class DBHelper | |
{ | |
public static List<Customer> RetrieveAllCustomers() | |
{ | |
DataTable data = new DataTable(); | |
List<Customer> customers = new List<Customer>(); | |
using (SqlConnection conn = new SqlConnection(GetConnectionString())) | |
{ | |
conn.Open(); | |
if (conn.State == ConnectionState.Open) | |
{ | |
using (SqlCommand cmd = conn.CreateCommand()) | |
{ | |
cmd.CommandText = "SELECT name, age, row_id FROM customer"; | |
DataReader reader = cmd.ExecuteReader(); | |
data.Load(reader); | |
foreach (DataRow row in data.Rows) | |
{ | |
customers.Add(new Customer { Name = row.Field<string>("NAME"), Age = row.Field<int>("AGE"), row_id = row.Field<int>("ROW_ID")}); | |
} | |
} | |
} | |
} | |
return customers; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment