我把简单问题复杂化了:)[旧代码]

using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;

namespace PatternsStudy1
{
    
class Program
    {
        
/// <summary>
        
/// 程序入口点
        
/// </summary>
        
/// <param name="args"></param>
        static void Main(string[] args)
        {
            BorrowReader br 
= new BorrowReader(GetNames);

            
string strSql = "SELECT * FROM ROCKET";

            
foreach (string str in LendReader(strSql, br))
            {
                Console.WriteLine(str);
            }
            
            Console.Read();
        }

        
/// <summary>
        
/// 取得数据库连接实例
        
/// </summary>
        
/// <returns>数据库连接实例</returns>
        private static OleDbConnection CreateConnection()
        {
            
//读取应用程序配置节
            string strDbConn = new AppSettingsReader().GetValue("dbconn"typeof(string)).ToString();
            
//string strDbConn = ConfigurationManager.AppSettings["dbconn"];
            
//Console.WriteLine(strDbConn);
            
//创建数据库连接对象
            OleDbConnection conn = new OleDbConnection();
            
//设定数据库连接字符串
            conn.ConnectionString = strDbConn;
            
//返回数据库连接实例
            return conn;
        }

        
/// <summary>
        
/// 执行读取操作
        
/// </summary>
        
/// <param name="strSql">要执行的Sql语句</param>
        
/// <param name="br">委托对象</param>
        
/// <returns>读取的结果</returns>
        private static IEnumerable LendReader(string strSql, BorrowReader br)
        {
            List
<string> list = new List<string>();
            
using (OleDbConnection conn = CreateConnection())
            {
                
//打开数据库连接
                conn.Open();
                OleDbCommand olcom 
= new OleDbCommand(strSql, conn);
                
//执行读取
                OleDbDataReader oldr = olcom.ExecuteReader();
                list 
= br(oldr);
                
foreach (string str in list)
                {
                    yield 
return str;
                }
            }
        }

        
/// <summary>
        
/// 读取结果集
        
/// </summary>
        
/// <param name="idr">读取数据对象</param>
        
/// <returns>读取的数据集</returns>
        private static List<string> GetNames(IDataReader idr)
        {
            List
<string> list = new List<string>();
            
while (idr.Read())
            {
                list.Add(idr[
1].ToString());
            }
            
return list;
        }
    }

    
/// <summary>
    
/// 声明委托,用于取得数据
    
/// </summary>
    
/// <param name="idr">读取数据对象</param>
    
/// <returns>读取的数据集</returns>
    public delegate List<string> BorrowReader(IDataReader idr);
}
呵呵,复杂化了吧。

posted on 2006-05-08 09:29  萝卜青菜  阅读(334)  评论(0编辑  收藏  举报

导航