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

ASP.Net網絡數據庫:修改數據庫[3]

2022-06-13   來源: .NET編程 
    ——此文章摘自《ASPNET網絡數據庫開發實例精解》定價 特價 詳細>>

        try 
        { 
            accessConnOpen();                    //打開OLEDB數據庫的連接 
            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);                      //拋出數據庫連接異常 
        }
    }

    單擊頁面UpdateDataaspx中【修改數據】按鈕控件觸發事件 UpdateDataBtn_Click(object sender SystemEventArgs e)該事件實現修改數據庫MyFirstAccessDB中數據的功能它調用函數UpdataDBData(String myIDString myName)首先從頁面獲取MyID和MyName的值然後再使用SQL更新語句更新表中相應的記錄最後調用函數ShowDBData()顯示表的數據事件UpdateDataBtn_Click(object sender SystemEventArgs e)和函數UpdataDBData(String myIDString myName)的程序代碼如下
    private void UpdateDataBtn_Click(object sender SystemEventArgs e)
    { 
        if(MyIDTextTrim()Length > && MyNameTextTrim()Length >
        {  
            UpdataDBData(MyIDTextTrim()MyNameTextTrim());              //修改表中的記錄 
            ShowDBData();                    //顯示修改後的數據 
        }
    }
    private void UpdataDBData(String myIDString myName)
    {        //設置訪問數據庫的連接 
        string accessString = ACCESSCONNECTIONSTRING 
        + ServerMapPath(MyFirstAccessDBmdb); 
        //設置更新數據庫的SQL語句 
        string cmdText = UPDATE MyDataTable SET MyName= + myName 
        + WHERE MyID= + myID; 
        //創建訪問數據庫的連接和Command 
        OleDbConnection accessConn = new OleDbConnection(accessString); 
        OleDbCommand accessComm = new OleDbCommand(cmdTextaccessConn); 

[]  []  []  []  


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