简体   繁体   English

如何使用对象数组创建可选参数

[英]How to create optional parameters using object array

I am creating a method which MUST be use with or without parameters. 我正在创建必须与参数一起使用或不与参数一起使用的方法。 I am using object array so that I can hold string, integer, binary types, etc at the same time. 我正在使用对象数组,以便可以同时容纳字符串,整数,二进制类型等。

Method: SQLDB_UsingReader(string strSQL_WithParam, params object[,] obj) 方法: SQLDB_UsingReader(字符串strSQL_WithParam,params object [,] obj)

Error: The array must be a single dimensional array. 错误:数组必须是一维数组。

PS This method SQLDB_UsingReader(string strSQL_WithParam, object[,] obj) is working, but when I add " params " as the solution I searched when creating optional parameter, the error occurs. PS此方法SQLDB_UsingReader(string strSQL_WithParam,object [,] obj)有效,但是当我在创建可选参数时添加“ params ”作为搜索的解决方案时,会发生错误。

CODE

public void SQLDB_UsingReader(string strSQL_WithParam, params object[,] obj)
    {
        try
        {
            using (SqlCommand mCmd = new SqlCommand(strSQL_WithParam, mConn))
            {
                for (int i = 0; i < obj.Length / 2; i++)
                {
                    if (obj[i, 1] == null || obj[i, 1].ToString() == "" || obj[i, 1].ToString().Length == 0)
                    { mCmd.Parameters.Add(new SqlParameter(obj[i, 0].ToString(), DBNull.Value)); }
                    else
                    { mCmd.Parameters.Add(new SqlParameter(obj[i, 0].ToString(), obj[i, 1])); }
                }
                mConn.Open();
                mDataReader = mCmd.ExecuteReader();
                mConn.Close();
            }
        }
        catch (Exception ex) { ex.ToString(); }
    }

Use Dictionary<string, object> . 使用Dictionary<string, object> You have two values: a string and an object per parameter you wish to set. 您有两个值:每个参数要设置的字符串和对象。 Either create a class that has one of each and use params for an array of that class or simply use the built in type for a collection of named values. 创建一个每个都有一个的类,并为该类的数组使用参数,或者仅将内置类型用于命名值的集合。

public void SQLDB_UsingReader(string strSQL_WithParam, IDictionary<string, object> obj)
{
    try
    {
        string where = obj.Any() ? ("where " + string.Join("AND", obj
                                   .Select(x => x.Key +"==@" + x.Key)) : "";
        using (SqlCommand mCmd = new SqlCommand(strSQL_WithParam + where, mConn))
        {
            foreach pair in obj
            {
                ... (use pair.Value and pair.Key)
            }
            ...
        }
    }
    ...
}

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

相关问题 可选参数以及一系列参数 - Optional parameters together with an array of parameters 如何使用带有可选参数的WebAPI 2创建真正的多用途端点? - How do I create a truly multi-purpose endpoint using WebAPI 2 with optional parameters? 如何使用反射创建具有已定义参数的通用对象 - How can I create a generic object with defined parameters using reflection 如何使用XML-RPC.NET为方法定义可选参数 - How to define optional parameters for a method using XML-RPC.NET 如何使用反射调用带有可选参数的方法 c# - How to call Method with optional parameters using reflections c# 如何创建对象数组? - How to create an array of object? C# 尝试从方法内部使用可选参数创建全局 object 实例 - C# Trying to create a global object instance with optional parameters from inside of a method 如何使用structuremap asp.net mvc注册带有可选参数的可选装饰器或装饰器? - How to register an optional decorator or decorator with optional parameters using structuremap asp.net mvc? 使用可选参数创建功能Visual Studio 2010 - create function visual studio 2010 with optional parameters 如何创建返回具有特定参数的 object 的工厂? - How to create a Factory that returns an object with specific parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM