Commit c9569d3e authored by Marc Gravell's avatar Marc Gravell

Proposal for TVP support; one concern is that it won't play nicely with things like mini-profiler

parent 12d7617f
......@@ -558,7 +558,7 @@ static SqlMapper()
typeMap[typeof(DateTime?)] = DbType.DateTime;
typeMap[typeof(DateTimeOffset?)] = DbType.DateTimeOffset;
typeMap[typeof(TimeSpan?)] = DbType.Time;
typeMap[typeof(Object)] = DbType.Object;
typeMap[typeof(object)] = DbType.Object;
}
/// <summary>
/// Configire the specified type to be mapped to a given db-type
......@@ -3502,6 +3502,18 @@ public void Dispose()
}
}
}
/// <summary>
/// Used to pass a DataTable as a TableValuedParameter
/// </summary>
public static ICustomQueryParameter AsTableValuedParameter(this DataTable table, string typeName
#if !CSHARP30
= null
#endif
)
{
return new TableValuedParameter(table, typeName);
}
}
/// <summary>
......@@ -3779,6 +3791,34 @@ public T Get<T>(string name)
}
}
/// <summary>
/// Used to pass a DataTable as a TableValuedParameter
/// </summary>
sealed partial class TableValuedParameter : Dapper.SqlMapper.ICustomQueryParameter
{
private readonly DataTable table;
private readonly string typeName;
/// <summary>
/// Create a new instance of TableValuedParameter
/// </summary>
public TableValuedParameter(DataTable table) : this(table, null) { }
/// <summary>
/// Create a new instance of TableValuedParameter
/// </summary>
public TableValuedParameter(DataTable table, string typeName)
{
this.table = table;
this.typeName = typeName;
}
void SqlMapper.ICustomQueryParameter.AddParameter(IDbCommand command, string name)
{
var param = new System.Data.SqlClient.SqlParameter(name, SqlDbType.Structured);
param.Value = (object)table ?? DBNull.Value;
if (!string.IsNullOrEmpty(typeName)) param.TypeName = typeName;
command.Parameters.Add(param);
}
}
/// <summary>
/// This class represents a SQL string, it can be used if you need to denote your parameter is a Char vs VarChar vs nVarChar vs nChar
/// </summary>
......@@ -4170,6 +4210,7 @@ public partial class DbString
}
public partial class SimpleMemberMap
{
......
......@@ -2822,6 +2822,21 @@ class HasDoubleDecimal
public decimal? D { get; set; }
}
public void DataTableParameters()
{
try { connection.Execute("drop type MyTVPType"); } catch { }
connection.Execute("create type MyTVPType as table (id int)");
connection.Execute("create proc #DataTableParameters @ids MyTVPType readonly as select count(1) from @ids");
var table = new DataTable { Columns = { { "id", typeof(int) } }, Rows = { { 1 }, { 2 }, { 3 } } };
int count = connection.Query<int>("#DataTableParameters", new { ids = table.AsTableValuedParameter() }, commandType: CommandType.StoredProcedure).First();
count.IsEqualTo(3);
count = connection.Query<int>("select count(1) from @ids", new { ids = table.AsTableValuedParameter("MyTVPType") }).First();
count.IsEqualTo(3);
}
#if POSTGRESQL
class Cat
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment