Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Need advice with ExecuteScalar()

Need advice with ExecuteScalar()

Scheduled Pinned Locked Moved Database
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    akak1997
    wrote on last edited by
    #1

    Hi all the experts in Code Project, I've a problem with returning the value returned by ExecuteScalar function under OleDbCommand. Here is a code sample. ... cmd.CommandText = searchSqlstr; long result = Convert.ToInt64(cmd.ExecuteScalar()); connection.Close(); return result; Is there a way to rewrite this code so that I DO NOT need the 'result' variable? since we always have to close the connection, if I write: return Convert.ToInt64(cmd.ExecuteScalar()); it would be a bad idea right? So is there a trick to do this? Thanks a Lot

    G 1 Reply Last reply
    0
    • A akak1997

      Hi all the experts in Code Project, I've a problem with returning the value returned by ExecuteScalar function under OleDbCommand. Here is a code sample. ... cmd.CommandText = searchSqlstr; long result = Convert.ToInt64(cmd.ExecuteScalar()); connection.Close(); return result; Is there a way to rewrite this code so that I DO NOT need the 'result' variable? since we always have to close the connection, if I write: return Convert.ToInt64(cmd.ExecuteScalar()); it would be a bad idea right? So is there a trick to do this? Thanks a Lot

      G Offline
      G Offline
      Grimolfr
      wrote on last edited by
      #2

      cmd.CommandText = searchSqlstr;

      try
      {
      return Convert.ToInt64(cmd.ExecuteScalar());
      }
      finally
      {
      connection.Close();
      }

      Better yet would be a using(){} statement for the connection object, so that it will automatically be disposed, but since you didn't list all the code from instantiation to disposal, I can't reacreate it accurately.

      using (SqlConnection connection = new SqlConnection(connString))
      {
      connection.Open();

      try
      {
      	SqlCommand cmd = connection.CreateCommand();
      	cmd.CommandType = CommandType.Text;
      	cmd.CommandText = searchSqlstr;
      	return Convert.ToInt64(cmd.ExecuteScalar());
      }
      // Put a catch{} block here, if you need one
      finally
      {
      	connection.Close();
      }
      

      }


      Grim

      (aka Toby)

      MCDBA, MCSD, MCP+SB

      Need a Second Life?[^]

      SELECT * FROM user WHERE clue IS NOT NULL GO

      (0 row(s) affected)

      A 1 Reply Last reply
      0
      • G Grimolfr

        cmd.CommandText = searchSqlstr;

        try
        {
        return Convert.ToInt64(cmd.ExecuteScalar());
        }
        finally
        {
        connection.Close();
        }

        Better yet would be a using(){} statement for the connection object, so that it will automatically be disposed, but since you didn't list all the code from instantiation to disposal, I can't reacreate it accurately.

        using (SqlConnection connection = new SqlConnection(connString))
        {
        connection.Open();

        try
        {
        	SqlCommand cmd = connection.CreateCommand();
        	cmd.CommandType = CommandType.Text;
        	cmd.CommandText = searchSqlstr;
        	return Convert.ToInt64(cmd.ExecuteScalar());
        }
        // Put a catch{} block here, if you need one
        finally
        {
        	connection.Close();
        }
        

        }


        Grim

        (aka Toby)

        MCDBA, MCSD, MCP+SB

        Need a Second Life?[^]

        SELECT * FROM user WHERE clue IS NOT NULL GO

        (0 row(s) affected)

        A Offline
        A Offline
        akak1997
        wrote on last edited by
        #3

        Ah! Great tip! totally forgot about the using statements :D Thanks a Lot!

        S 1 Reply Last reply
        0
        • A akak1997

          Ah! Great tip! totally forgot about the using statements :D Thanks a Lot!

          S Offline
          S Offline
          Steven Campbell
          wrote on last edited by
          #4

          In the example given, the connection.Close(); is redundant, since the using block will take care of that. Which makes the try-finally is also redundant, (unless you want to implement a catch). So a simple version is:

          using (SqlConnection connection = new SqlConnection(connString))
          {
          connection.Open();

          SqlCommand cmd = connection.CreateCommand();
          cmd.CommandType = CommandType.Text;
          cmd.CommandText = searchSqlstr;
          return Convert.ToInt64(cmd.ExecuteScalar());
          

          }


          my blog

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups