Support SqlServer TypeName

This commit is contained in:
sunkaixuan
2019-04-28 13:59:07 +08:00
parent adbbcc00a3
commit 42a56874e0
2 changed files with 71 additions and 2 deletions

View File

@@ -41,6 +41,35 @@ namespace SqlSugar
SettingDataType(type); SettingDataType(type);
} }
public SugarParameter(string name, object value, System.Data.DbType type)
{
this.Value = value;
this.ParameterName = name;
this.DbType = type;
}
public SugarParameter(string name, DataTable value,string SqlServerTypeName)
{
this.Value = value;
this.ParameterName = name;
this.TypeName = SqlServerTypeName;
}
public SugarParameter(string name, object value, System.Data.DbType type, ParameterDirection direction)
{
this.Value = value;
this.ParameterName = name;
this.Direction = direction;
this.DbType = type;
}
public SugarParameter(string name, object value, System.Data.DbType type, ParameterDirection direction, int size)
{
this.Value = value;
this.ParameterName = name;
this.Direction = direction;
this.Size = size;
this.DbType = type;
}
private void SettingDataType(Type type) private void SettingDataType(Type type)
{ {
if (type == UtilConstants.ByteArrayType) if (type == UtilConstants.ByteArrayType)
@@ -197,5 +226,8 @@ namespace SqlSugar
{ {
this.DbType = System.Data.DbType.String; this.DbType = System.Data.DbType.String;
} }
public string TypeName { get; set; }
} }
} }

View File

@@ -66,8 +66,8 @@ namespace SqlSugar
} }
if (parameters.HasValue()) if (parameters.HasValue())
{ {
IDataParameter[] ipars = ToIDbDataParameter(parameters); SqlParameter[] ipars = GetSqlParameter(parameters);
sqlCommand.Parameters.AddRange((SqlParameter[])ipars); sqlCommand.Parameters.AddRange(ipars);
} }
CheckConnection(); CheckConnection();
return sqlCommand; return sqlCommand;
@@ -108,5 +108,42 @@ namespace SqlSugar
} }
return result; return result;
} }
/// <summary>
/// if mysql return MySqlParameter[] pars
/// if sqlerver return SqlParameter[] pars ...
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
public SqlParameter[] GetSqlParameter(params SugarParameter[] parameters)
{
if (parameters == null || parameters.Length == 0) return null;
SqlParameter[] result = new SqlParameter[parameters.Length];
int index = 0;
foreach (var parameter in parameters)
{
if (parameter.Value == null) parameter.Value = DBNull.Value;
var sqlParameter = new SqlParameter();
sqlParameter.ParameterName = parameter.ParameterName;
sqlParameter.UdtTypeName = parameter.UdtTypeName;
sqlParameter.Size = parameter.Size;
sqlParameter.Value = parameter.Value;
sqlParameter.DbType = parameter.DbType;
sqlParameter.Direction = parameter.Direction;
result[index] = sqlParameter;
if (parameter.TypeName.HasValue()) {
sqlParameter.TypeName = parameter.TypeName;
sqlParameter.SqlDbType = SqlDbType.Structured;
sqlParameter.DbType = System.Data.DbType.Object;
}
if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput, ParameterDirection.ReturnValue))
{
if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);
this.OutputParameters.Add(sqlParameter);
}
++index;
}
return result;
}
} }
} }