Using Stored Procedures and Parameters.AddWithValue
The code is pretty basic. Call GetCode with the stored procedure name and a connection string. The code reads INFORMATION_SCHEMA.PARAMETERS and fills in the ADO.NET AddWithValue calls in the template string. The results are returned and copied to the clipboard for convenience.
Regex.Unescape is used to take into account the \t (tab) and \r\n (carriage return newlines) for pagination purposes, and the Log property can be assigned Console.Out to display the output of the class—that is, tell you what the class is doing.
With a little extra work, you could add parameter directions, data types, and lengths and take into account elements such as readers, adapters, and DataSets. I hope you have fun playing with the code.
From Alice in Wonderland, Lewis Carroll wrote "Begin at the beginning and go on till you come to the end; then stop." Everyone is at different phases of their programming journey. I hope some basic ADO.NET code helps those new to ADO.NET and that the Tip and sample at the end gave the rest of you a reason to read, too. Thanks for playing.
About the Author
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his upcoming book LINQ Unleashed for C#; preorder your copy today at Amazon.com. Paul Kimmel is an Application Architect for EDS. You may contact him for technology questions at firstname.lastname@example.org.
Copyright © 2008 by Paul T. Kimmel. All Rights Reserved.
Page 3 of 3