I am trying to pass 2 values from c# to sql server that executes a stored procedure. Going through a user defined table type that looks like this:
CREATE TYPE [dbo].[Ident] AS TABLE(
[Id] [uniqueidentifier] NOT NULL,
[Id2] [uniqueidentifier] NOT NULL
)
GO
and the start of my stored procedure looks like this
CREATE procedure [dbo].[indicator]
@id [dbo].[Ident] READONLY
as
i am trying to pass the ids from tvp & tvp2 to sql server table type in the GetValue2 method. However i cant get it to work. I can get it to work when i pass one value but how do i get it to work for two? Any help would be great!
static void Main(string[] args)
{
String connectionString = "........";
List<Guid> tempguid = new List<Guid>();
tempguid.Add(Guid.Parse("guid...."));
DataTable tvp = new DataTable();
tvp.Columns.Add(new DataColumn("Id", typeof(Guid)));
//populate DataTable from your List here
foreach (var id in tempguid)
tvp.Rows.Add(id);
List<Guid> tempguidid2 = new List<Guid>();
tempguid.Add(Guid.Parse("guid....."));
DataTable tvp2 = new DataTable();
tvp2.Columns.Add(new DataColumn("Id", typeof(Guid)));
//populate DataTable from your List here
foreach (var id in tempguid)
tvp2.Rows.Add(id);
Console.WriteLine(GetValue2(ref tvp, connectionString));
}
public static List<Guid> GetValue2(ref DataTable tvp, ref DataTable tvp2, String connectionString)
{
List<Guid> items = new List<Guid>();
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("[dbo].[indicator]", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter tvpParameter = new SqlParameter();
tvpParameter.ParameterName = "@id";
tvpParameter.SqlDbType = System.Data.SqlDbType.Structured;
tvpParameter.Value = tvp;
tvpParameter.TypeName = "[dbo].[Ident]";
cmd.Parameters.Add(tvpParameter);
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
Console.WriteLine((int)rdr["type"]);
}
Console.ReadLine();
}
}
return items;
}