Files
SqlSugar/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoAccessory.cs

86 lines
3.4 KiB
C#
Raw Normal View History

2017-01-07 21:54:51 +08:00
using System;
using System.Collections.Generic;
using System.Data;
2017-01-08 00:16:37 +08:00
using System.Data.Common;
2017-01-07 21:54:51 +08:00
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
namespace SqlSugar
{
2017-05-19 11:21:48 +08:00
public partial class AdoAccessory
2017-01-07 21:54:51 +08:00
{
protected IDbBind _DbBind;
protected IDbFirst _DbFirst;
protected ICodeFirst _CodeFirst;
protected IDbMaintenance _DbMaintenance;
protected IDbConnection _DbConnection;
2017-05-28 18:40:09 +08:00
protected virtual SugarParameter[] GetParameters(object parameters, PropertyInfo[] propertyInfo,string sqlParameterKeyWord)
2017-01-07 21:54:51 +08:00
{
2017-05-28 18:44:45 +08:00
List<SugarParameter> result = new List<SugarParameter>();
2017-05-28 18:40:09 +08:00
if (parameters != null)
2017-01-07 21:54:51 +08:00
{
2017-05-28 18:40:09 +08:00
var entityType = parameters.GetType();
2017-05-28 18:43:27 +08:00
var isDictionary = entityType.IsIn(PubConst.DicArraySO, PubConst.DicArraySS);
if (isDictionary)
2017-01-07 21:54:51 +08:00
{
2017-05-28 18:44:45 +08:00
DictionaryToParameters(parameters, sqlParameterKeyWord, result, entityType);
2017-01-07 21:54:51 +08:00
}
else
{
2017-05-28 18:44:45 +08:00
ProperyToParameter(parameters, propertyInfo, sqlParameterKeyWord, result, entityType);
2017-01-07 21:54:51 +08:00
}
}
2017-05-28 18:44:45 +08:00
return result.ToArray();
2017-01-07 21:54:51 +08:00
}
2017-05-28 18:43:27 +08:00
protected void ProperyToParameter(object parameters, PropertyInfo[] propertyInfo, string sqlParameterKeyWord, List<SugarParameter> listParams, Type entityType)
{
2017-05-29 17:06:21 +08:00
PropertyInfo[] properties = null;
2017-05-28 18:43:27 +08:00
if (propertyInfo != null)
{
2017-05-29 17:06:21 +08:00
properties = propertyInfo;
2017-05-28 18:43:27 +08:00
}
else
{
2017-05-29 17:06:21 +08:00
properties = entityType.GetProperties();
2017-05-28 18:43:27 +08:00
}
2017-05-29 17:06:21 +08:00
foreach (PropertyInfo properyty in properties)
2017-05-28 18:43:27 +08:00
{
2017-05-29 17:06:21 +08:00
var value = properyty.GetValue(parameters, null);
2017-07-01 13:10:36 +08:00
if (properyty.PropertyType.IsEnum())
2017-05-28 18:43:27 +08:00
{
value = Convert.ToInt64(value);
}
if (value == null || value.Equals(DateTime.MinValue)) value = DBNull.Value;
2017-05-29 17:06:21 +08:00
if (properyty.Name.ToLower().Contains("hierarchyid"))
2017-05-28 18:43:27 +08:00
{
2017-05-29 17:06:21 +08:00
var parameter = new SugarParameter(sqlParameterKeyWord + properyty.Name, SqlDbType.Udt);
parameter.UdtTypeName = "HIERARCHYID";
parameter.Value = value;
listParams.Add(parameter);
2017-05-28 18:43:27 +08:00
}
else
{
2017-05-29 17:06:21 +08:00
var parameter = new SugarParameter(sqlParameterKeyWord + properyty.Name, value);
listParams.Add(parameter);
2017-05-28 18:43:27 +08:00
}
}
}
protected void DictionaryToParameters(object parameters, string sqlParameterKeyWord, List<SugarParameter> listParams, Type entityType)
{
if (entityType == PubConst.DicArraySO)
{
2017-05-29 17:06:21 +08:00
var dictionaryParameters = (Dictionary<string, object>)parameters;
var sugarParameters = dictionaryParameters.Select(it => new SugarParameter(sqlParameterKeyWord + it.Key, it.Value));
listParams.AddRange(sugarParameters);
2017-05-28 18:43:27 +08:00
}
else
{
2017-05-29 17:06:21 +08:00
var dictionaryParameters = (Dictionary<string, string>)parameters;
var sugarParameters = dictionaryParameters.Select(it => new SugarParameter(sqlParameterKeyWord + it.Key, it.Value));
listParams.AddRange(sugarParameters); ;
2017-05-28 18:43:27 +08:00
}
}
2017-01-07 21:54:51 +08:00
}
2017-06-19 01:45:03 +08:00
}