Support array parameters

This commit is contained in:
sunkaixuan 2019-01-12 03:30:41 +08:00
parent 45626e7a41
commit f0daf77b32
4 changed files with 43 additions and 1 deletions

View File

@ -197,6 +197,9 @@ namespace OrmTest.Demo
var t2 = db.Ado.GetInt("select 1 from dual");
var t3 = db.Ado.GetDataTable("select 1 as id from dual");
var sqlPage = db.SqlQueryable<Student>("select * from student").ToPageList(1, 2);
var t4 = db.Ado.GetScalar("select * from student where id in (@id) ", new { id = new List<int>() { 1, 2, 3 } });
var t5 = db.Ado.GetScalar("select * from student where id in (@id) ", new { id = new int[] { 1, 2, 3 } });
var t6 = db.Ado.GetScalar("select * from student where id in (@id) ", new SugarParameter("@id", new int[] { 1, 2, 3 }));
db.Ado.CommitTran();
//more
//db.Ado.GetXXX...

View File

@ -310,6 +310,9 @@ namespace OrmTest.Demo
var t1 = db.Ado.SqlQuery<string>("select 'a'");
var t2 = db.Ado.GetInt("select 1");
var t3 = db.Ado.GetDataTable("select 1 as id");
var t4 = db.Ado.GetScalar("select * from student where id in (@id) ", new { id = new List<int>() { 1, 2, 3 } });
var t5 = db.Ado.GetScalar("select * from student where id in (@id) ", new { id = new int [] { 1, 2, 3 } });
var t6= db.Ado.GetScalar("select * from student where id in (@id) ", new SugarParameter("@id", new int[] { 1, 2, 3 }));
db.Ado.CommitTran();
var t11 = db.Ado.SqlQuery<Student>("select * from student");
//more

View File

@ -48,7 +48,7 @@ namespace OrmTest.Demo
//Insert List<T>
var insertObjs = new List<Student>();
for (int i = 0; i < 1000; i++)
for (int i = 0; i < 10; i++)
{
insertObjs.Add(new Student() { Name = "name" + i });
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
@ -284,6 +285,7 @@ namespace SqlSugar
{
try
{
InitParameters(ref sql, parameters);
if (FormatSql != null)
sql = FormatSql(sql);
SetConnectionStart(sql);
@ -313,6 +315,7 @@ namespace SqlSugar
{
try
{
InitParameters(ref sql, parameters);
if (FormatSql != null)
sql = FormatSql(sql);
SetConnectionStart(sql);
@ -341,6 +344,7 @@ namespace SqlSugar
{
try
{
InitParameters(ref sql, parameters);
if (FormatSql != null)
sql = FormatSql(sql);
SetConnectionStart(sql);
@ -373,6 +377,7 @@ namespace SqlSugar
{
try
{
InitParameters(ref sql,parameters);
if (FormatSql != null)
sql = FormatSql(sql);
SetConnectionStart(sql);
@ -791,6 +796,37 @@ namespace SqlSugar
{
ErrorEvent(new SqlSugarException(this.Context,ex, sql, parameters));
}
private void InitParameters(ref string sql, SugarParameter[] parameters)
{
if (parameters.HasValue())
{
foreach (var item in parameters)
{
if (item.Value != null)
{
var type = item.Value.GetType();
if ((type != UtilConstants.ByteArrayType && type.IsArray) || type.FullName.IsCollectionsList())
{
var newValues = new List<string>();
foreach (var inValute in item.Value as IEnumerable)
{
newValues.Add(inValute.ObjToString());
}
if (newValues.IsNullOrEmpty())
{
newValues.Add("-1");
}
if (item.ParameterName.Substring(0, 1) == ":")
{
sql = sql.Replace("@"+item.ParameterName.Substring(1), newValues.ToArray().ToJoinSqlInVals());
}
sql = sql.Replace(item.ParameterName, newValues.ToArray().ToJoinSqlInVals());
item.Value = DBNull.Value;
}
}
}
}
}
#endregion
}
}