简体   繁体   English

如何将键入的DataTable作为输出参数传递给辅助方法

[英]How to pass typed DataTable as output parameter to helper method

I am learning Typed Dataset, 我正在学习类型化数据集,

Have one helper method which takes procedure name & returns DataTable, 有一个使用方法名称并返回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;
        }

I have 3-4 different DataTables under my Typed DataSet, I want a Helper function to work for my all typed datatable. 我的Typed DataSet下有3-4个不同的DataTables,我想要一个Helper函数来为我所有的typed Datatable工作。 How Can I pass Typed DataTable as output parameter to this function? 如何将Typed DataTable作为输出参数传递给此函数? I also doubt, is this possible? 我也怀疑,这可能吗?

Make the method generic. 使该方法通用。 So that it can return your typed DataTable . 这样它就可以返回您键入的DataTable You should add DataTable, new() constraint to your method. 您应该将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;
}

Then call it as: 然后将其称为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM