博 之 文

以 拼 搏 设 计 梦 想 , 以 恒 心 编 程 明 天
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

增加传值参数,返回SqlParameter[]

Posted on 2012-02-15 14:56  IsNull_Soft  阅读(1465)  评论(0编辑  收藏  举报

/// <summary>        

/// 生成SqlParameter参数        

/// </summary>        

/// <param name="paramStr">多个参数组成的串</param>        

/// <param name="addReturnParam">是否增加返回参数</param>        

/// <param name="values">字段的值一维array</param>        

/// <returns>SqlParameter[]</returns>        

public static SqlParameter[] GetSqlParams(string paramStr, bool addReturnParam, params object[] values)        

{            

        SqlParameter[] arParams = null;            

        if ((paramStr != null) && (paramStr.Trim() != ""))            

   {                

     string[] paramStrs = paramStr.Split(new Char[] { ',' });

               arParams = new SqlParameter[paramStrs.Length];

               int i = 0;                

     foreach (string param in paramStrs)                

      {                    

        if (values[i] == null)                        

        arParams[i] = new SqlParameter(param.Trim(), DBNull.Value);                    

        else                        

        arParams[i] = new SqlParameter(param.Trim(), values[i]);                    

        i++;                

        }            

    }            

    if (addReturnParam)            

    {                

      AddReturnParam(ref arParams, "@ReturnValue", SqlDbType.Int);            

     }            

     return arParams;        

}

 

///AddReturnParam函数

public static void AddReturnParam(ref SqlParameter[] arParams, string paramName, SqlDbType ot)        

{            

  SqlParameter newParam = new SqlParameter(paramName.Trim(), ot, 2000);            

  newParam.Direction = ParameterDirection.ReturnValue;            

  if (arParams == null)            

  {                

    arParams = new SqlParameter[] { newParam };                

    return;            

  }

            SqlParameter[] newParams = new SqlParameter[arParams.Length + 1];            

    for (int i = 0; i < arParams.Length; i++)            

    {                

      newParams[i] = arParams[i];            

    }            

    newParams[arParams.Length] = newParam;

            arParams = newParams;        

 }