执行 SqlCommand 会完全 moot/redundant 并减慢速度吗?

Would executing the SqlCommand be completely moot/redundant, and slow things down?

多亏了一些提示和提醒 here,我更改了我的代码,摆脱了这个乱七八糟的问题:

try
{
    DataSet dsUsage = new DataSet();

    SqlConnection conn = new SqlConnection("SERVER=PROSQL05;DATABASE=platypusdata;UID=duckbill;PWD=poisonToe42;Connection Timeout=0");

    SqlDataAdapter da = new SqlDataAdapter();

    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = String.Format("Exec sp_ViewProductUsage_MappingRS '{0}', '{1}', '{2}'", mammal, dateBegin, dateEnd);
    da.SelectCommand = cmd;

    conn.Open();
    da.Fill(dsUsage);
    conn.Close();

    DataTable dtUsage = dsUsage.Tables[0];

    if (dtUsage.Rows.Count > 0)
    {
        foreach (DataRow productUsageByMonthDataRow in dtUsage.Rows)
        {
            . . .

...为此:

try
{
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet dsUsage = new DataSet();

    using (SqlConnection conn = new SqlConnection(UsageRptConstsAndUtils.PlatypusConnStr))
    {
        using (SqlCommand cmd = new SqlCommand("sp_ViewProductUsage_MappingRS", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@Unit", SqlDbType.VarChar).Value = _unit;
            cmd.Parameters.Add("@BegDate", SqlDbType.DateTime).Value = dtBegin;
            cmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = dtEnd;

            da.SelectCommand = cmd;

            conn.Open();
            //cmd.ExecuteReader(); <- Is this even necessary?
            da.Fill(dsUsage);
        }
    }

    DataTable dtUsage = dsUsage.Tables[0];

    if (dtUsage.Rows.Count > 0)
    {
        // Populate the cells
        foreach (DataRow productUsageByMonthDataRow in dtUsage.Rows)
        {
            . . .

请注意,我在新代码中注释掉了 SqlCommandExecuteReader,因为 SqlDataAdapter 被提供给 SqlCommand,这似乎是不必要的。它工作正常。所以:我假设我可以完全删除 cmd.ExecuteReader() 是否正确?保留它有什么好处,还是完全多余并为流程创建 "busy work"?

更新

因此,要传递一个 SqlParameter 数组(到 MethodMan 的回答中的 ExecuteDataSet 方法),我认为我首先必须执行如下操作:

SqlParameter sqlp = new SqlParameter();
sqlp.ParameterName = "Unit";
sqlp.Value = _unit;
cmd.Parameters.Add(sqlp);

...等(然后将它们添加到数组中 - 或者,可能更好的是 SqlParameter 的通用列表)。

更新 2

我只是 运行 第一次接触这个:如果您使用 MethodMan 的示例(我就是这样做的)并且您使用无参数查询,您需要像这样绕过参数添加循环:

if (null != parameters)
{
    foreach (var item in parameters)
    {
        cmd.Parameters.Add(item);
    }
}

我会亲自创建一个 SqlDBHelper class 并使用这样的方法传递调用存储过程

public static class SqlDBHelper
{
    public static DataSet ExecuteDataSet(string sql, CommandType cmdType, params SqlParameter[] parameters)
    {
        using (DataSet ds = new DataSet())
        using (SqlConnection connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConn"].ConnectionString))
        using (SqlCommand cmd = new SqlCommand(sql, connStr))
        {
            cmd.CommandType = cmdType;
            foreach (var item in parameters)
            {
                cmd.Parameters.Add(item);
            }

            try
            {
                cmd.Connection.Open();
                new SqlDataAdapter(cmd).Fill(ds);
            }
            catch (SqlException ex)
            {
                //log to a file or write to Console for example
                Console.WriteLine(ex.Message);
            }
            return ds;
        }
    }
}

If you want to return a DataTable then change the return type in the Method signature and call the following in the return statement below

public static DataTable ExecuteDataSet(string sql, CommandType cmdType, params SqlParameter[] parameters)

return ds.Tables[0];

Here is an example on how you would call the method

someDataTable = SqlDBHelper.ExecuteDataSet("sp_ViewProductUsage_MappingRS", CommandType.StoredProcedure,
            new SqlParameter() { ParameterName = "@Unit", SqlDbType = SqlDbType.VarChar, Value = _unit },
            new SqlParameter() { ParameterName = "@BegDate", SqlDbType = SqlDbType.DateTime, Value = dtBegin },
            new SqlParameter() { ParameterName = "@EndDate", SqlDbType = SqlDbType.DateTime, Value = dtEnd }
            );