-
Notifications
You must be signed in to change notification settings - Fork 18
/
SqlCeDatabase.cs
executable file
·82 lines (75 loc) · 2.65 KB
/
SqlCeDatabase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Data.SqlServerCe;
using System.Linq;
namespace DapperLite
{
public class SqlCeDatabase<TId> : Database<TId>
{
public SqlCeDatabase(SqlCeConnection connection) : base(connection) { }
public SqlCeDatabase(SqlCeConnection connection, DapperLiteException exceptionHandler, bool throwExceptions)
: base(connection, exceptionHandler, throwExceptions)
{
}
/// <summary>
/// This query is specific to SQLCE 3.5 at the time of writing.
/// A similar query can be used for SQL Server.
/// </summary>
protected override Dictionary<string, string> GetTableNameMap()
{
const string sql = @"
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'TABLE'";
return GetConnection().Query<string>(sql).ToDictionary(name => name);
}
public new SqlCeConnection GetConnection()
{
return base.GetConnection() as SqlCeConnection;
}
/// <summary>
/// Inserts the supplied object into the database. Infers table name from type name.
/// Uses CommitMode.Immediate to ensure data is written to disk immediately.
/// </summary>
public override void Insert(object obj)
{
try
{
string tableName = GetTableName(obj);
SqlCeConnection conn = GetConnection();
using (SqlCeTransaction tran = conn.BeginTransaction())
{
conn.Insert(tran, tableName, obj);
tran.Commit(CommitMode.Immediate);
}
}
catch (Exception e)
{
ExceptionHandler(e);
if (m_throwExceptions) throw;
}
}
/// <summary>
/// Updates the supplied object. Infers table name from type name.
/// Uses CommitMode.Immediate to ensure data is written to disk immediately.
/// </summary>
public override void Update(object obj)
{
try
{
string tableName = GetTableName(obj);
SqlCeConnection conn = GetConnection();
using (SqlCeTransaction tran = conn.BeginTransaction())
{
conn.Update(tran, tableName, obj);
tran.Commit(CommitMode.Immediate);
}
}
catch (Exception e)
{
ExceptionHandler(e);
if (m_throwExceptions) throw;
}
}
}
}