About Us

Add a Metadata Property to User Profiles in SharePoint 2010

by Jess Collicott 13. April 2012 04:54

In this example, our pretend company has decided it would be beneficial for the culture if employees knew how to better work with and relate to each other as they take on and complete projects. To do this, the company decided to focus on a strengths-based culture, to better allow each person's passions drive them to success. One of the methods used to help employees know their own strengths and the strengths of others was having each individual take the Clifton StrengthFinders assessment, to discover their top five strengths. Knowing employees were going to be taking and completing the assessments, the leaders at the company would like each individual to be able to populate their user profile on SharePoint 2010 with these strengths.

Within SharePoint 2010, the list of strengths were added to a Managed Metadata Term Store, so that the spelling and usage of the strengths would be consistent across all of the user profiles:

Term-Store

The following are the steps that would be used to add a property to each person's user profile so that they could assign terms from the Strengths Term Store, and show them on their individual profile.

Create the User Profile Property

Open SharePoint Central Administration, and access the User Profile Service Application by following these steps:

  1. Under Application Management, click Manage service applications
  2. Find the desired User Profile Service Application in the list, and click to highlight it
  3. Click Manage in the Ribbon

On the following page, under People, click Manage User Properties:

Managed_Properties_Link

On the Manage User Properties page, click New Property:

New_Property_Link

In the first section, provide the desired Property Settings. In this example, we will choose the name of "CliftonStrengths", and the Display Name of "Strengths". We will choose a multi-value string, with up to 150 characters. The last field maps the property to the managed terms that were previously created in the Term Store.

Property_Settings

For the Sub-type of Profile, make sure the profiles are selected that you would like to display the property on. In this case, we only have the Default Profile:

Profile_Type

Under User Description, we will provide a description that will help people understand what the intended purpose of the property is for as they are editing their profile:

User_Description

For the Policy Settings, we will choose Required for the Policy Setting, and default the visibility to Everyone.

Policy_Settings

For the Edit Settings section, we need to make sure we allow employees to edit the property's values:

Edit_Settings

On the Display Settings section, we need to show the property on the user profile page. We also need to allow the property to show up to 5 values before being shortened with an ellipsis. To be able to edit the field, we need to allow the property to be shown on the Edit Details page. We also will allow updates to the property to show in the newsfeed.

Display_Settings

After these values are set, click OK to create the new user profile property.

Custom_Properties

 

Move to the Desired User Properties Section

As shown in the last step, the new Strengths property will be displayed under a section called "Custom Properties". We want to change this so the property shows in the "Details" section. To do this, click the blue "up" arrow next to the name of the property until it is in the desired section.

Order

 

Edit the User Profile

The property is now ready to be used on the SharePoint user profiles. Navigate to your profile, and click "Edit My Profile":

Edit_My_Profile

Scroll down to the "Details" section, and either type or select the five strengths to be listed on the profile:

Select_Strengths

Click Save and Close.

Then, on the user profile click the link titled "More Information":

More_Information_Link

The user profile should then show the "Strengths" property, with the selected strengths for the individual.

Final

A Quick Introduction to Fluent Validation in ASP.net MVC

by Travis Ellis 2. April 2012 20:03

In one of the projects I am working on we are using ASP.net MVC for our web front-end. I love the framework but I have never really been thrilled about validation using the DataAnnotations library. Based on the recommendation of some other people I discovered the FluentValidation library. The goal of this post is to show you how to create a very simple MVC application using FluentValidation as an alternative to DataAnnotations.

The first thing you need to do is create a new MVC Application using Visual Studio 2010:

image

The template you choose does not matter but for this post I am using the Internet Application template. There won’t be any authentication in the application so the Intranet template will work fine too.

Once the project is created right-click on the References item in Solution Explorer and click Manage NuGet Packages (if you do not see this menu then make sure you download the most recent version of NuGet from NuGet.org):

image

This will bring up the Package Manager dialog. In the Search box, type in FluentValidation and hit Enter. You should see a few packages in the results list. The one you want to add to the project is FluentValidation.MVC3. This will install the FluentValidation MVC3 package and FluentValidation itself (the MVC3 package depends on it). Select the FluentValidation.MVC3 package in the list and click the Install button:

image

Once the assemblies are added to the Solution, you can close the Package Manager and open the Global.asax file in the root directory of your project. You need to add a single line to your project to configure the framework to work with MVC. In the Application_Start method in Global.asax, add the following line:

