IDataReader的。什么时候用? [英] Idatareader. When to use this?

查看:58
本文介绍了IDataReader的。什么时候用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时使用IDataReader。



我在我的项目中发现使用IDataReader的大部分地方。什么时候用?请帮助。





When to use IDataReader.

I find in my project most of the places IDataReader is used. When to use this? Kindly help.


public virtual ArrayList ExecuteListStoredProcWithXML(String ClassName, string spName, TMS.API.VO_Base_Object baseObject)
       {
           ArrayList ObjectList = new ArrayList();
           IDataReader dataReaderToFill = null;
           Type ClassType = Type.GetType(ClassName + ", TMS.API, Version=1.0.0.0,





[edit]已添加代码块[/ edit]



[edit]Code block added[/edit]

推荐答案

IDataReader 在Ado.Net数据类的界面中。



许多不同的数据连接器符合Ado数据接口。包括SQL和Oracle等等。



所以使用Ado.Net界面,您可以编写对所使用的数据库技术透明的类。



例如



IDataReader in an interface from the Ado.Net data classes.

Lots of different data connectors conform to the Ado data interface. These include SQL and Oracle and many more.

So using the Ado.Net interface you can write classes which are transparent to the database technology being used.

e.g.

public MyObject GetMyObject(IDbConnection connection, int myObjectId)
{
  IDbCommand cmd = connection.CreateCommand();
  cmd.CommandText = "GetMyObjectSP";
  cmd.CommandType = CommandType.StoredProcedure;

  IDbDataParameter param = null;

  param = cmd.CreateParameter();
  param.DbType = DbType.Int32;
  param.ParameterName = "MyObjectId";
  param.value = myObjectId;

  //Here's the IDataReader
  IDataReader dr = cmd.ExecuteReader();

  MyObject result = null;

  if(dr.Read())
  {
    result = new MyObject();
    //populate object from reader
  }

  return result;

}







然后您可以将此方法与任意数量的数据库技术只需创建不同的连接类型。






You could then use this method with any number of database technologies just by creating a different connection type.

OracleConnection oracleConn = new OracleConnection("myConnectionString");
MyObject oracleObject = GetMyObject(oracleConn, objectId);

SqlConnection sqlConn = new SqlConnection("mySqlConnectionString");
MyObject sqlObject = GetMyObject(sqlObject, objectId);


这篇关于IDataReader的。什么时候用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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