如何将类型化数据表作为输出参数传递给助手方法

本文关键字:参数传递 方法 输出 类型化 数据表 | 更新日期: 2023-09-27 18:18:46

我正在学习类型化数据集,

有一个助手方法,它接受过程名&返回数据表

public static DataTable ExecuteProcedureReturnDataTable(string procedureName, SqlParameter[] prmArray = null)
        {
            DataTable dt = new DataTable();
            dt = null;
            using (SqlConnection connection = new SqlConnection(Common.GetConnectionString()))
            {
                SqlCommand command = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter(command);
                DataSet ds = new DataSet();
                try
                {
                    // attempt to open sql connection and exec command
                    connection.Open();
                    command.Connection = connection;
                    command.CommandText = procedureName;
                    command.CommandType = CommandType.StoredProcedure;
                    // add parameters to command if they exist
                    if (prmArray != null)
                    {
                        foreach (SqlParameter p in prmArray)
                        {
                            command.Parameters.AddWithValue(p.ParameterName, p.Value);
                        }
                    }
                    da.Fill(ds);
                    //Check whether there is table in dataset
                    if (ds != null )
                    {
                        if (ds.Tables.Count > 0)
                        {
                            dt = ds.Tables[0];
                        }
                    }
                }
                catch (SqlException exSql)
                {
                    EventLogging.LogEvent("Exception", exSql.ToString());
                    dt = null;
                }
                catch (Exception ex)
                {
                    EventLogging.LogEvent("Exception", ex.ToString());
                    dt = null;
                }
                finally
                {
                    command.Dispose();
                    connection.Dispose();
                }
            }
            return dt;
        }

我有3-4个不同的数据表在我的类型化数据集下,我想要一个辅助函数为我所有的类型化数据表工作。我怎么能传递类型化数据表作为输出参数到这个函数?我也怀疑,这可能吗?

如何将类型化数据表作为输出参数传递给助手方法

将方法设为泛型。这样它就可以返回输入的DataTable。您应该在您的方法中添加DataTable, new()约束。

public static T ExecuteProcedureReturnDataTable<T>(string procedureName, SqlParameter[] prmArray = null) where T : DataTable, new()
{
    T dt = new T();
    using (SqlConnection connection = new SqlConnection(Common.GetConnectionString()))
    {
        SqlCommand command = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter(command);
        try
        {
            // attempt to open sql connection and exec command
            connection.Open();
            command.Connection = connection;
            command.CommandText = procedureName;
            command.CommandType = CommandType.StoredProcedure;
            // add parameters to command if they exist
            if (prmArray != null)
            {
                foreach (SqlParameter p in prmArray)
                {
                    command.Parameters.AddWithValue(p.ParameterName, p.Value);
                }
            }
            da.Fill(dt);
        }
        catch (SqlException exSql)
        {
            EventLogging.LogEvent("Exception", exSql.ToString());
            dt = null;
        }
        catch (Exception ex)
        {
            EventLogging.LogEvent("Exception", ex.ToString());
            dt = null;
        }
        finally
        {
            command.Dispose();
            connection.Dispose();
        }
    }
    return dt;
}

则称其为:

var table = ExecuteProcedureReturnDataTable<YourDataSet.YourTable>(...);