FluentValidation.Mvc.FluentValidationModelValidatorProvider.Configure();

Once you have done this we can create a new Model class that will be used by one of our Views/Controllers:


public class RegistrationModel
{
    public string Username { get; set; }
    public string Password { get; set; }
    public string ConfirmPassword { get; set; }
    public DateTime BirthDate { get; set; }
    public int NumberOfChildren { get; set; }     
}

This is a simple model used to register for a website. It asks for a username, password (with confirmation), date of birth and number of children. Now that we have a model we can create the controller actions for it. We will need a GET and POST action:


[HttpGet]
public ActionResult Create()
{
    return View();
}

[HttpPost]
public ActionResult Create(RegistrationModel model)
{
    if(!ModelState.IsValid)
    {
        return View(model);
    }
    
    // register here

    return RedirectToAction("Index", "Home");
}

The code here is pretty bare and more than likely identical to the code that would be used when using DataAnnotations to do the validation. If you were to run the code right now there would be no validation. Let's fix that by creating a new validator class:


public class RegistrationValidator : AbstractValidator
{
    public RegistrationValidator()
    {
        RuleFor(model => model.Username)
            .NotEmpty()
            .Length(1, 256);

        RuleFor(model => model.Password)
            .NotEmpty()
            .Length(1, 50);

        RuleFor(model => model.ConfirmPassword)
            .NotEmpty()
            .Equal(model => model.Password);

        RuleFor(model => model.DateofBirth)
            .GreaterThanOrEqual(DateTime.Today.AddYears(-18));

        RuleFor(model => model.NumberOfChildren)
            .NotNull()
            .GreaterThan(0)
            .NotEqual(13);
    }
}

We have defined the following rules for our model:

  • Username is required and must be between 1 and 256 characters long.
  • Password is required and must be between 1 and 50 characters long.
  • Password confirmation is required and must match the password.
  • The Date of Birth must be greater than or equal to todays date 18 years ago (to confirm they are 18 or older).
  • User must have at least one child but can not have 13 (we are very superstitious).

Now that we have our validations in place, we need to update our model to include an attribute that will wire up the validation with MVC. Add the following attribute to our RegistrationModel class:


[Validator(typeof(RegistrationModel))]
public class RegistrationModel

That is all that is needed to use FluentValidation. The same rules apply when using client side validation. You need to include jQuery, jQuery Validation and optionally include jQuery Unobtrusive Validation scripts. These steps are no different than using DataAnnotations so it's very simple to convert a site to use the framework. If you are using Dependency Injection then you can also create your own validator factory that will wire up the validators for you so you don't need to register the type of validator on each model you create. If you use Ninject, there is already a NuGet package that has a validator factory that you can use. Just open up the NuGet package manager and search for Ninject.Extensions.FluentValidation.

FluentValidation is a very powerful framework. There are plenty of built in validators that you can use such as verifying a credit card or e-mail address. Creating your own custom validators is also very simple. Head on over to the CodePlex page today and look at the documentation to start using it in your projects.

Table Valued Parameters with Entity Framework

by Travis Ellis 16. November 2011 12:00

I recently worked on a project where I was calling a WCF service that returned a collection of values that had a Name and a Type. I needed to use that list to select values from another database using Entity Framework.  A typical result set from the WCF service looked like this:

var list = new List<NameAndType>
{
    new NameAndType { Name = "ABC", Type = "123" },
    new NameAndType { Name = "ABC", Type = "456" },
    new NameAndType { Name = "DEF", Type = "123" },
    new NameAndType { Name = "DEF", Type = "123" }
};

In a local database I had a table that had a composite primary key made up of Name and Type. I needed to write a query that took this list of name/type values and found matching rows in my database. This would be a simple thing to do if I had a single value as my key. If that were the case I could have written a query like this:

var list = List<string>
{
    "ABC",
    "DEF"
};
    
var items = from i in context.Items
            where list.Contains(i.Name)
            select i;

The above query will translate into a statement similar to SELECT * FROM Items WHERE Name IN ('ABC', 'DEF'). The problem is that I had two properties that I needed to predicate on. I wasn't sure how to write the query in Entity Framework but my first thought was something like the following:

