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

訪問並顯示數據庫中的數據[2]

2022-06-13   來源: .NET編程 
    ——此文章摘自《ASPNET網絡數據庫開發實例精解》定價 特價 詳細>>http://tracklinktechcn/?m_id=dangdang&a_id=A&l=&l_type= width= height= border= nosave>

    private void Page_Load(object sender SystemEventArgs e)
    { 
        if(!PageIsPostBack) 
        { 
            ShowDBData();                     //顯示數據庫MyFirstAccessDB中的數據 
        }
    } 
    private void ShowDBData()
    {        //設在訪問數據庫的連接字符串 
        string accessString = ACCESSCONNECTIONSTRING 
        + ServerMapPath(MyFirstAccessDBmdb); 
        string cmdText = SELECT * FROM MyDataTable;   //創建獲取數據的SQL語句 
        //創建數據庫的連接和Command 
        OleDbConnection accessConn = new OleDbConnection(accessString); 
        OleDbCommand accessComm = new OleDbCommand(cmdTextaccessConn); 
        //定義數據顯示的格式 
        string dataString = <br>MyID  MyName  MyType  My Length<br>
        try 
        {   //打開OLEDB數據庫的連接 
            accessConnOpen(); 
            ResponseWrite(連接MyFirstAccessDB數據源成功!!!); 
            //顯示數據庫中的信息 
            OleDbDataReader reca = accessCommExecuteReader(); 
            while(recaRead()) 
            {   //獲取相應數據 
                dataString += reca[MyID]ToString() +  
                + reca[MyName]ToString() 
                + + reca[MyType]ToString() +  
                + reca[MyLength]ToString(); 
                dataString += <br>
            } 
            recaClose(); 
            ResponseWrite(dataString);                                   //顯示數據庫中的內容 
            accessConnClose();                                                //關閉OLEDB數據庫的連接 
        } 
        catch(Exception ex) 
        { 
            ResponseWrite(exMessage);                                //拋出數據庫連接異常 
        }
    }

    設置頁面ShowDataaspx為應用程序的起始頁面按F鍵運行後出現如圖所示的初始界面

right>[http://developcsaicn/dotnet_ASP/htm>]  []  


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