diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml
index ddf513b8..9675d56b 100644
--- a/FreeSql/FreeSql.xml
+++ b/FreeSql/FreeSql.xml
@@ -1996,6 +1996,137 @@
+
+
+ 查询,若使用读写分离,查询【从库】条件cmdText.StartsWith("SELECT "),否则查询【主库】
+
+
+
+
+
+
+
+
+ 查询,ExecuteReaderAsync(dr => {}, "select * from user where age > @age", new { age = 25 })
+
+
+
+
+
+
+ 查询
+
+
+
+
+
+
+ 查询,ExecuteArrayAsync("select * from user where age > @age", new { age = 25 })
+
+
+
+
+
+
+
+ 查询
+
+
+
+
+
+
+ 查询,ExecuteDataSetAsync("select * from user where age > @age; select 2", new { age = 25 })
+
+
+
+
+
+
+
+ 查询
+
+
+
+
+
+
+ 查询,ExecuteDataTableAsync("select * from user where age > @age", new { age = 25 })
+
+
+
+
+
+
+
+ 在【主库】执行
+
+
+
+
+
+
+
+ 在【主库】执行,ExecuteNonQueryAsync("delete from user where age > @age", new { age = 25 })
+
+
+
+
+
+
+
+ 在【主库】执行
+
+
+
+
+
+
+
+ 在【主库】执行,ExecuteScalarAsync("select 1 from user where age > @age", new { age = 25 })
+
+
+
+
+
+
+
+ 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new SqlParameter { ParameterName = "age", Value = 25 })
+
+
+
+
+
+
+
+
+
+ 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new { age = 25 })
+
+
+
+
+
+
+
+
+ 执行SQL返回对象集合,Query<User>("select * from user where age > @age; select * from address", new SqlParameter { ParameterName = "age", Value = 25 })
+
+
+
+
+
+
+
+
+
+ 执行SQL返回对象集合,Query<User>("select * from user where age > @age; select * from address", new { age = 25 })
+
+
+
+
+
+
可自定义解析表达式
diff --git a/Providers/FreeSql.Provider.Sqlite/MonoAdapter.cs b/Providers/FreeSql.Provider.Sqlite/MonoAdapter.cs
new file mode 100644
index 00000000..ae762497
--- /dev/null
+++ b/Providers/FreeSql.Provider.Sqlite/MonoAdapter.cs
@@ -0,0 +1,72 @@
+using System;
+using System.Collections.Generic;
+using System.Data.Common;
+using System.Reflection;
+using System.Text;
+
+namespace FreeSql.Sqlite
+{
+ internal class MonoAdapter
+ {
+
+ static bool? _isMono;
+ static object _isMonoLock = new object();
+ static Assembly _monoAssemly;
+ static Type _monoSqliteConnectionType;
+ static Type _monoSqliteCommandType;
+ static Type _monoSqliteParameterType;
+ static Type _monoSqliteExceptionType;
+
+ static bool IsMono
+ {
+ get
+ {
+ if (_isMono != null) return _isMono == true;
+ lock (_isMonoLock)
+ {
+ Assembly ass = null;
+ try
+ {
+ ass = Assembly.Load("Mono.Data.Sqlite");
+ }
+ catch { }
+ _isMono = ass != null;
+ if (_isMono == false) return false;
+
+ _monoAssemly = ass;
+ _monoSqliteConnectionType = _monoAssemly.GetType("Mono.Data.Sqlite.SqliteConnection");
+ _monoSqliteCommandType = _monoAssemly.GetType("Mono.Data.Sqlite.SqliteCommand");
+ _monoSqliteParameterType = _monoAssemly.GetType("Mono.Data.Sqlite.SqliteParameter");
+ _monoSqliteExceptionType = _monoAssemly.GetType("Mono.Data.Sqlite.SqliteException");
+ }
+ return true;
+ }
+ }
+
+ public static DbConnection GetSqliteConnection(string connectionString)
+ {
+ if (IsMono == false) return new System.Data.SQLite.SQLiteConnection(connectionString);
+ return Activator.CreateInstance(_monoSqliteConnectionType, new object[] { connectionString }) as DbConnection;
+ }
+
+ public static DbCommand GetSqliteCommand()
+ {
+ if (IsMono == false) return new System.Data.SQLite.SQLiteCommand();
+ return Activator.CreateInstance(_monoSqliteCommandType, new object[0]) as DbCommand;
+ }
+
+ public static DbParameter GetSqliteParameter()
+ {
+ if (IsMono == false) return new System.Data.SQLite.SQLiteParameter();
+ return Activator.CreateInstance(_monoSqliteParameterType, new object[0]) as DbParameter;
+ }
+
+ public static bool IsSqliteException(Exception exception)
+ {
+ if (exception == null) return false;
+ if (IsMono == false) return exception is System.Data.SQLite.SQLiteException;
+ return exception.GetType() == _monoSqliteExceptionType;
+ }
+
+ }
+}
diff --git a/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteAdo.cs b/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteAdo.cs
index 137dd99e..f1217038 100644
--- a/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteAdo.cs
+++ b/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteAdo.cs
@@ -3,7 +3,6 @@ using SafeObjectPool;
using System;
using System.Collections;
using System.Data.Common;
-using System.Data.SQLite;
using System.Text;
using System.Threading;
@@ -56,7 +55,7 @@ namespace FreeSql.Sqlite
protected override DbCommand CreateCommand()
{
- return new SQLiteCommand();
+ return MonoAdapter.GetSqliteCommand();
}
protected override void ReturnConnection(ObjectPool pool, Object conn, Exception ex)
diff --git a/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs b/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs
index f5f200f8..0aac8b70 100644
--- a/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs
+++ b/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs
@@ -4,7 +4,6 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
-using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
@@ -35,7 +34,7 @@ namespace FreeSql.Sqlite
public void Return(Object obj, Exception exception, bool isRecreate = false)
{
- if (exception != null && exception is SQLiteException)
+ if (exception != null && MonoAdapter.IsSqliteException(exception))
{
try { if (obj.Value.Ping() == false) obj.Value.OpenAndAttach(policy.Attaches); } catch { base.SetUnavailable(exception); }
}
@@ -112,7 +111,7 @@ namespace FreeSql.Sqlite
public DbConnection OnCreate()
{
- var conn = new SQLiteConnection(_connectionString);
+ var conn = MonoAdapter.GetSqliteConnection(_connectionString);
return conn;
}
diff --git a/Providers/FreeSql.Provider.Sqlite/SqliteUtils.cs b/Providers/FreeSql.Provider.Sqlite/SqliteUtils.cs
index f861c6c6..e3c7b46e 100644
--- a/Providers/FreeSql.Provider.Sqlite/SqliteUtils.cs
+++ b/Providers/FreeSql.Provider.Sqlite/SqliteUtils.cs
@@ -4,7 +4,6 @@ using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
-using System.Data.SQLite;
using System.Linq.Expressions;
using System.Text;
@@ -34,13 +33,16 @@ namespace FreeSql.Sqlite
dbtype = DbType.Int64;
break;
}
- var ret = new SQLiteParameter { ParameterName = QuoteParamterName(parameterName), DbType = dbtype, Value = value };
+ var ret = MonoAdapter.GetSqliteParameter();
+ ret.ParameterName = QuoteParamterName(parameterName);
+ ret.DbType = dbtype;
+ ret.Value = value;
_params?.Add(ret);
return ret;
}
public override DbParameter[] GetDbParamtersByObject(string sql, object obj) =>
- Utils.GetDbParamtersByObject(sql, obj, "@", (name, type, value) =>
+ Utils.GetDbParamtersByObject(sql, obj, "@", (name, type, value) =>
{
var dbtype = (DbType)_orm.CodeFirst.GetDbInfo(type)?.type;
switch (dbtype)
@@ -56,7 +58,10 @@ namespace FreeSql.Sqlite
dbtype = DbType.Int64;
break;
}
- var ret = new SQLiteParameter { ParameterName = $"@{name}", DbType = dbtype, Value = value };
+ var ret = MonoAdapter.GetSqliteParameter();
+ ret.ParameterName = $"@{name}";
+ ret.DbType = dbtype;
+ ret.Value = value;
return ret;
});