首页 > 杂七杂八 > SqlHelper之ExecuteScalar方法
网站建设、切图、企业建站、微信开发 联系QQ:404658186 tel:18716186715
2015
06-27

SqlHelper之ExecuteScalar方法

ExecuteScalar方法是SqlHelper中使用很频繁的操作,它的功能是执行查询,并返回查询所返回的结果集中第一行的第一列,所有其他的列和行会被忽略。

返回值: 类型: System.Object,结果集中第一行的第一列。

SqlHelper.ExecuteScalar程序源码

/// <summary>
 /// Execute a SqlCommand (that returns a 1x1 resultset) against the specified SqlConnection 
 /// using the provided parameters.
 /// </summary>
 /// <remarks>
 /// e.g.: 
 /// int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));
 /// </remarks>
 /// <param name="connection">A valid SqlConnection</param>
 /// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
 /// <param name="commandText">The stored procedure name or T-SQL command</param>
 /// <param name="commandParameters">An array of SqlParamters used to execute the command</param>
 /// <returns>An object containing the value in the 1x1 resultset generated by the command</returns>
public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
        {
            if (connection == null) throw new ArgumentNullException("connection");

            // Create a command and prepare it for execution
            SqlCommand cmd = new SqlCommand();

            bool mustCloseConnection = false;
            PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);

            // 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();

            if (mustCloseConnection)
                connection.Close();

            return retval;
        }

使用方法

using (SqlConnection connection = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
            {
                try
                {
                    obj = SqlHelper.ExecuteScalar(connection, CommandType.StoredProcedure, "proc_SelectColumn", para); //存储过程使用方式
                    obj = SqlHelper.ExecuteScalar(connection,CommandType.Text,"select name from table_users where id=12"); //sql字符串使用方式
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
            }

说明,上面存储过程使用方法涉及到一个para,下面给出一个para示例

SqlParameter[] para = new SqlParameter[] 
{
     new SqlParameter("@tableName",SqlDbType.VarChar,50),
     new SqlParameter("@Columns",SqlDbType.VarChar,1000),
     new SqlParameter("@condition",SqlDbType.VarChar,500),
     new SqlParameter("@rows",SqlDbType.VarChar,50),
     new SqlParameter("@order",SqlDbType.VarChar,555) 
};
     para[0].Value = tableName;
     para[1].Value = Columns;
     para[2].Value = condition;
     para[3].Value = rows;
     para[4].Value = order;
最后编辑:
作者:码农一号
这个作者貌似有点懒,什么都没有留下。
捐 赠如果您觉得这篇文章有用处,请支持作者!鼓励作者写出更好更多的文章!

留下一个回复

你的email不会被公开。

*