mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 20:38:16 +08:00
- 增加 IAdo.CommandFluent(sql) 方法执行 SQL 命令;
- 修复 SqlServer SqlBulkCopy IgnoreColumns 无效的 bug;
This commit is contained in:
@ -2805,6 +2805,21 @@
|
||||
<param name="obj">new { id = 1 } 或者 Dictionary<string, object></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.CommandFluent(System.String,System.Object)">
|
||||
<summary>
|
||||
SQL 命令执行类,fsql.Ado.CommandFluent("select * from user where age > ?age", new { age = 25 })<para></para>
|
||||
.WithConnection(connection)<para></para>
|
||||
.WithTransaction(transaction)<para></para>
|
||||
.WithParameter("age", 25)<para></para>
|
||||
.WithParameter("id", 11)<para></para>
|
||||
.CommandType(CommandType.Text)<para></para>
|
||||
.CommandTimeout(60)<para></para>
|
||||
.Query<T>(); 或者 ExecuteNonQuery/ExecuteScalar/ExecuteDataTable/ExecuteDataSet/ExecuteArray
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteConnectTest(System.Int32)">
|
||||
<summary>
|
||||
测试数据库是否连接正确,本方法执行如下命令:<para></para>
|
||||
@ -3674,6 +3689,43 @@
|
||||
<param name="where">表达式</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.Internal.Model.AdoCommandFluent.WithConnection(System.Data.Common.DbConnection)">
|
||||
<summary>
|
||||
使用指定 DbConnection 连接执行
|
||||
</summary>
|
||||
<param name="conn"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.Internal.Model.AdoCommandFluent.WithTransaction(System.Data.Common.DbTransaction)">
|
||||
<summary>
|
||||
使用指定 DbTransaction 事务执行
|
||||
</summary>
|
||||
<param name="tran"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.Internal.Model.AdoCommandFluent.WithParameter(System.String,System.Object,System.Action{System.Data.Common.DbParameter})">
|
||||
<summary>
|
||||
增加参数化对象
|
||||
</summary>
|
||||
<param name="parameterName">参数名</param>
|
||||
<param name="value">参数值</param>
|
||||
<param name="modify">修改本次创建好的参数化对象,比如将 parameterName 参数修改为 Output 类型</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.Internal.Model.AdoCommandFluent.CommandType(System.Data.CommandType)">
|
||||
<summary>
|
||||
设置执行的命令类型,SQL文本、或存储过程
|
||||
</summary>
|
||||
<param name="commandType"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.Internal.Model.AdoCommandFluent.CommandTimeout(System.Int32)">
|
||||
<summary>
|
||||
设置命令执行超时(秒)
|
||||
</summary>
|
||||
<param name="commandTimeout"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="P:FreeSql.Internal.Model.BatchProgressStatus`1.Data">
|
||||
<summary>
|
||||
当前操作的数据
|
||||
|
@ -62,6 +62,21 @@ namespace FreeSql
|
||||
/// <returns></returns>
|
||||
DbParameter[] GetDbParamtersByObject(object obj);
|
||||
|
||||
/// <summary>
|
||||
/// SQL 命令执行类,fsql.Ado.CommandFluent("select * from user where age > ?age", new { age = 25 })<para></para>
|
||||
/// .WithConnection(connection)<para></para>
|
||||
/// .WithTransaction(transaction)<para></para>
|
||||
/// .WithParameter("age", 25)<para></para>
|
||||
/// .WithParameter("id", 11)<para></para>
|
||||
/// .CommandType(CommandType.Text)<para></para>
|
||||
/// .CommandTimeout(60)<para></para>
|
||||
/// .Query<T>(); 或者 ExecuteNonQuery/ExecuteScalar/ExecuteDataTable/ExecuteDataSet/ExecuteArray
|
||||
/// </summary>
|
||||
/// <param name="cmdText"></param>
|
||||
/// <param name="parms"></param>
|
||||
/// <returns></returns>
|
||||
AdoCommandFluent CommandFluent(string cmdText, object parms = null);
|
||||
|
||||
/// <summary>
|
||||
/// 测试数据库是否连接正确,本方法执行如下命令:<para></para>
|
||||
/// MySql/SqlServer/PostgreSQL/达梦/人大金仓/神通: SELECT 1<para></para>
|
||||
|
@ -16,8 +16,8 @@ namespace FreeSql.Internal.CommonProvider
|
||||
{
|
||||
|
||||
protected abstract void ReturnConnection(IObjectPool<DbConnection> pool, Object<DbConnection> conn, Exception ex);
|
||||
protected abstract DbCommand CreateCommand();
|
||||
protected abstract DbParameter[] GetDbParamtersByObject(string sql, object obj);
|
||||
public abstract DbCommand CreateCommand();
|
||||
public abstract DbParameter[] GetDbParamtersByObject(string sql, object obj);
|
||||
public DbParameter[] GetDbParamtersByObject(object obj) => GetDbParamtersByObject("*", obj);
|
||||
|
||||
protected bool IsTracePerformance => _util?._orm?.Aop.CommandAfterHandler != null;
|
||||
@ -28,6 +28,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
public string ConnectionString { get; }
|
||||
public string[] SlaveConnectionStrings { get; }
|
||||
public Guid Identifier { get; }
|
||||
|
||||
protected CommonUtils _util { get; set; }
|
||||
protected int slaveUnavailables = 0;
|
||||
private object slaveLock = new object();
|
||||
@ -97,6 +98,8 @@ namespace FreeSql.Internal.CommonProvider
|
||||
//return props;
|
||||
}
|
||||
|
||||
public AdoCommandFluent CommandFluent(string cmdText, object parms = null) => new AdoCommandFluent(this, cmdText, parms);
|
||||
|
||||
public bool ExecuteConnectTest(int commandTimeout = 0)
|
||||
{
|
||||
try
|
||||
|
113
FreeSql/Internal/Model/AdoCommandFluent.cs
Normal file
113
FreeSql/Internal/Model/AdoCommandFluent.cs
Normal file
@ -0,0 +1,113 @@
|
||||
using FreeSql.Internal.CommonProvider;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql.Internal.Model
|
||||
{
|
||||
public class AdoCommandFluent
|
||||
{
|
||||
internal protected AdoProvider Ado { get; protected set; }
|
||||
internal protected DbConnection Connection { get; protected set; }
|
||||
internal protected DbTransaction Transaction { get; protected set; }
|
||||
internal protected CommandType CmdType { get; protected set; } = System.Data.CommandType.Text;
|
||||
internal protected string CmdText { get; protected set; }
|
||||
internal protected int CmdTimeout { get; protected set; }
|
||||
internal protected List<DbParameter> CmdParameters { get; } = new List<DbParameter>();
|
||||
|
||||
public AdoCommandFluent(AdoProvider ado, string commandText, object parms)
|
||||
{
|
||||
this.Ado = ado;
|
||||
this.CmdText = commandText;
|
||||
this.CmdParameters.AddRange(this.Ado.GetDbParamtersByObject(parms));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用指定 DbConnection 连接执行
|
||||
/// </summary>
|
||||
/// <param name="conn"></param>
|
||||
/// <returns></returns>
|
||||
public AdoCommandFluent WithConnection(DbConnection conn)
|
||||
{
|
||||
this.Transaction = null;
|
||||
this.Connection = conn;
|
||||
return this;
|
||||
}
|
||||
/// <summary>
|
||||
/// 使用指定 DbTransaction 事务执行
|
||||
/// </summary>
|
||||
/// <param name="tran"></param>
|
||||
/// <returns></returns>
|
||||
public AdoCommandFluent WithTransaction(DbTransaction tran)
|
||||
{
|
||||
this.Transaction = tran;
|
||||
if (tran != null) this.Connection = tran.Connection;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加参数化对象
|
||||
/// </summary>
|
||||
/// <param name="parameterName">参数名</param>
|
||||
/// <param name="value">参数值</param>
|
||||
/// <param name="modify">修改本次创建好的参数化对象,比如将 parameterName 参数修改为 Output 类型</param>
|
||||
/// <returns></returns>
|
||||
public AdoCommandFluent WithParameter(string parameterName, object value, Action<DbParameter> modify = null)
|
||||
{
|
||||
var param = this.Ado.GetDbParamtersByObject(new Dictionary<string, object> { [parameterName] = value }).FirstOrDefault();
|
||||
modify?.Invoke(param);
|
||||
this.CmdParameters.Add(param);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置执行的命令类型,SQL文本、或存储过程
|
||||
/// </summary>
|
||||
/// <param name="commandType"></param>
|
||||
/// <returns></returns>
|
||||
public AdoCommandFluent CommandType(CommandType commandType)
|
||||
{
|
||||
this.CmdType = commandType;
|
||||
return this;
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置命令执行超时(秒)
|
||||
/// </summary>
|
||||
/// <param name="commandTimeout"></param>
|
||||
/// <returns></returns>
|
||||
public AdoCommandFluent CommandTimeout(int commandTimeout)
|
||||
{
|
||||
this.CmdTimeout = commandTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int ExecuteNonQuery() => this.Ado.ExecuteNonQuery(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public object ExecuteScalar() => this.Ado.ExecuteScalar(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public DataTable ExecuteDataTable() => this.Ado.ExecuteDataTable(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public DataSet ExecuteDataSet() => this.Ado.ExecuteDataSet(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public object[][] ExecuteArray() => this.Ado.ExecuteArray(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public List<T> Query<T>() => this.Ado.Query<T>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public NativeTuple<List<T1>, List<T2>> Query<T1, T2>() => this.Ado.Query<T1, T2>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public NativeTuple<List<T1>, List<T2>, List<T3>> Query<T1, T2, T3>() => this.Ado.Query<T1, T2, T3>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public NativeTuple<List<T1>, List<T2>, List<T3>, List<T4>> Query<T1, T2, T3, T4>() => this.Ado.Query<T1, T2, T3, T4>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public NativeTuple<List<T1>, List<T2>, List<T3>, List<T4>, List<T5>> Query<T1, T2, T3, T4, T5>() => this.Ado.Query<T1, T2, T3, T4, T5>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
|
||||
#if net40
|
||||
#else
|
||||
public Task<int> ExecuteNonQueryAsync() => this.Ado.ExecuteNonQueryAsync(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public Task<object> ExecuteScalarAsync() => this.Ado.ExecuteScalarAsync(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public Task<DataTable> ExecuteDataTableAsync() => this.Ado.ExecuteDataTableAsync(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public Task<DataSet> ExecuteDataSetAsync() => this.Ado.ExecuteDataSetAsync(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public Task<object[][]> ExecuteArrayAsync() => this.Ado.ExecuteArrayAsync(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public Task<List<T>> QueryAsync<T>() => this.Ado.QueryAsync<T>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public Task<NativeTuple<List<T1>, List<T2>>> QueryAsync<T1, T2>() => this.Ado.QueryAsync<T1, T2>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public Task<NativeTuple<List<T1>, List<T2>, List<T3>>> QueryAsync<T1, T2, T3>() => this.Ado.QueryAsync<T1, T2, T3>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public Task<NativeTuple<List<T1>, List<T2>, List<T3>, List<T4>>> QueryAsync<T1, T2, T3, T4>() => this.Ado.QueryAsync<T1, T2, T3, T4>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
public Task<NativeTuple<List<T1>, List<T2>, List<T3>, List<T4>, List<T5>>> QueryAsync<T1, T2, T3, T4, T5>() => this.Ado.QueryAsync<T1, T2, T3, T4, T5>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray());
|
||||
#endif
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user