设为首页收藏本站language 语言切换
查看: 1399|回复: 0
收起左侧

解析ADO.NET中的ADODataReader类

[复制链接]
发表于 2010-2-25 10:27:07 | 显示全部楼层 |阅读模式
<p >ADO.NET是我们熟知并深爱的ADO的.NET加强版。ADO.NET意图改善一些传统ADO的缺陷,比如缺乏类型安全性,缺乏面向对象模型以及返回数据行时的低效。<p >本文介绍了一些访问数据库时的常规事务:数据查询,从头至尾地截取数据以显示表中的内容(或其子集)。<p ><center><font color="#000099"><strong>ADODataReader类</strong></font></center><p >ADO.NET以数据行的观点代替了数据集对象。这从本质上让我们能以面向对象和类型安全的方式完全访问一个指定的数据库,包括其所有的行、表、关系。但是,这过度取代了以前经常对数据库进行的简单查询和截取数据等操作。<p >本文的.NET例子中为我们提供了一个ADODataReader类,它本质上是一个安全只读并仅支持前向的数据行集的类型。我们所要做的一切就是打开一个数据库连接,发送一个SOL命令,然后遍历以Read方法所获得的ADODataReader结果集并处理这些结果。<p >展示此点的最简单方式莫过于让你看C#代码。这些代码打开一个数据库访问,从某表中读出所有数据,最后形成一个包含这些数据的列表视图控制。<p >有关代码的一些注解:<p >StatusText是一个多格式文本框控制,声明如下:<p ><ccid_nobr><table width="550" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code"><pre><ccid_code>System.WinForms.RichTextBox  StatusText;StatusText = new System.WinForms.RichTextBox ();</ccid_code></pre></td></tr></table></ccid_nobr><p >listView是一个列表视图控制,声明如下:<p ><ccid_nobr><table width="550" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code"><pre><ccid_code>System.WinForms.ListView listView;listView = new System.WinForms.ListView ();</ccid_code></pre></td></tr></table></ccid_nobr><p >这个列表试图被放置于一个带网格行的报告模式中:<p ><ccid_nobr><table width="550" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code"><pre><ccid_code>listView.View = System.WinForms.View.Report;listView.GridLines = true;</ccid_code></pre></td></tr></table></ccid_nobr><p >代码:<p ><ccid_nobr><table width="550" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code"><pre><ccid_code>ADOConnection adoConnection = new ADOConnection();// TODO: Change the location of this file// The '@' means that the string will be treated as-is, and the// '\'s will not be interpreted as the escape character.// This saves typing &quot;D:\\ADONETdemo...&quot;String strDatabaseFile = @&quot;D:\ADONETdemo\Authors.mdb&quot;;try{        // Open a connection to the database        adoConnection.ConnectionString = &quotrovider=Microsoft.Jet.OLEDB.4.0;&quot; +                                          &quot;Data Source=&quot; + strDatabaseFile + &quot;;&quot; +                                         &quotersist Security Info=False;&quot;;        adoConnection.Open();        // Create an SQL command, set its connection and its command text        ADOCommand command = new ADOCommand();        command.ActiveConnection = adoConnection;        command.CommandText = &quot;SELECT * FROM Author&quot;;        // Execute the command, and return the rows in the data reader object        ADODataReader dataReader;        command.Execute(out dataReader);        // Get the number of fields (columns) in the table        int nFields = dataReader.FieldCount;        // Setup the columns in the listview using the fields in the table        listView.Clear();        for (int i = 0; i&lt;nFields; i++)        {                listView.InsertColumn(i, dataReader.GetName(i), 100, HorizontalAlignment.Left);        }        // Fill the rows in the listview using the data in the rows        int nRow = 0;        while (dataReader.Read())        {                // Create an array of subitems for quick insertion                // The subitems will be all fields in the row except for                 // the first field                String [] subitems = new String[nFields-1];                for (int i = 1; i&lt;nFields; i++)                {                        subitems[i-1] = dataReader.GetValue(i).ToString();                }                // Insert a new item into the listview, and add the subitems                // at the same time. The item will be the first field in the                // row                listView.InsertItem(nRow, dataReader.GetValue(0).ToString(),                         -1, subitems);                // next row.                nRow++;        }                                        dataReader.Close();        // Set the status text        StatusText.Text = nFields.ToString() + &quot; columns, &quot; +                                           nRow.ToString() + &quot; rows read&quot;;}catch {        // If an error occured alert the user        StatusText.Text = &quot;Error occurred&quot;;}finally{        // Close the connection if necessary        if (adoConnection.State == DBObjectState.Open)                adoConnection.Close();}</ccid_code></pre></td></tr></table></ccid_nobr><p >这就是所有的。我们关闭了所有的数据库连接,但因为我们是用的是被管理的代码,因此没必要(也无法)清除我们调用的对象和内存。<p ><b>关于Chris Maunder</b><p >Chris是名管理员,设计者,主编以及知名的VBScript黑客,他编写并运行代码项目。他从1988年就开始在各种身份的掩饰下开始编程,数学家,航空物理学家,物理学家,地理学家,智能防御系统研究者,不过本质上他还是一个web开发者。他是澳大利亚乃至全球的Visual C++ 领域的MVP。<p >他的编程经验包括C/C++, C#, MFC, ASP,ASP.NET很多方面,尤其在FORTRAN领最为丰富。他曾经在PocketPCs, AIX mainframes, Sun工作站甚至CRAY YMP C90上工作过,最后发现PC还是占地面积要小的多。<p >(责任编辑 <ccid_nobr>Sunny</ccid_nobr>)                                 <p align="center"></p></p>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-10 15:49 , Processed in 0.059862 second(s), 24 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表