var list = new List
{
    new NameAndType { Name = "ABC", Type = "123" },
    new NameAndType { Name = "ABC", Type = "456" },
    new NameAndType { Name = "DEF", Type = "123" },
    new NameAndType { Name = "DEF", Type = "123" }
};

var result = from a in context.Items
             join b in list
             on new { a.Name, a.Type } equals new { b.Name, b.Type }
             select a; 

This query threw an error complaining that only primitive types could be used. I tried a few more queries but I could not figure out a way to do this. I started to think about how I would actually accomplish this in SQL using a stored procedure and settled on using a Table-Valued Parameter (which I have blogged about before). At first I thought I was going to need to write a stored procedure and call that from Entity Framework, but after doing a little testing I was able to come up with a solution that allowed me to do this using an Entity Framework query:

DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Type"< typeof(string));

list.ForEach(l => dt.Rows.Add(l.Name, l.Type));

SqlParameter tableParameter = new SqlParameter("@Ids", SqlDbType.Structured);
tableParameter.TypeName = "dbo.NameAndType";
tableParameter.Value = dt;

var items = context.Database.SqlQuery<Item>(
    "SELECT * FROM dbo.Items items " +
    "INNER JOIN @Ids ids ON items.Name = ids.Name " +
    "AND items.Type = ids.Type", tableParameter);

The code creates a DataTable and adds a row for each item in my list of NameAndType values. It then creates a SqlParameter with SqlDbType of Structured and gives it a TypeName of dbo.NameAndType and a value of the table. I then created an Entity Framework query using the Database.SqlQuery<T> method which allows you to execute some SQL code. I select from my table and join to my table valued parameter and was able to get the items from my database.

One thing to note with this approach is that all of the items returned from the query will not be tracked by Entity Framework. If you need Entity Framework to track the entities you would have to do something similar to this:

foreach ( var item in items ) {
    db.Items.Attach(item);
}

In Summary

This concludes my post on writing an Entity Framework query that uses a table-valued parameter. If you have any questions, feel free to contact me with any questions.

Creating a Cascading Dropdown in ASP.net MVC 3 and jQuery

by Travis Ellis 5. August 2011 09:00

One of the common tasks that comes up when developing web applications is working with dependent data. If you have a form in your application that asks the user to select the Make and Model of their car, then you need to refresh the list of Models every time the Make changes. In ASP.net Web Forms this was commonly done by setting the AutoPostBack property of the DropDownList control and creating a SelectedIndexChanged event handler. The problem with this approach is that the page does a full postback, which can often be inefficient and can appear slow to the user. Fortunately, a better method exists in ASP.net MVC that is easy to accomplish with jQuery.

In order to show you how to create a cascading dropdown I am going to create a very simple web application. It will have a single page that will have two dropdowns, one for a list of States and one for a list of Cities. The list of Cities will change whenever the State changes. Here is the main controller for our application:

public class HomeController : Controller
{
    private ILocationRepository locationRepository = new LocationRepository();

    public ActionResult Index()
    {
        var model = new IndexViewModel();

        model.AvailableStates = new SelectList(locationRepository.GetStates(), "Abbreviation", "Name");
        model.AvailableCities = new SelectList(locationRepository.GetCities(), "Id", "Name");

        return View(model);
    }
}

public class IndexViewModel
{
    public IndexViewModel()
    {
        AvailableStates = new SelectList(Enumerable.Empty<State>(), "Abbreviation", "Name");
        AvailableCities = new SelectList(Enumerable.Empty<City>(), "Id", "Name");
    }

    public string State { get; set; }
    public int City { get; set; }

    public SelectList AvailableStates { get; set; }
    public SelectList AvailableCities { get; set; }
}

The view for our page will be extremely simple. It will consist of the two dropdown lists needed to display the data and a bit of javascript used to retrieve the data.

@model CascadingDropdowns.Models.IndexViewModel
@using (Html.BeginForm())
{
    <div class="editor-label">
        @Html.LabelFor(m => m.State)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(m => m.State, Model.AvailableStates, new { style = "width: 150px" })
    </div>
    
    <div class="editor-label">
        @Html.LabelFor(m => m.City)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(m => m.City, Model.AvailableCities, new { style = "width: 150px" })
    </div>
    
    <p>
        <input type="submit" value="Submit" />
    </p>
}

I am holding off on posting the javascript code until I post the code to retrieve the locations. The view is pretty simple. It defines a label and a dropdown for both the State and City property of our model. It uses the AvailableStates and AvailableCities properties as the data source for the dropdowns. It also contains a Submit button to POST the form, but this example won't be doing anything when the form is posted.

