My TVP looks like this::
create type p_tvp as table
(
id int identity not null,
value float not null
)
I have some datareader that yields a list of double (not actual SQL executed):
using (var conn = new SqlConnection(_srcString)
using (var cmd = new SqlCommand("select * from (values(1e),(2e),(3e))f(v)", conn))
{
await conn.OpenAsync();
using(var rdr = await _cmd.ExecuteReaderAsync())
using(var tCon = new SqlConnection(_targetString))
using(var tcmd = new SqlCommand("MyStoredProcedure",tCon))
{
await tCon.OpenAsync();
tcmd.CommandType = CommandType.StoreProcedure;
var param = tcmd.Parameters.AddWithValue(@tvp, rdr);
param.SqlDbType = SqlDbType.Structured;
param.TypeName = "dbo.p_tpv";
await tcmd.ExecuteNonQuery();
}
}
The error I get is that I don't have enough parameters. As the TVP I'm targeting has 2 columns. If I add a dummy column, the error I get is that I can't identity insert into table valued parameters.
I cannot redefine the TVP.
The other question specifies a DataTable
as the source, the answer uses a IEnumerable<SqlDataRecord>
. Unfortunately, neither solves my case of having a DbDataReader
. I can convert to SqlDataRecord
if necessary, but I'd like to avoid the extra steps as I cannot easily control the DbDataReader
source.