我把簡單問題複雜化了:)[舊代碼]

來源:互聯網
上載者:User
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);
}

呵呵,複雜化了吧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.