Blog ( Of Ryan Olshan)

News

About Ryan Olshan

LinkedIn Profile

Follow Ryan via twitter

Join StrongTypes - Free Generics Help By Email
Join StrongTypes

Community Credit Winner

View my MVP profile

The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

Subscribe to Rolog via My AOL
Subscribe to Rolog via Bloglines


Subscribe to Rolog via Feedburner

Furl Rolog




Subscribe to Rolog via Newsburst


Subscribe to Rolog via Pluck

Add Rolog to Technorati Favorites




Thycotic Secret Server

Receive Email Updates


.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • LearnAsp.com
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Charles Carroll
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Doug Nelson
  • Kevin Jones

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

ADO.NET

Generic ExecuteScalar method

Just wanted to share with everyone the generic helper method I use for ExecuteScalar. A helper method for ExecuteScalar is the perfect candidate for generics due to the different types that could be returned. Of course, you could always return an object, but this is more fun. :)

        public static T ExecuteScalar( string commandText, string connectionString)

        {

            return ExecuteScalar(commandText, connectionString, null );

        }

 

        public static T ExecuteScalar( string commandText, string connectionString, params SqlParameter [] parameters)

        {

            T result = default (T);

            if (! string .IsNullOrEmpty(commandText))

            {

                using ( SqlConnection connection = new SqlConnection (connectionString))

                {

                    using ( SqlCommand command = new SqlCommand (commandText, connection))

                    {

                        if (parameters != null && parameters.Length > 0)

                        {

                            foreach ( SqlParameter parameter in parameters)

                            {

                                command.Parameters.Add(parameter);

                            }

                        }

                        connection.Open();

                        result = (T)command.ExecuteScalar();

                        connection.Close();

                    }

                }

            }

            return result;

        }

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Jan 28 2007, 05:45 PM by Ryan Olshan | with 1 comment(s)
Filed under: ,
What's new in ADO.NET 3.0?

Yesterday, a video was posted to Channel 9 called What's coming in ADO.NET?  Pretty awesome stuff.

UPDATE: It turns out they released this video prematurely, so the link doesn't work anymore.

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: May 11 2006, 09:51 AM by Ryan Olshan | with 2 comment(s)
Filed under:
More Posts