2009/Study 모임 B조2009. 2. 24. 14:03
#region ExecuteScalar

  /// <summary>
  /// Execute a single command against a MySQL database.
  /// </summary>
  /// <param name="connectionString">Settings to use for the update</param>
  /// <param name="commandText">Command text to use for the update</param>
  /// <returns>The first column of the first row in the result set, or a null reference if the result set is empty.</returns>
  public static object ExecuteScalar(string connectionString, string commandText)
  {
   //pass through the call providing null for the set of MySqlParameters
   return ExecuteScalar(connectionString, commandText, (MySqlParameter[])null);
  }

  /// <summary>
  /// Execute a single command against a MySQL database.
  /// </summary>
  /// <param name="connectionString">Settings to use for the command</param>
  /// <param name="commandText">Command text to use for the command</param>
  /// <param name="commandParameters">Parameters to use for the command</param>
  /// <returns>The first column of the first row in the result set, or a null reference if the result set is empty.</returns>
  public static object ExecuteScalar(string connectionString, string commandText, params MySqlParameter[] commandParameters)
  {
   //create & open a SqlConnection, and dispose of it after we are done.
   using (MySqlConnection cn = new MySqlConnection(connectionString))
   {
    cn.Open();

    //call the overload that takes a connection in place of the connection string
    return ExecuteScalar(cn, commandText, commandParameters);
   }
  }

  /// <summary>
  /// Execute a single command against a MySQL database.
  /// </summary>
  /// <param name="connection"><see cref="MySqlConnection"/> object to use</param>
  /// <param name="commandText">Command text to use for the command</param>
  /// <returns>The first column of the first row in the result set, or a null reference if the result set is empty.</returns>
  public static object ExecuteScalar(MySqlConnection connection, string commandText)
  {
   //pass through the call providing null for the set of MySqlParameters
   return ExecuteScalar(connection, commandText, (MySqlParameter[])null);
  }

  /// <summary>
  /// Execute a single command against a MySQL database.
  /// </summary>
  /// <param name="connection"><see cref="MySqlConnection"/> object to use</param>
  /// <param name="commandText">Command text to use for the command</param>
  /// <param name="commandParameters">Parameters to use for the command</param>
  /// <returns>The first column of the first row in the result set, or a null reference if the result set is empty.</returns>
  public static object ExecuteScalar(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters)
  {
   //create a command and prepare it for execution
   MySqlCommand cmd = new MySqlCommand();
   cmd.Connection = connection;
   cmd.CommandText = commandText;
   cmd.CommandType = CommandType.Text;
   
   if (commandParameters != null)
    foreach (MySqlParameter p in commandParameters)
     cmd.Parameters.Add( p );
   
   //execute the command & return the results
   object retval = cmd.ExecuteScalar();
   
   // detach the SqlParameters from the command object, so they can be used again.
   cmd.Parameters.Clear();
   return retval;
   
  }

  #endregion
 }
}


http://www.koders.com/csharp/fid01EA450A209966FCB52CA84DABB208DEF807D142.aspx
Posted by penguindori