如何将类型化的DataTable作为输出参数传递给helper方法

时间:2014-08-20 12:04:11

标签: c# datatable ado.net strongly-typed-dataset

我正在学习Typed Dataset,

有一个辅助方法,它采用程序名称&返回DataTable,

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;
        }

我的Typed DataSet下有3-4个不同的DataTables,我希望Helper函数适用于我所有类型的数据表。 如何将Typed DataTable作为输出参数传递给此函数? 我也怀疑,这可能吗?

1 个答案:

答案 0 :(得分:1)

使方法通用。这样它就可以返回您输入的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>(...);