diff --git a/Apewer.Source/Source/Access.cs b/Apewer.Source/Source/Access.cs
index 40fbfc8..94ebac1 100644
--- a/Apewer.Source/Source/Access.cs
+++ b/Apewer.Source/Source/Access.cs
@@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
-using System.Drawing;
using System.IO;
using System.Text;
using static Apewer.Source.SourceUtility;
@@ -14,7 +13,7 @@ using static Apewer.Source.SourceUtility;
namespace Apewer.Source
{
- /// 用于快速连接 Microsoft Access 数据库的辅助。
+ /// 连接 Access 数据库的客户端。
public abstract partial class Access
{
@@ -80,7 +79,7 @@ namespace Apewer.Source
public override string[] StoreNames() => throw new InvalidOperationException();
///
- public override string[] TableNames() => TextColumn("select name from msysobjects where type=1 and flags = 0");
+ public override string[] TableNames() => QueryStrings("select name from msysobjects where type=1 and flags = 0");
///
public override string Insert(object record, string table = null, bool adjust = true)
@@ -271,21 +270,21 @@ namespace Apewer.Source
}
///
- protected override string KeysSql(string tableName, string keyField, string flagField, long flagValue)
+ protected override string Keys(string tableName, string keyField, string flagField, long flagValue)
{
if (flagValue == 0) return $"select [{keyField}] from [{tableName}]";
else return $"select [{keyField}] from [{tableName}] where [{flagField}] = {flagValue}";
}
///
- protected override string RecordSql(string tableName, string keyField, string keyValue, string flagField, long flagValue)
+ protected override string Get(string tableName, string keyField, string keyValue, string flagField, long flagValue)
{
if (flagValue == 0) return $"select top 1 * from [{tableName}] where [{keyField}] = '{keyValue}'";
else return $"select top 1 * from [{tableName}] where [{keyField}] = '{keyValue}' and [{flagField}] = {flagValue}";
}
///
- protected override string RecordsSql(string tableName, string flagField, long flagValue)
+ protected override string List(string tableName, string flagField, long flagValue)
{
if (flagValue == 0) return $"select * from [{tableName}]";
else return $"select * from [{tableName}] where [{flagField}] = {flagValue}";
@@ -406,9 +405,10 @@ namespace Apewer.Source
#region protected
/// 获取或设置连接字符串。
+ ///
internal protected static string GenerateCS(string provider, string path, string pass, string jo)
{
- if (!File.Exists(path)) return null;
+ if (!File.Exists(path)) throw new FileNotFoundException("文件不存在。", path);
var sb = new StringBuilder();
@@ -486,12 +486,13 @@ namespace Apewer.Source
}
/// 使用 Microsoft.Jet.OLEDB.4.0 访问 Access 97 - 2003 数据库文件。
- public sealed class AccessJet4 : Access
+ public class AccessJet4 : Access
{
const string JetOleDB4 = "microsoft.jet.oledb.4.0";
/// 创建 Access 类的新实例。
+ ///
public AccessJet4(string path, string pass = null, string jo = null, Timeout timeout = null)
: base(GenerateCS(JetOleDB4, path, pass, jo), timeout) { }
@@ -503,12 +504,13 @@ namespace Apewer.Source
}
/// 使用 Microsoft.ACE.OLEDB.12.0 访问 Access 2007 数据库文件。
- public sealed class AccessAce12 : Access
+ public class AccessAce12 : Access
{
const string AceOleDB12 = "microsoft.ace.oledb.12.0";
/// 创建 Access 类的新实例。
+ ///
public AccessAce12(string path, string pass = null, string jo = null, Timeout timeout = null)
: base(GenerateCS(AceOleDB12, path, pass, jo), timeout) { }
diff --git a/Apewer.Source/Source/DbClient.cs b/Apewer.Source/Source/DbClient.cs
index 0ec9066..b1a90e9 100644
--- a/Apewer.Source/Source/DbClient.cs
+++ b/Apewer.Source/Source/DbClient.cs
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
-using System.Data.Common;
-using System.Reflection;
-using System.Text;
namespace Apewer.Source
{
@@ -21,7 +18,7 @@ namespace Apewer.Source
_timeout = timeout ?? Timeout.Default;
}
- #region connection
+ #region Connection
Timeout _timeout = null;
IDbConnection _conn = null;
@@ -84,7 +81,7 @@ namespace Apewer.Source
}
/// 关闭连接,并释放对象所占用的系统资源。
- public void Close()
+ public virtual void Close()
{
if (_conn != null)
{
@@ -100,7 +97,7 @@ namespace Apewer.Source
}
/// 关闭连接,释放对象所占用的系统资源,并清除连接信息。
- public void Dispose()
+ public virtual void Dispose()
{
Close();
}
@@ -110,7 +107,7 @@ namespace Apewer.Source
#endregion
- #region transaction
+ #region Transaction
private IDbTransaction _transaction = null;
private bool _autocommit = false;
@@ -205,9 +202,11 @@ namespace Apewer.Source
#endregion
- #region ado
+ #region ADO
/// 查询。
+ /// SQL 语句。
+ /// 为 SQL 语句提供的参数。
public IQuery Query(string sql, IEnumerable parameters = null)
{
if (TextUtility.IsEmpty(sql)) return new Query(false, "语句无效。");
@@ -268,7 +267,32 @@ namespace Apewer.Source
}
}
- /// 执行 SQL 语句,并加入参数。
+ /// 输出查询结果的首列数据。
+ ///
+ protected string[] QueryStrings(string sql, string[] excluded = null)
+ {
+ if (Connect().NotEmpty()) return new string[0];
+ using (var query = Query(sql))
+ {
+ if (!query.Success) throw new SqlException(query, sql);
+
+ var rows = query.Rows;
+ var list = new List(rows);
+ for (int r = 0; r < query.Rows; r++)
+ {
+ var cell = query.Text(r, 0);
+ if (TextUtility.IsEmpty(cell)) continue;
+ if (excluded != null && excluded.Contains(cell)) continue;
+ list.Add(cell);
+ }
+ return list.ToArray();
+ }
+ }
+
+ /// 执行。
+ /// SQL 语句。
+ /// 为 SQL 语句提供的参数。
+ /// 自动启动事务。
public IExecute Execute(string sql, IEnumerable parameters = null, bool autoTransaction = false)
{
if (TextUtility.IsEmpty(sql)) return new Execute(false, "语句无效。");
@@ -310,28 +334,18 @@ namespace Apewer.Source
}
}
- /// 输出查询结果的首列数据。
- protected string[] TextColumn(string sql, string[] excluded = null)
- {
- if (Connect().NotEmpty()) return new string[0];
- using (var query = Query(sql))
- {
- var rows = query.Rows;
- var list = new List(rows);
- for (int r = 0; r < query.Rows; r++)
- {
- var cell = query.Text(r, 0);
- if (TextUtility.IsEmpty(cell)) continue;
- if (excluded != null && excluded.Contains(cell)) continue;
- list.Add(cell);
- }
- return list.ToArray();
- }
- }
+ /// 查询数据库中的所有表名。
+ public abstract string[] TableNames();
+
+ /// 查询数据库实例中的所有数据库名。
+ public abstract string[] StoreNames();
+
+ /// 查询表中的所有列名。
+ public abstract string[] ColumnNames(string tableName);
#endregion
- #region parameter
+ #region Parameter
/// 创建参数。
///
@@ -360,7 +374,22 @@ namespace Apewer.Source
#endregion
- #region orm
+ #region ORM
+
+ /// 检查数据模型结构,存在异常时抛出异常。
+ ///
+ ///
+ protected static TableStructure Parse(Type model)
+ {
+ if (model == null) throw new ArgumentNullException(nameof(model), "数据模型类型无效。");
+
+ var ts = TableStructure.Parse(model);
+ if (ts == null) throw ModelException.InvalidStructure(model);
+ if (ts.TableName.IsEmpty()) throw ModelException.InvalidTableName(ts.Model);
+ if (ts.Key == null || ts.Key.Field.IsEmpty()) throw ModelException.MissingKey(ts.Model);
+ if (ts.Flag == null || ts.Flag.Field.IsEmpty()) throw ModelException.MissingFlag(ts.Model);
+ return ts;
+ }
/// 初始化指定类型,以创建表或增加字段。
/// 指定新的表名。
@@ -402,35 +431,32 @@ namespace Apewer.Source
/// 目标记录的类型。
/// 要执行的 SQL 语句。
/// 为 SQL 语句提供的参数。
- public Result