调用存储过程时最小化代码重复

Иво*_*дев 7 c# asp.net

我使用某个方法体来调用存储过程,使用以下示例代码:

     public void StoredProcedureThatIsBeingcalled(int variable_1, int variable_2, out DataSet ds)
 {
     using (SqlConnection con = new SqlConnection(DatabaseConnectionString))
     {
         ds = new DataSet("DsToGoOut");
         using (SqlCommand cmd = new SqlCommand("StoredProcedureThatIsBeingcalled", DbConn.objConn))
         {
             cmd.CommandType = CommandType.StoredProcedure;


             cmd.Parameters.Add(new SqlParameter("@variable_1", variable_1));
             cmd.Parameters.Add(new SqlParameter("@variable_2", variable_2));
             try
             {
                 con.Open();
                 SqlDataAdapter objDataAdapter = new SqlDataAdapter();
                 objDataAdapter.SelectCommand = cmd;

                 objDataAdapter.Fill(ds);

                 con.Close();
             }
             catch (Exception ex)
             {

                 //sql_log_err
             }

         }
     }
 }
Run Code Online (Sandbox Code Playgroud)

有什么问题我上面的大部分代码都在我的cs文件中一次又一次地重复我调用的每个不同的过程.

显然,我可以清除它,并使用过程名称作为变量调用一个函数,但是如何为不同的过程提供不同数量的参数(具有不同的数据类型 - int,字符串bool - 从不其他任何东西)我用 ?

我可以使用不同数量的参数(0-10)来实现几个不同的功能,但我觉得有更好的方法吗?

Zoh*_*led 4

更新

我知道这是一个非常古老的问题(事实上,我只是在搜索另一个旧答案时偶然发现了它,我给了其他人作为重复的答案),但我最近发布了一个 git hub 项目来满足这个非常需要。它通过封装连接、命令、参数和数据适配器来最大限度地减少使用 ADO.Net 时的代码重复。
如果您想尝试一下,我很高兴知道您的想法。

第一个版本

您可以使用辅助类来封装 sql 参数并创建一个方法来处理所有数据集填充,如下所示:

辅助类:

private class SqlParamDefinition
{

    public SqlParamDefinition(string name, SqlDbType dbType, object value)
    {
        this.Name = name;
        this.DbType = dbType;
        this.Value = value;
    }

    public string Name { get; }
    public SqlDbType DbType { get; }

    public object Value { get; }


}
Run Code Online (Sandbox Code Playgroud)

执行方法(根据您发布的方法):

public DataSet ExecuteSelectProcedure(string procedeureName, params SqlParamDefinition[] parameters)
{
    var ds = new DataSet();
    using (var con = new SqlConnection(DatabaseConnectionString))
    {

        using (var cmd = new SqlCommand(procedeureName, DbConn.objConn))
        {
            cmd.CommandType = CommandType.StoredProcedure;

            for(int i = 0; i < parameters.Length; i++)
            {
                var param = parameters[i];
                cmd.Parameters.Add(new SqlParameter(param.Name, param.DbType).Value = param.Value);
            }

            try
            {
                con.Open();
                var objDataAdapter = new SqlDataAdapter();
                objDataAdapter.SelectCommand = cmd;

                objDataAdapter.Fill(ds);

                con.Close();
            }
            catch (Exception ex)
            {

                //sql_log_err
            }

        }
    }
    return ds;
}
Run Code Online (Sandbox Code Playgroud)

调用示例:

var parameters = new SqlParamDefinition[]
{
    new SqlParamDefinition("@Param1", SqlDbType.VarChar, "value1"),
    new SqlParamDefinition("@Param2", SqlDbType.VarChar, "value2"),
    new SqlParamDefinition("@Param3", SqlDbType.Int, 123),
};

var ds = ExecuteSelectProcedure("Strong procedure name", parameters);
Run Code Online (Sandbox Code Playgroud)