Here is the controller we will use to retrieve our location data. It has two action methods that both return JSON formatted data. The first method will return a list of all states. The other method will retrieve all of the cities based on the state abbreviation that is passed in.

public class LocationsController : Controller
{
    private ILocationRepository locationRepository = new LocationRepository();

    [HttpPost]
    public ActionResult States()
    {
        var states = locationRepository.GetStates();
            
        return Json(new SelectList(state, "Id", "Name"));
    }

    [HttpPost]
    public ActionResult Cities(string abbreviation)
    {
        var cities = locationRepository.GetCities(abbreviation);

        return Json(new SelectList(cities, "Abbreviation", "Name")); 
    }
}

Here is the corresponding repository class that will retrieve the data. I am going to hard code the data to simplify the example, although it would be easy to hook up this code to a database using your favorite data access tool such as Entity Framework or LINQ to SQL.

public class LocationRepository : ILocationRepository
{
    public IQueryable<State> GetStates()
    {
        return new List<State>
        {
            new State { Abbreviation = "NE", Name = "Nebraska" },
            new State { Abbreviation = "NC", Name = "North Carolina" }
        }.AsQueryable();
    }

    public IQueryable<City> GetCities(string abbreviation)
    {
        var cities = new List<City>();
 
        if (abbreviation == "NE")
        {
            cities.AddRange(new List<City> {
                new City { Id = 1, Name = "Omaha" },
                new City { Id = 2, Name = "Lincoln" }
            });
        }
        else if (abbreviation == "NC")
        {
            cities.AddRange(new List<City> {
                new City { Id = 3, Name = "Charlotte" },
                new City { Id = 4, Name = "Raleigh" }
            });
        }

        return cities.AsQueryable();
    }
}

public interface ILocationRepository
{
    IQueryable<State> GetStates();
    IQueryable<City> GetCities(string abbreviation);
}

Now that we have a controller action that will return the data we need, it is time to wrap it all up with our jQuery code. You might have noticed that our controller actions have been marked with the [HttpPost] attribute. This will prevent the browser from caching the data, and MVC doesn't allow JSON GET requests by default. Here is the jQuery code:

<script type="text/javascript" src="/Scripts/jquery-1.5.1.min.js"></script>
<script type="text/javascript">
    function getCities(abbr) {
        $.ajax({
            url: "@Url.Action("Cities", "Locations")",
            data: {abbreviation: abbr},
            dataType: "json",
            type: "POST",
            error: function() {
                alert("An error occurred.");
            },
            success: function(data) {
                var items = "";
                $.each(data, function(i, item) {
                    items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
                });

                $("#City").html(items);
            }
        });
    }

    $(document).ready(function(){
        $("#State").change(function() {
            var abbr = $("#State").val();

            getCities(abbr);
        });
    });
</script>

That is all the code that is required to get a cascading dropdown. The jQuery code sets up an event handler that will run whenever the selected item is changed, making an AJAX call that is expecting JSON formatted data. When the AJAX call successfully completes it iterates over the collection of objects building an <option> element for each one and sets the HTML of the dropdown to the new list of items. It would be trivial to replace the code in the repository to retrieve data from a database as well, but I will leave that as an exercise for the reader.

SEO Friendly Routes with ASP.net MVC 3

by Travis Ellis 14. March 2011 16:35

One of the most important things for a modern day website is for it to be easily found. If nobody can find your website, then it isn't very useful. One thing you can do to help is by creating URLs that contain useful information. Google will usually rank a site higher with a URL like http://www.northwind.com/products/3/aniseed-syrup compared to http://www.northwind.com/products/3. In addition to being ranked higher by search engines, it is a lot easier for humans to remember as well. I am going to show you an easy way to create these SEO-style routes for your ASP.net MVC 3 applications.

The first thing we need to do in order to get this to work is create a route that will match this pattern:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(
        "ProductDetails",
        "products/{productId}/{productName}", // URL
        new { controller = "Products", action = "Details", productName = UrlParameter.Optional }, // URL Defaults
        new { id = @"\d+" } // URL Constraints
    );
}

What this does is register a route in our application that matches the pattern /products/id/ and optionally /products/id/name. The product name is optional but the id is required and must be an integer.

