如何返回此对象列表? [英] How Can I return this Object list?

查看:61
本文介绍了如何返回此对象列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个类,它的属性是对象类型,因为它可能被不同的类型(字符串,布尔值,或......)估价

  public   class  paramout 
{
public string param { set ; get ; }
public object value {设置; get ; }
}



我有一个函数,它返回此类的任何存储过程的输出参数。

< pre lang =c#> public paramout ExecProced( String spName,List< InputParameterMapping> mapIn,List< OutputParameterMapping> mapOut)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = Sqlc;
cmd.CommandText = spName;
cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameterCollection objParamColl = cmd.Parameters;
尝试
{
if (mapIn!= null
foreach (ParameterMappingInput map in mapIn)
AddInputParameterToCollection(map.ParameterName,map.Value,objParamColl);
if (mapOut!= null
foreach (ParameterMappingOutput map in mapOut)
AddOutputParameterToCollection(map.ParameterName,map.ValueType,objParamColl);
OpenConnection(Sqlc);
cmd.ExecuteNonQuery(); // 用于插入/删除/更新
CloseConnection(SQLC);
if (mapOut!= null && objParamColl!= null
{
List< paramout> lst = new List< paramout>();
foreach (SqlParameter p in objParamColl)
{
lst。添加( new paramout(){param = p.ParameterName, value = p.Value});
}
return lst; // 错误
}
返回 null ;
}
catch (例外情况)
{
throw ex;
}





错误是:无法将类型'paramout'隐式转换为'System.Collections.Generic .List< paramout>'

解决方案

定义这样的方法

  public   List< paramout>  ExecProced( String  spName,List< InputParameterMapping> mapIn,List< outputparametermapping> mapOut )


请参阅问题的评论。你不能问这样的问题,不能显示相关的代码。



如果你定义

 System.Collections.Generic.List< paramout> lst =  new  System.Collections.Generic.List< paramout>(); 





-SA


there is a class which on of it's properties is object type,because it may valued by Different type(string,boolean,or,...)

public class paramout
{
    public string param { set; get; }
    public object value { set; get; }
}


I have a function which returns output parameters of any Stored Procedure by this class.

public paramout  ExecProced(String spName,List<InputParameterMapping> mapIn,List<OutputParameterMapping> mapOut)
      {
          SqlCommand cmd = new SqlCommand();
          cmd.Connection = Sqlc;
          cmd.CommandText = spName;
          cmd.CommandType = System.Data.CommandType.StoredProcedure;
          SqlParameterCollection objParamColl = cmd.Parameters;
          try
          {
              if (mapIn != null)
                  foreach (ParameterMappingInput map in mapIn)
                      AddInputParameterToCollection(map.ParameterName, map.Value, objParamColl);
              if(mapOut!=null)
                  foreach(ParameterMappingOutput  map in mapOut)
                      AddOutputParameterToCollection(map.ParameterName,map.ValueType,objParamColl);
              OpenConnection(Sqlc);
              cmd.ExecuteNonQuery();// for insert/delete/update
              CloseConnection(Sqlc);
              if(mapOut!=null && objParamColl!=null)
              {
                  List<paramout> lst=new List<paramout>();
                  foreach(SqlParameter p in objParamColl)
                  {
                      lst.Add(new paramout() { param = p.ParameterName, value =p.Value});
                  }
                  return lst; //error
              }
                 return null;
          }
          catch (Exception ex)
          {
              throw ex;
          }



the error is:Cannot implicitly convert type 'paramout' to'System.Collections.Generic.List<paramout>'

解决方案

define the method like this

public List<paramout> ExecProced(String spName,List<InputParameterMapping> mapIn,List<outputparametermapping> mapOut)


Please see comments to the question. You cannot ask questions like that, not showing relevant code.

It will work if you, for example, define

System.Collections.Generic.List<paramout> lst = new System.Collections.Generic.List<paramout>();



—SA


这篇关于如何返回此对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