English 中文(简体)
不能使用IDataReader读值
原标题:Could not read values using IDataReader

我想读到数据库中的数据。

我尝试了以下法典:

public List<T> StoredProcedureForIList<T>(string spName, params IDataParameter[] commandParameters)
{
   List<T> list = new List<T>();

   T item;
   Type listItemType = typeof(T);
   item = (T)Activator.CreateInstance(listItemType);
   list.Add(item);

   using (IDatabaseConnection connection = new DatabaseConnection())
   {
       IDbCommand cmd = connection.CreateCommandForStoredProcedure(spName);
       foreach (SqlParameter par in commandParameters)
       {
          cmd.Parameters.Add(par);
       }

       try
       {
          using (IDataReader reader = cmd.ExecuteReader())
          {
             while (reader != null && reader.Read())
             {
                for (int i = 0; i < reader.FieldCount; i++)
                {
                   var prop = listItemType.GetProperty(reader.GetName(i));
                   prop.SetValue(item, reader[i], null);
                }
                list.Add(item);
             }
          }
       }
       catch(Exception ex)
       { }

       return list;
   }
} 

但问题是,当休息开始时,读者就会失去数据。

数值是计算结果。

最佳回答

我的猜测是,在执行你 lo期间出现了一些错误。 ......

try 
{ 
    ...
} 
catch(Exception ex) 
{ } 

......确保这一错误被忽视,所有你都得到的结果不完整。 正如你所注意到的那样,这使得偷渡工作变得十分困难。 因此,没有这样做。

因此,解决办法是:

  • remove the try-catch block (i.e., replace try { ... } catch(Exception ex) {} by ...),
  • run the code again,
  • note the error that occurs,
  • if you understand the error
    • fix it
  • else
    • ask again on StackOverflow, in a new question.

而且,从来没有写过<条码>副渔获物(Exception ex){} 。 Do proper误差处理,或根本不处理错误。

问题回答

读物者得 won,读书者经过良好测试。 开放式的例外情况获得了帮助。 如果我不得不猜测,这里的问题是,你正在加上相同的<条码>项目。 事实上,你增加了N+1倍(即使没有输电,你也增加了一次)。

However, can I suggest: just use something like dapper, which does everything above, except a: it gets it right, and b: it is highly optimized (it emits custom IL to avoid constant reflection, and caches that IL). It would be something akin to:

var list = connection.Query<T>(procName, namedArgs,
       commandType: CommandType.StoredProcedure).ToList();

www.un.org/spanish/ga/president

new {id=123, name="abc"}

i.e.

int id = ...
string name = ...
var list = connection.Query<T>("MyProc", new {id, name},
       commandType: CommandType.StoredProcedure).ToList();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签