RSS

SQL Server 2008 Table Valued Parameters

Sun, Dec 23, 2007

ADO.net, SQL, c#, visual studio 2008

Last two weeks I have been working on developing bulk data manipulations. And even before that I always wondered “What’s the best way to pass an array of values into a SQL Server stored procedure.” One option I found was OPENXML, which I blogged here.


And I was trying out visual studio 2008 and SQL server 2008 these days. And last night I found out that SQL server now support Tabled valued Parameters which allow us to send data tables as parameters to stored procedures. It still uses the same ADO.Net API.

Now I’m going to show how it works. For this I’m using the same scenario which I used in my previous post (reading contents of a folder and saving their information). here i have used sql server 2008 november ctp and visual c# 2008 express edition.

 First create the table given below:

CREATE TABLE FileDetails(
FileName varchar(50)
PRIMARY KEY,
CreatedDate varchar(50) ,
Size
decimal(18, 0) )

 

Then, we should declare a new Table User Defined Type in the database:

create type FileDetailsType as table
(
    FileName        varchar(50),
    CreatedDate        varchar(50),
    Size       decimal(18,0)
)

 


Then create a stored procedure that gets a parameter of the above type, and inserts many rows in a single command.

create procedure InsertFileDetails
(
    @FileDetails FileDetailsType readonly
)
as

    insert
    into   FileDetails (FileName, CreatedDate, Size)
    select FileName, CreatedDate, Size
    from   @FileDetails;

 

 

Then to execute this procedure, we can create a data table and add the rows in to it. And then pass this data table as parameter to the database.

private static void SaveFileDetail(List<FileInfo> info)

 {

  Console.WriteLine(“**********updating with tablevalued parameters****”);

  DataTable dt = preparedatatable();

  foreach (FileInfo file in info)

     {

       DataRow dr = dt.NewRow();

       dr[0] = file.Name;

       dr[1] = file.CreationTime.ToShortDateString();

       dr[2] = (decimal)file.Length;

       dt.Rows.Add(dr);

     }

 

           

 using (SqlConnection conn = new SqlConnection(“your connection string”))

    {

       SqlCommand cmd = conn.CreateCommand();

       cmd.CommandType = System.Data.CommandType.StoredProcedure;

       cmd.CommandText = “dbo.InsertFileDetails”;

       SqlParameter param = cmd.Parameters.AddWithValue(“@FileDetails”, dt);                              

       conn.Open();

       cmd.ExecuteNonQuery();

       Console.WriteLine(“Completed Updating the database”);

     }

 

 

Now that’s cool isn’t it????

 

Download the code 

This post was written by:

Aneef Fashir - who has written 19 posts on Aneef.Net.

Software Engineer, Sri Lanka.

Contact the author

4 Comments For This Post

  1. mike Says:

    thats interesting

  2. Carl Says:

    Its just what i am trying to do but im using vb and i keep getting a error saying there is no mapping between the datatable and the database ???

    i’ve downloaded your code and your doing nothing different than me, So any ideas ???

    Many Thanks
    Carl

  3. Aneef Fashir Says:

    Hi Carl, did u check your database columns, and their types, and see whether they match with the columns and types of the datatable.

    And are you sure you are using SQL Server 2008 November CTP or later. because passing datatables is not supported prior versions.

  4. mb Says:

    FINALLY!!!! A real sample!!! All other examples always leave out the client code part of the equation.

    This is awesome, your example got me started - the key being the need to convert entities into the datatable… I assume your preparedatatable() function simply creates the datatable with the same types as your UDT.

    Now what we need next is the ability to generate the design time database from the UDT automatically to have it typed beforehand.. but we can worry about that later, I’m just thrilled now to have this working… no more XML parsing! yeeehaaa!!!

Leave a Reply