c# - 调用存储过程时尽量减少代码重复

标签 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
             }

         }
     }
 }

让我烦恼的是,对于我调用的每个不同过程,上面的大部分代码在我的 cs 文件中一次又一次地重复。

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

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

最佳答案

更新

我知道这是一个非常古老的问题(事实上,我只是在搜索另一个旧答案时偶然发现了它,我给别人关闭了重复),但我最近 released a git hub project这非常需要。 它通过封装连接、命令、参数和数据适配器,最大限度地减少使用 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; }


}

执行方法(基于您发布的方法):

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

调用示例:

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

关于c# - 调用存储过程时尽量减少代码重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37407710/

相关文章:

c# - 使用 Microsoft.WindowsAzure.Storage (2.0.3.0) 找不到方法

javascript - 服务器端数据表分页无法正常工作

c# - 加密 Web.Config

ASP.NET:如何正确重定向出现 404 错误的请求?

c# - MVC 4 网络 API : returning tasks from actions

C# 和 SQL Server : any reason to put a single INSERT into a transaction?

c# - 在 asp.net 中选择列表框中的所有项目

c# - Azure WebJob 运行后更新 UI

c# - ASP.NET - Razor 从模型中获取图像

c# - Raspberry Pi 和单声道帧缓冲区输入