Now that we have our route in place we need to create the controller that will handle requests that match the route:

public class ProductsController : Controller
{
    public ActionResult Index(int id, string productName)
    {
        // retrieve the product from the database
        Product product = db.Products.Single(p => p.ProductID == id);

        return View(product);
    }
}

This controller action will take in an id and a product name, retrieve a product based on the id and then return the view, passing in the Product data. You might have noticed that the controller isn't using the productName at the moment. Currently, if we have a product that contains spaces, the URL will encode the string like so:

http://www.northwind.com/products/3/aniseed%20syrup

This is far from ideal. Let's go ahead and fix that problem.

public static class StringHelpers
{
    public static string ToSeoUrl(this string url)
    {
        // make the url lowercase
        string encodedUrl = (url ?? "").ToLower();

        // replace & with and
        encodedUrl = Regex.Replace(encodedUrl, @"\&+", "and");

        // remove characters
        encodedUrl = encodedUrl.Replace("'", "");

        // remove invalid characters
        encodedUrl = Regex.Replace(encodedUrl, @"[^a-z0-9]", "-");

        // remove duplicates
        encodedUrl = Regex.Replace(encodedUrl, @"-+", "-");

        // trim leading & trailing characters
        encodedUrl = encodedUrl.Trim('-');

        return encodedUrl;
    }
}

The above code creates an extension method for the string type and returns an "SEO-encoded" URL using the following steps:

  1. Convert the string to lowercase.
  2. Replace the & symbol with the word and
  3. Remove unwanted characters by replacing with a space (currently only ')
  4. Replace all other characters other than letters/numbers with a -
  5. Remove duplicate -'s from the string (we don't want aniseed--syrup, etc.)
  6. Remove leading/trailing -'s from the string

Now, a string like "AnIseeD Syrup" will be converted to "aniseed-syrup". Now we can modify our controller to make use of this functionality.

public class ProductsController : Controller
{
    public ActionResult Index(int id, string productName)
    {
        // retrieve the product from the database
        Product product = db.Products.Single(p => p.ProductID == id);

        // make sure the productName for the route matches the encoded product name
        string expectedName = product.ProductName.ToSeoUrl();
        string actualName = (productName ?? "").ToLower();

        // permanently redirect to the correct URL
        if( expectedName != actualName )
        {
            return RedirectToActionPermanent("Index", "Products", new { id = product.ProductID, productName = expectedName });
        }
        
        return View(product);
    }
}

This updated controller will do an HTTP 301 redirect if the product name in the URL does not match what it should be, or if it wasn't provided at all. A 301 redirect will tell a search engine to permanently re-index the content. If the user types http://www.northwind.com/products/3 or http://www.northwind.com/products/3/aniseed or any other URL other than http://www.northwind.com/products/3/aniseed-syrup then it will automatically redirect them to the correct one.

The final piece to wire this all up is to make sure your views are creating the proper links. We don't want the requests to redirect by default, otherwise every request will make two calls to the database. Here I am going to use the new MVC 3 View Engine Razor, but it could be done just as easily using the Web Forms View Engine.

@model IEnumerable<Models.Product>
@using Mvc.Helpers

<table>
   <tr>
      <th>Edit</th>
      <th>Product Name</th>
   </tr>
   
   @foreach( var product in Model )
   {
      <tr>
         <td>@Html.ActionLink("Edit", "Details", new { id = product.ProductID, productName = product.ProductName.ToSeoUrl() })</td>
         <td>@product.ProductName</td>
      </tr>
   }
</table>

That's all you need to do. Create an ActionLink and make sure you pass the productName value by calling our ToSeoUrl() helper. Hopefully you found this technique as helpful as I did.

Table-Valued Parameters in SQL Server 2008

by Travis Ellis 25. October 2010 21:30

I recently worked on a project for a client where we needed to import a lot of data into a new database schema. The application was written to insert a single record at a time. When I first tested the application this was not a problem because I was testing everything locally inside a virtual machine. When we deployed the database to a production server the application was taking a lot longer to import the data because I was connected over a VPN connection. I decided to look for some alternatives to speed up my code and prototyped the code using table-valued parameters.

Table-Valued Parameters were introduced in SQL Server 2008 as a way to pass in multiple rows of strongly-typed data into a stored procedure or function. In order to pass the data into a stored procedure you must first define the data type:


