熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#中DataReader類的使用

2013-11-13 09:54:02  來源: .NET編程 

  

  using System;
        using SystemIO;
        using SystemDataOleDb;
        namespace ConsoleCommandDemo
        {
            /// <summary>
            /// Class 的摘要說明
            /// </summary>
            class CommandClass
            {
                /// <summary>
                /// 應用程序的主入口點
                /// </summary>
                [STAThread]
                static void Main(string[] args)
                {
                    string strCon = @Provider=MicrosoftJetOLEDB;Data Source=DataBasemdb;;
                    // 定義連接
                    OleDbConnection MyCon = new OleDbConnection(strCon);
                    try
                    {
                        MyConOpen();
                        string strSQL = @INSERT INTO ADODemo (姓名 年齡) VALUES (趙六 );
                        OleDbCommand MyCom = new OleDbCommand(strSQL MyCon);
                        // 執行沒有返回結果的指令
                        ConsoleWriteLine(執行的命令:{} MyComCommandText);
                        ConsoleWriteLine(執行結果影響了:{}個記錄 MyComExecuteNonQuery());
                        // 刪除一條記錄
                        strSQL = @delete from ADODemo where (姓名 = 趙六);
                        MyComCommandText = strSQL;
                        ConsoleWriteLine(執行的命令:{} MyComCommandText);
                        ConsoleWriteLine(執行結果影響了:{}個記錄 MyComExecuteNonQuery());
                        // 使用參數插入一條記錄
                        MyComCommandText =INSERT INTO ADODemo (姓名 年齡) VALUES (? ?);
                        MyComParametersAdd(@姓名 OleDbTypeVarChar );
                        MyComParametersAdd(@年齡 OleDbTypeTinyInt);
                        MyComParameters[@姓名]Value = 趙六;
                        MyComParameters[@年齡]Value = ;
                        ConsoleWriteLine(執行的命令:{} MyComCommandText);
                        ConsoleWriteLine(執行結果影響了:{}個記錄 MyComExecuteNonQuery());
                        // 刪除一條記錄
                        strSQL = @delete from ADODemo where (姓名 = 趙六);
                        MyComCommandText = strSQL;
                        ConsoleWriteLine(執行的命令:{} MyComCommandText);
                        ConsoleWriteLine(執行結果影響了:{}個記錄 MyComExecuteNonQuery());
                        // 浏覽數據
                        strSQL = @select * from ADODemo;
                        MyComCommandText = strSQL;
                        OleDbDataReader MyDataReader = MyComExecuteReader();
                        // 顯示數據庫中的數據
                        ConsoleWriteLine(顯示數據庫中的數據);
                        while(MyDataReaderRead())
                        {
                            ConsoleWriteLine(姓名:{} 年齡:{} MyDataReaderGetValue() MyDataReaderGetValue());
                        }
                        MyDataReaderClose();
                    }
                    catch (Exception ex)
                    {
                        ConsoleWriteLine({} exToString());
                    }
                    finally
                    {
                        MyConClose();
                        MyConClose();
                    }
                    ConsoleReadLine();
                }
            }
        }


From:http://tw.wingwit.com/Article/program/net/201311/11905.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.