CSTrüter HomeArticlesDownloadsAbout meContact me
a quick look at how to create a windows service using C# 2010-02-28 21:48:06
How to call server-side code from client-side code, using PageMethods in ASP.net 2010-02-21 12:31:27
How to pass a set of data to an xml type in SQL 2005/8 2010-02-12 19:04:23
Some funky behaviour regarding overload Resolution of dynamic/object types 2010-02-09 17:16:52
Object orientated programming within JavaScript 2010-01-28 07:25:45
How to sort data using ASP.net (C#) and SQL 2005/8 2010-01-18 15:23:14
Quick look at some of the new features added to C# 4.0 2010-01-12 21:52:13
SQL 2008 introduced a nifty feature called Table-Valued Parameters (TVP) into its codebase 2010-01-06 22:58:25
How to page data using ASP.net (C#) and SQL 2005/8 2009-10-19 15:01:45
a post about sql joins 2009-09-20 15:50:57
Creating a WYSIWYG textbox for your website is actually quite simple. 2007-02-01 12:00:00
Move items between two listboxes in ASP.net(C#, VB.NET) and PHP 2008-06-12 17:07:43
Firefox word wrapping issues 2008-06-09 09:51:21
2007-02-22 12:00:00
Blog about passing parameters by reference to functions using func_get_arg(s) 2008-07-27 12:38:24
SQL 2008 introduced a nifty feature called Table-Valued Parameters (TVP) into its codebase. TVPs allow developers to pass sets of data to a stored procedure (i.e. passing a table to a stored procedure parameter). The first thing we need to do, is define a table type, like this: CREATE TYPE [dbo].[MyFriends] AS TABLE( [firstname] [varchar](50) NOT NULL, [lastname] [varchar](50) NOT NULL ) Create a little table for our testing purposes: CREATE TABLE [dbo].[friends]( [friendID] [int] IDENTITY(1,1) NOT NULL, [firstname] [varchar](50) NOT NULL, [lastname] [varchar](50) NOT NULL, CONSTRAINT [PK_friends] PRIMARY KEY CLUSTERED ( [friendID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] Next we create a stored procedure using our newly created table type. Note a TVP must be passed as READONLY - which means you can't modify(insert/update/delete rows) it within the body of the routine you're passing it to. CREATE PROCEDURE [dbo].[addFriends] @friends MyFriends READONLY AS BEGIN INSERT INTO friends(firstname, lastname) SELECT firstname, lastname FROM @friends END In our C# snippet we create a DataTable, add some rows to it, and pass it to our stored procedure: using (SqlConnection connection = new SqlConnection(@"Some connectionString")) { using (SqlCommand command = new SqlCommand("addFriends", connection) { CommandType = CommandType.StoredProcedure }) { DataTable dt = new DataTable(); // Add columns dt.Columns.Add("firstname", typeof(String)).MaxLength = 50; dt.Columns.Add("lastname", typeof(String)).MaxLength = 50; ; // Add some rows dt.Rows.Add(new Object[] { "Eugene", "Stander" }); dt.Rows.Add(new Object[] { "Roland", "Cooper" }); dt.Rows.Add(new Object[] { "Jacques", "Brits" }); connection.Open(); command.Parameters.Add("@friends", SqlDbType.Structured).Value = dt; command.ExecuteNonQuery(); } } How does all of this affect performance though? A few clues can be found at http://technet.microsoft.com/en-us/library/bb510489.aspx Summarized in the case of bulk inserts vs TVPs; a formatted data file on the server will perform better using a bulk insert, unless it is a complex operation with less than 1000 rows. Using a remote client process TVPs will usually perform better, unless its a direct insert with more than 1000 rows.
CREATE TYPE [dbo].[MyFriends] AS TABLE( [firstname] [varchar](50) NOT NULL, [lastname] [varchar](50) NOT NULL )
CREATE TABLE [dbo].[friends]( [friendID] [int] IDENTITY(1,1) NOT NULL, [firstname] [varchar](50) NOT NULL, [lastname] [varchar](50) NOT NULL, CONSTRAINT [PK_friends] PRIMARY KEY CLUSTERED ( [friendID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
CREATE PROCEDURE [dbo].[addFriends] @friends MyFriends READONLY AS BEGIN INSERT INTO friends(firstname, lastname) SELECT firstname, lastname FROM @friends END
using (SqlConnection connection = new SqlConnection(@"Some connectionString")) { using (SqlCommand command = new SqlCommand("addFriends", connection) { CommandType = CommandType.StoredProcedure }) { DataTable dt = new DataTable(); // Add columns dt.Columns.Add("firstname", typeof(String)).MaxLength = 50; dt.Columns.Add("lastname", typeof(String)).MaxLength = 50; ; // Add some rows dt.Rows.Add(new Object[] { "Eugene", "Stander" }); dt.Rows.Add(new Object[] { "Roland", "Cooper" }); dt.Rows.Add(new Object[] { "Jacques", "Brits" }); connection.Open(); command.Parameters.Add("@friends", SqlDbType.Structured).Value = dt; command.ExecuteNonQuery(); } }
Codebooth my semi-community site etc (work in progress)
The company I'am currently working for as software developer.
a Parallel reference of programming languages 2009-09-10 12:48:23
a tutorial explaining how to develop a simple login using PHP and MySQL 2009-09-05 18:26:47
An article looking at adding some kind of event driven model to PHP 5 2008-07-28 12:48:09
It is very simple creating your own rss reader, the following article looks at a few methods of doing this. 2008-06-23 13:18:25
A quick reference about working with dropdown boxes (select element) in javascript. 2007-02-17 16:36:41
Collection of funny programming articles 2006-10-08 14:23:43