一般传递参数

时间:2013-11-19 18:24:26

标签: c# sql parameters parameter-passing

我想创建一个通用的数据库类,所以我们所有的开发人员都可以通过导入数据库dll来遵循完全相同的语法。话虽这么说,我有点坚持如何完善这一部分。我需要找到一种通过此方法传递未知数量参数的方法。我试着做一个清单,但似乎没有随处可见。同样为了清楚起见,db.ValidateConnection(连接)部分只是确保连接尚未使用或打开,然后打开它。

想要调用此方法:

public static SqlDataReader ExecuteReader(string CommandName, 
                                          SqlConnection Connection,
                                          PARAMETERS??!?!?)
{
    using (SqlCommand cmd = new SqlCommand(CommandName, Connection))
    {
        // Ensure we are executing a stored procedure.
        cmd.CommandType = CommandType.StoredProcedure;

        // Ensure our connection is not already open and then open it.
        db.ValidateConnection(Connection);

        // Return our SqlDataReader object with the desired execution results.
        return cmd.ExecuteReader();
    }
}

有了这个代码的想法:

// Want to add these Parameters..//
    cmd.Parameters.AddWithValue("@Param0", Param0);
    cmd.Parameters.AddWithValue("@Param1", Param1);
    cmd.Parameters.AddWithValue("@Param2", Param2);

    // Or These Parameter...
    cmd.Parameters.AddWithValue("@Param2", Param2);

    // Or NO Parameters...

    // To this Execution statement.
    ExecuteReader(CommandName: "[MyStoredProcedure]",
                  Connection: "MyConnection",
                  PARAMTERS??!?!?!: "ListOfAllParametersToAttach")

- UPDATE

最终工作代码!在我们的.dll中调用:

 public static SqlDataReader ExecuteReader(string CommandName,
                                          SqlConnection Connection,
                                          params SqlParameter[] Parameters)
{
    // ExecuteReader
    //
    // @CommandName string :
    //     Name of the stored procedure we are looking to execute.
    //
    // @Connection SqlConnection :
    //     SQL Connection to be used when executing the provided @CommandName.
    //
    // @Parameters params SqlParameter[] :
    //     If any parameters exist this will contain the list of we will attach to our SqlCommand.

    using (SqlCommand cmd = new SqlCommand(CommandName, Connection))
    {
        // Ensure we are executing a stored procedure.
        cmd.CommandType = CommandType.StoredProcedure;

        // Iterate through our list and add all required parameters.
        foreach (SqlParameter Parameter in Parameters)
        {
            cmd.Parameters.Add(Parameter);
        }

        // Ensure our connection is not already open and then open it.
        db.ValidateConnection(Connection);

        // Return our SqlDataReader object with the desired execution results.
        return cmd.ExecuteReader();
    }
}

从应用程序调用:

using (SqlDataReader dr = SMCConnect.ExecuteReader("[ProcedureName]",
                                                    con,
                                                    new SqlParameter("@ParameterName", ParameterName))) { }

3 个答案:

答案 0 :(得分:2)

我使用params SqlParameter[]。这被称为“可变长度参数”或Variadic

public static SqlDataReader ExecuteReader(string CommandName,
                                  SqlConnection Connection,
                                  params SqlParameter[] parameters)
{
    using (SqlCommand cmd = new SqlCommand(CommandName, Connection))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        foreach (var param in parameters)
        {
            cmd.Parameters.Add(param);
        }
        //Do something..                
        return cmd.ExecuteReader();
    }
}

用例:

ExecuteReader("MyStoredProcedure",myConnection,new SqlParameter("@param1",value1),new SqlParameter("@param2",value2),...);

答案 1 :(得分:1)

在签名中使用params[] KeyValuePair<string, object>,如下所示:

public static SqlDataReader ExecuteReader(string CommandName, 
                                      SqlConnection Connection,
                                      params[] KeyValuePair<string, object> parameters)
//...
foreach (var p in parameters)
   cmd.Parameters.Add(p.Key, p.Value)
//...

答案 2 :(得分:0)

public static SqlDataReader ExecuteReader(string CommandName, 
                                          SqlConnection Connection,
                                          Dictionary<string, object> parameters)

然后

foreach (var key in parameters.Keys)
{
  cmd.Parameters.AddWithValue(key, parameters[key]);
}