forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLiteDal.cs
137 lines (133 loc) · 4.89 KB
/
SQLiteDal.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System.Data;
using System.Data.Common;
using CYQ.Data.Cache;
using System.Reflection;
using System;
using System.IO;
namespace CYQ.Data
{
internal class SQLiteDal : DbBase
{
private CacheManage _Cache = CacheManage.LocalInstance;//Cache操作
public SQLiteDal(ConnObject co)
: base(co)
{
base.isUseUnsafeModeOnSqlite = co.Master.Conn.ToLower().Contains("syncpragma=off");
}
public override void AddReturnPara()
{
}
protected override bool IsExistsDbName(string dbName)
{
string name = Path.GetFileNameWithoutExtension(DbFilePath);
string newDbPath = DbFilePath.Replace(name, dbName);
return File.Exists(newDbPath);
}
//protected override string ChangedDbConn(string newDbName)
//{
// string dbName = Path.GetFileNameWithoutExtension(DbFilePath);
// string newDbPath = DbFilePath.Replace(dbName, newDbName);
// if (File.Exists(newDbPath))
// {
// filePath = string.Empty;
// return base.conn.Replace(dbName, newDbName);
// }
// return conn;
//}
private Assembly GetAssembly()
{
object ass = _Cache.Get("SQLite_Assembly");
if (ass == null)
{
try
{
ass = Assembly.Load(providerName);
_Cache.Add("SQLite_Assembly", ass, null, 10080, System.Web.Caching.CacheItemPriority.High);
}
catch (Exception err)
{
string errMsg = err.Message;
if (!System.IO.File.Exists(AppConst.RunFolderPath + "System.Data.SQLite.DLL"))
{
errMsg = "Can't find the System.Data.SQLite.dll more info : " + errMsg;
}
else
{
errMsg = "You need to choose the right version : x86 or x64. more info : \r\n" + errMsg;
}
Error.Throw(errMsg);
}
}
return ass as Assembly;
}
protected override DbProviderFactory GetFactory(string providerName)
{
object factory = _Cache.Get("SQLite_Factory");
if (factory == null)
{
Assembly ass = GetAssembly();
factory = ass.CreateInstance("System.Data.SQLite.SQLiteFactory");
if (factory == null)
{
throw new System.Exception("Can't Create SQLiteFactory in System.Data.SQLite.dll");
}
else
{
_Cache.Add("SQLite_Factory", factory, null, 10080, System.Web.Caching.CacheItemPriority.High);
}
}
return factory as DbProviderFactory;
}
public override string DataBase
{
get
{
return Path.GetFileNameWithoutExtension(DbFilePath) + "." + _con.Database;
}
}
string filePath = string.Empty;
public string DbFilePath
{
get
{
if (string.IsNullOrEmpty(filePath))
{
string conn = _con.ConnectionString;
int start = conn.IndexOf('=') + 1;
int end = conn.IndexOf(';');
int length = end > start ? end - start : conn.Length - start;
filePath = conn.Substring(start, length);
}
return filePath;
}
}
//public override DbParameter GetNewParameter()
//{
// Assembly ass = GetAssembly();
// object para = ass.CreateInstance("System.Data.SQLite.SQLiteParameter");
// if (para == null)
// {
// throw new System.Exception("Can't Create SQLiteParameter in System.Data.SQLite.dll");
// }
// else
// {
// return para as DbParameter;
// }
// //object para = _Cache.Get("SQLite_Parameter");
// //if (para == null)
// //{
// // Assembly ass = GetAssembly();
// // para = ass.CreateInstance("System.Data.SQLite.SQLiteParameter");
// // if (para == null)
// // {
// // throw new System.Exception("Can't Create SQLiteParameter in System.Data.SQLite.dll");
// // }
// // else
// // {
// // _Cache.Add("SQLite_Parameter", para, null, 10080);
// // }
// //}
// //return para as DbParameter;
//}
}
}