CREATE TYPE dbo.EmployeeType AS TABLE
(
   EmployeeID INT,
   FirstName NVARCHAR(50),
   LastName NVARCHAR(50),
   JobTitle NVARCHAR(100)
)

If you execute this statement in SQL Server Management Studio (SSMS) you can find under the Programmability->Types->User-Defined Table Types section.

Once you have the type defined you can create the stored procedure that uses the type:


CREATE PROCEDURE [dbo].[Employee_Insert_Bulk]
   @Employees dbo.EmployeeType READONLY
AS
BEGIN
    MERGE dbo.Employee AS e
    USING
    (
        SELECT EmployeeID,
               FirstName,
               LastName,
               JobTitle
        FROM @Employees
    ) AS et
    ON (e.EmployeeID = et.EmployeeID)
    WHEN MATCHED THEN
        UPDATE SET e.FirstName = et.FirstName,
                   e.LastName = et.LastName,
                   e.JobTitle = et.JobTitle
    WHEN NOT MATCHED THEN
        INSERT (
            FirstName,
            LastName,
            JobTitle,
        )
        VALUES (
            et.FirstName,
            et.LastName,
            et.JobTitle
        );         
END

The above code accepts our dbo.EmployeeType as a parameter to the procedure and then does a merge statement on our Employee table. When it finds a record in the table that is passed into the stored procedure it will update the Employee table. When it does not find a match then it will insert a new record.

Now that we have a stored procedure that will accept a table as a parameter, we need to create some code that will call the stored procedure. This is pretty easy to do in ADO.net:


// create the table
DataTable table = new DataTable();
table.Columns.Add("EmployeeID", typeof(int));
table.Columns.Add("FirstName", typeof(string));
table.Columns.Add("LastName", typeof(string));
table.Columns.Add("JobTitle", typeof(string));

// populate the table
// ...

// create a connection to the database
using( var connection = new SqlConnection(connectionString) )
{
   // create a command to execute our procedure
   SqlCommand command = connection.CreateCommand();
   command.CommandText = "dbo.Employee_Insert_Bulk";
   command.CommandType = CommandType.StoredProcedure;

   SqlParameter param = command.Parameters.AddWithValue("@Employees", table);
   param.SqlDbType = SqlDbType.Structured;
   param.TypeName = "dbo.EmployeeType";

   // open the connection and execute the command
   conn.Open();
   command.ExecuteNonQuery();
}

First we create a DataTable with the same columns as our user defined type. You would then call the code to populate the table (which was left out to save space). Next I set up an ADO.net connection to the database and create a SqlCommand that has a single parameter of type SqlDbType.Structured. I set the TypeName of the parameter and pass in the value of our table and execute the query. That is all you need to do call a stored procedure in ADO.net.

I mentioned initially that I looked at using table-valued parameters to speed up my database code so I decided to run a few benchmark tests. I used a few different methods to test the inserts in my database:

  • Inserts with SqlDataAdapter
  • Batch Inserts with SqlDataAdapter
  • Table-Valued Parameters

The first method uses a SqlDataAdapter to do the inserts. I created a SqlDataAdapter and call the Update method passing in a DataTable with all of the rows in the Added state. This will cause the Data Adapter to call the InsertCommand for each row in the DataTable which calls a stored procedure that will insert a single record into the database.

The second method is the same as the first method except it uses the UpdateBatchSize property introduced in .NET 2.0. The UpdateBatchSize property will send the commands in batches to the server instead of making a round trip for each command.

The final method passes in the data using a table-valued parameter described earlier in this post.

I ran my tests using a few different sample sizes:

  • 10,000 rows
  • 50,000 rows
  • 100,000 rows

Here are the results of the test:

Method 1 - Using SqlDataAdapter:
10,000 rows - average of around 7 seconds
50,000 rows - average of around 5 seconds
100,000 rows - average of around 1 minute 15 seconds

Method 2 - Using SqlDataAdapter with Batch Updates:
10,000 rows - average of around 5 seconds
50,000 rows - average of around 32 seconds
100,000 rows - average of around 50 seconds

Method 3 - Using Table Valued Parameters:
10,000 rows - average of less than 1 second
50,000 rows - average of around 1 second
100,000 rows - average of around 2 seconds

As you can see the results are pretty impressive. Table-valued parameters are a great new feature of SQL Server 2008 and seem to be a good fit for situations where you need to insert a lot of data in bulk.