-
Notifications
You must be signed in to change notification settings - Fork 18
Quick start
sdrapkin edited this page Apr 27, 2017
·
15 revisions
Install TinyORM From NuGet
// add "using" statements
using System.Threading.Tasks;
using SecurityDriven.TinyORM;
// create DbContext instance out of some connection string
var db = DbContext.Create(connectionString:
"Data Source=.\\SQL2012;Initial Catalog=tempdb;Integrated Security=True;Pooling=true;Max Pool Size=3000;");
var rows = await db.QueryAsync("select [Answer] = 2 + 3"); // "rows" is of type IReadOnlyList<dynamic>
// You can use any of these 3 ways to dynamically access the row data:
Console.WriteLine(rows[0].Answer); // 5 (name-based lookup)
Console.WriteLine(rows[0]["Answer"]); // 5 (name-based lookup)
Console.WriteLine(rows[0][0]); // 5 (order-based lookup)
public class POCO
{
public int Answer { get; set; }
}
// single static projection:
var poco = (rows[0] as RowStore).ToObject<POCO>();
var poco_via_factory = (rows[0] as RowStore).ToObject(() => new POCO());
Console.WriteLine(poco.Answer); // 5
// static projection of a list of rows:
var ids = await db.QueryAsync("select [Answer] = object_id from sys.objects;");
var pocoArray = ids.ToObjectArray<POCO>();
var pocoArray_via_factory = ids.ToObjectArray(() => new POCO());
for (int i = 0; i < pocoArray.Length; ++i) Console.WriteLine(ids[i].Answer);
var ids1 = await db.QueryAsync(
"select [Answer] = object_id from sys.objects where object_id between @low and @high;",
new { @low = 10, @high = 40});
var ids2 = await db.QueryAsync(
"select [Answer] = object_id from sys.objects where object_id in (@range)",
new { @range = Enumerable.Range(1, 40) });
// To pass in NULL for value types use Nullable null values:
var emptyResult = await db.QueryAsync(
"select [Answer] = object_id from sys.objects where object_id = @id",
new { @id = default(int?) }); // or "@id = (int?)null"
var parameters = new Dictionary<string, (object, Type)>();
parameters.Add("@low", 10.WithType()); // .WithType() is a helper extension method
parameters.Add("@high", 40.WithType());
var ids = await db.QueryAsync(
"select [Answer] = object_id from sys.objects where object_id between @low and @high;",
parameters);
var rows = await db.QueryAsync("select [Answer] = 2 + 3");
Console.WriteLine(rows[0].Answer is FieldNotFound); // False
Console.WriteLine(rows[0].answer is FieldNotFound); // True
var row = (await db.QueryAsync("select * from sys.objects")).First();
foreach (var column in row)
Console.WriteLine("[{0}] [{1}]", column.Key, (column.Value ?? "[NULL]").ToString());
/*
[name] [sysrscols]
[object_id] [3]
[principal_id] [[NULL]]
[schema_id] [4]
[parent_object_id] [0]
[type] [S ]
[type_desc] [SYSTEM_TABLE]
[create_date] [2012-02-10 20:16:00]
[modify_date] [2012-02-10 20:16:00]
[is_ms_shipped] [True]
[is_published] [False]
[is_schema_published] [False]
*/
var row = (await db.QueryAsync("select * from sys.objects")).First();
Console.WriteLine(row); // row.ToString() is called here
/*
[name, sysrscols]
[object_id, 3]
[principal_id, ]
[schema_id, 4]
[parent_object_id, 0]
[type, S ]
[type_desc, SYSTEM_TABLE]
[create_date, 2012-02-10 20:16:00]
[modify_date, 2012-02-10 20:16:00]
[is_ms_shipped, True]
[is_published, False]
[is_schema_published, False]
*/
var rows = await db.QueryAsync("select top (4) * from sys.objects");
foreach (var row in rows)
Console.WriteLine(new { Name = (string)row.name, ObjectId = (int)row.object_id });
/*
{ Name = sysrscols, ObjectId = 3 }
{ Name = sysrowsets, ObjectId = 5 }
{ Name = sysclones, ObjectId = 6 }
{ Name = sysallocunits, ObjectId = 7 }
*/
Copyright (c) 2016-2022 Stan Drapkin