4

I have a query written as a string in C# that I'm attempting to call on a T-sql database (not a stored procedure). One of the parameters I have to pass in is a list of GUIDs. I'm attempting to use a table-valued parameter for this. Given that part of the query requires dynamic table names a stored procedure is considered somewhat clunky for debugging. The final part of the query joins the ID on the table to the list of Guids I'm passing in.

Adding the parameter to my command in

var ListOfIDs = new DataTable();
    ListOfIDs.Columns.Add("ID", typeof (Guid));
    selectedIDs.ForEach(x=> ListOfIDs.Rows.Add(x));

cmd.Parameters.Add(new SqlParameter("@ListOfIDs", SqlDbType.Udt)
{
   Value = ListOfIDs, UdtTypeName = "ListOfGuids"
});

My table

CREATE TYPE [dbo].[ListOfGuids] AS TABLE(
    [ID] [uniqueidentifier] NULL
)

When I run the line:

var reader = cmd.ExecuteReader();

I get the error:

Specified type is not registered on the target server.System.Data.DataTable, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.

I have been unable to locate what I need to do to get this working.

0

1 Answer 1

7

You should be using SqlDbType.Structured. .Udt is not a table-valued parameter or table type, that's a CLR User-Defined Type IIRC. A TVP / table-valued parameter is an alias type not a user-defined type. You also don't need to tell it the .UdtTypeName because, again, this has nothing to do with TVPs, and your code shouldn't be mentioning Udt in any shape or form.

If your procedure looks like this:

CREATE PROCEDURE dbo.MyProcedure
  @ListOfGUIDs dbo.ListOfGuids READONLY
AS
BEGIN
  ...
END

Then your C# code can be:

SqlCommand cmd       = new SqlCommand("dbo.MyProcedure", conn);
cmd.CommandType      = CommandType.StoredProcedure;
SqlParameter tvparam = cmd.Parameters.AddWithValue("@ListOfGUIDs", ListOfIDs);
tvparam.SqlDbType    = SqlDbType.Structured;

Or, if you just have a statement, like SELECT * FROM @ListOfGUIDs then:

SqlCommand cmd       = new SqlCommand("SELECT * FROM @ListOfGUIDs", conn);
cmd.CommandType      = CommandType.Text;
SqlParameter tvparam = cmd.Parameters.AddWithValue("@ListOfGUIDs", ListOfIDs);
tvparam.SqlDbType    = SqlDbType.Structured;
5
  • If that's the case, how do I define which DB type I'm using? Do I still use the UdtTypeName?
    – DocSmiley
    Oct 14, 2013 at 19:53
  • @DocSmiley you don't need to define that in the C# code - that should be defined in the input parameter for the stored procedure. Oct 14, 2013 at 19:55
  • I'm specifically not using a stored procedure. I have a built string in C# that is getting passed as the command text.
    – DocSmiley
    Oct 14, 2013 at 19:57
  • @DocSmiley that doesn't really change much about what I'm telling you at all, except that the CommandType wouldn't be a stored procedure. Maybe if you show the actual string you're building in C# you will get help more tailored to your actual problem (maybe you can explain why you don't want to use a stored procedure for something like this, too). Oct 14, 2013 at 20:00
  • Thank you very much for your prompt responses.
    – DocSmiley
    Oct 14, 2013 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.