Handle multiple db updates from c# in SQL Server 2008

Posted by joeriks on Stack Overflow See other posts from Stack Overflow or by joeriks
Published on 2011-09-25T14:04:15Z Indexed on 2012/09/08 15:38 UTC
Read the original article Hit count: 258

Filed under:
|
|
|
|

I like to find a way to handle multiple updates to a sql db (with one singe db roundtrip). I read about table-valued parameters in SQL Server 2008 http://www.codeproject.com/KB/database/TableValueParameters.aspx which seems really useful. But it seems I need to create both a stored procedure and a table type to use it. Is that true? Perhaps due to security? I would like to run a text query simply like this:

var sql = "INSERT INTO Note (UserId, note) SELECT * FROM @myDataTable";
var myDataTable = ... some System.Data.DataTable ...
var cmd = new System.Data.SqlClient.SqlCommand(sql, conn);
var param = cmd.Parameters.Add("@myDataTable", System.Data.SqlDbType.Structured);
param.Value=myDataTable;
cmd.ExecuteNonQuery();

So

A) do I have to create both a stored procedure and a table type to use TVP's? and
B) what alternative method is recommended to send multiple updates (and inserts) to SQL Server?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET