• <label id="pxtpz"><meter id="pxtpz"></meter></label>
      1. <span id="pxtpz"><optgroup id="pxtpz"></optgroup></span>

        當(dāng)前位置:雨林木風(fēng)下載站 > 技術(shù)開發(fā)教程 > 詳細(xì)頁面

        C#數(shù)據(jù)庫編程2

        C#數(shù)據(jù)庫編程2

        更新時間:2022-05-04 文章作者:未知 信息來源:網(wǎng)絡(luò) 閱讀次數(shù):

        四.插入數(shù)據(jù)記錄: </P><P>  對數(shù)據(jù)庫進(jìn)行插入記錄操作和刪除記錄操作基本的思路是一致的,就是通過ADO.NET首先插入數(shù)據(jù)記錄到數(shù)據(jù)庫,然后對"DataSet"對象進(jìn)行必要的修改。下列代碼就是以Access 2000數(shù)據(jù)庫為模型修改當(dāng)前記錄的代碼: </P><P>protected void Update_record ( object sender , System.EventArgs e )
        {

        int i = myBind.Position ;

        try{

        file://連接到一個數(shù)據(jù)庫

        string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ;

        OleDbConnection myConn = new OleDbConnection ( strCon ) ;

        myConn.Open ( ) ;

        myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ;

        file://從數(shù)據(jù)庫中修改指定記錄

        string strUpdt = " UPDATE person SET xm = '"

        + t_xm.Text + "' , xb = '"

        + t_xb.Text + "' , nl = "

        + t_nl.Text + " , zip = "

        + t_books.Text + " WHERE id = " + t_id.Text ;

        OleDbCommand myCommand = new OleDbCommand ( strUpdt , myConn ) ;

        myCommand.ExecuteNonQuery ( ) ;

        myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ;

        myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;

        myConn.Close ( ) ;

        }

        catch ( Exception ed )

        {

        MessageBox.Show ( "修改指定記錄錯誤: " + ed.ToString ( ) , "錯誤!" ) ;

        }

        myBind.Position = i ;

        } </P><P>  由于對Sql Server 2000數(shù)據(jù)記錄修改操作和Access 2000數(shù)據(jù)記錄修改操作的差異只在于不同的數(shù)據(jù)鏈接,具體的代碼可以參考"刪除數(shù)據(jù)記錄"中的代碼,在這里就不提供了。



        五.插入數(shù)據(jù)記錄:



          和前面二種操作在思路是一致的,就是通過ADO.NET首先插入數(shù)據(jù)記錄到數(shù)據(jù)庫,然后對"DataSet"對象進(jìn)行必要的修改。下列代碼就是以Access 2000數(shù)據(jù)庫為模型插入一條數(shù)據(jù)記錄的代碼



        protected void Insert_record ( object sender , System.EventArgs e )

        {

        try

        {

        file://判斷所有字段是否添完,添完則執(zhí)行,反之彈出提示

        if ( t_id.Text != "" && t_xm.Text != "" && t_xb.Text != "" && t_nl.Text != "" && t_books.Text != "" )

        {

        string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;

        OleDbConnection myConn = new OleDbConnection ( myConn1 ) ;

        myConn.Open ( ) ;

        string strInsert = " INSERT INTO person ( id , xm , xb , nl , zip ) VALUES ( " ;

        strInsert += t_id.Text + ", '" ;

        strInsert += t_xm.Text + "', '" ;

        strInsert += t_xb.Text + "', " ;

        strInsert += t_nl.Text + ", " ;

        strInsert += t_books.Text + ")" ;

        OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;

        inst.ExecuteNonQuery ( ) ;

        myConn.Close ( ) ;

        myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ;

        myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ;

        myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;

        }

        else

        {

        MessageBox.Show ( "必須填滿所有字段值!" , "錯誤!" ) ;

        }

        }

        catch ( Exception ed )

        {

        MessageBox.Show ( "保存數(shù)據(jù)記錄發(fā)生 " + ed.ToString ( ) , "錯誤!" ) ;

        }

        }



          同樣對Sql Server 2000數(shù)據(jù)庫進(jìn)行插入記錄操作和Access 2000數(shù)據(jù)庫插入記錄操作的差異也只在于不同的數(shù)據(jù)鏈接,具體的代碼可以參考"刪除數(shù)據(jù)記錄"中的代碼,在這里就不提供了。

          六.Visual C#數(shù)據(jù)庫編程的完成源代碼和程序運(yùn)行的主界面:



          掌握了上面要點(diǎn),編寫一個完整的數(shù)據(jù)庫編程的程序就顯得非常容易了,下面是Visual C#進(jìn)行數(shù)據(jù)庫編程的完整代碼(Data01.cs),此代碼是以Access 2000數(shù)據(jù)庫為模型設(shè)計(jì)的,具體如下:



        using System ;

        using System.Drawing ;

        using System.ComponentModel ;

        using System.Windows.Forms ;

        using System.Data.OleDb ;

        using System.Data ;



        public class Data : Form

        {

        private System.ComponentModel.Container components = null ;

        private Button lastrec ;

        private Button nextrec ;

        private Button previousrec ;

        private Button firstrec ;

        private TextBox t_books ;

        private TextBox t_nl ;

        private ComboBox t_xb ;

        private TextBox t_xm ;

        private TextBox t_id ;

        private Label l_books ;

        private Label l_nl ;

        private Label l_xb ;

        private Label l_xm ;

        private Label l_id ;

        private Label label1 ;

        private DataSet myDataSet ;

        private Button button1 ;

        private Button button2 ;

        private Button button3 ;

        private Button button4 ;

        private BindingManagerBase myBind ;



        public Data ( )

        {

        file://連接到一個數(shù)據(jù)庫

        GetConnected ( ) ;

        // 對窗體中所需要的內(nèi)容進(jìn)行初始化

        InitializeComponent ( ) ;

        }

        file://清除在程序中使用過的資源

        protected override void Dispose( bool disposing )

        {

        if( disposing )

        {

        if ( components != null )

        {

        components.Dispose ( ) ;

        }

        }

        base.Dispose( disposing ) ;

        }

        public static void Main ( )

        {

        Application.Run ( new Data ( ) ) ;

        }

        public void GetConnected ( )

        {

        try

        {

        file://創(chuàng)建一個 OleDbConnection

        string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;

        OleDbConnection myConn = new OleDbConnection ( strCon ) ;

        string strCom = " SELECT * FROM person " ;

        file://創(chuàng)建一個 DataSet

        myDataSet = new DataSet ( ) ;



        myConn.Open ( ) ;

        file://用 OleDbDataAdapter 得到一個數(shù)據(jù)集

        OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;

        file://把Dataset綁定books數(shù)據(jù)表

        myCommand.Fill ( myDataSet , "person" ) ;

        file://關(guān)閉此OleDbConnection

        myConn.Close ( ) ;

        }

        catch ( Exception e )

        {

        MessageBox.Show ( "連接錯誤! " + e.ToString ( ) , "錯誤" ) ;

        }

        }

        private void InitializeComponent ( )

        {



        file://添加控件,略



        this.Name = "Data" ;

        this.Text = "Visual C#的數(shù)據(jù)庫編程!" ;

        this.ResumeLayout(false) ;

        myBind = this.BindingContext [ myDataSet , "person" ] ;

        }

        protected void New_record ( object sender , System.EventArgs e )

        {

        t_id.Text = ( myBind.Count + 1 ).ToString ( ) ;

        t_xm.Text = "" ;

        t_xb.Text = "" ;

        t_nl.Text = "" ;

        t_books.Text = "" ;

        }

        protected void Insert_record ( object sender , System.EventArgs e )

        {

        try

        {

        file://判斷所有字段是否添完,添完則執(zhí)行,反之彈出提示

        if ( t_id.Text != "" && t_xm.Text != "" && t_xb.Text != "" && t_nl.Text != "" && t_books.Text != "" )

        {

        string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;

        OleDbConnection myConn = new OleDbConnection ( myConn1 ) ;

        myConn.Open ( ) ;

        string strInsert = " INSERT INTO person ( id , xm , xb , nl , zip ) VALUES ( " ;

        strInsert += t_id.Text + ", '" ;

        strInsert += t_xm.Text + "', '" ;

        strInsert += t_xb.Text + "', " ;

        strInsert += t_nl.Text + ", " ;

        strInsert += t_books.Text + ")" ;

        OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;

        inst.ExecuteNonQuery ( ) ;

        myConn.Close ( ) ;

        myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ;

        myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ;

        myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;

        }

        else

        {

        MessageBox.Show ( "必須填滿所有字段值!" , "錯誤!" ) ;

        }

        }

        catch ( Exception ed )

        {

        MessageBox.Show ( "保存數(shù)據(jù)記錄發(fā)生 " + ed.ToString ( ) , "錯誤!" ) ;

        }

        }

        protected void Update_record ( object sender , System.EventArgs e )

        {

        int i = myBind.Position ;

        try{

        file://連接到一個數(shù)據(jù)庫

        string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ;

        OleDbConnection myConn = new OleDbConnection ( strCon ) ;

        myConn.Open ( ) ;

        myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ;

        file://從數(shù)據(jù)庫中修改指定記錄

        string strUpdt = " UPDATE person SET xm = '"

        + t_xm.Text + "' , xb = '"

        + t_xb.Text + "' , nl = "

        + t_nl.Text + " , zip = "

        + t_books.Text + " WHERE id = " + t_id.Text ;

        OleDbCommand myCommand = new OleDbCommand ( strUpdt , myConn ) ;

        myCommand.ExecuteNonQuery ( ) ;

        myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ;

        myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;

        myConn.Close ( ) ;

        }

        catch ( Exception ed )

        {

        MessageBox.Show ( "修改指定記錄錯誤: " + ed.ToString ( ) , "錯誤!" ) ;

        }

        myBind.Position = i ;

        }



        protected void Delete_record ( object sender , System.EventArgs e )

        {

        DialogResult r = MessageBox.Show ( "是否刪除當(dāng)前記錄!" , "刪除當(dāng)前記錄!" , MessageBoxButtons.YesNo , MessageBoxIcon.Question ) ;

        int ss = ( int ) r ;

          if ( ss == 6 ) // 按動"確定"按鈕

        {

        try{

        file://連接到一個數(shù)據(jù)庫

        string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ;

        OleDbConnection myConn = new OleDbConnection ( strCon ) ;

        myConn.Open ( ) ;

        string strDele = "DELETE FROM person WHERE id= " + t_id.Text ;

        OleDbCommand myCommand = new OleDbCommand ( strDele , myConn ) ;

        file://從數(shù)據(jù)庫中刪除指定記錄

        myCommand.ExecuteNonQuery ( ) ;

        file://從DataSet中刪除指定記錄

        myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . Delete ( ) ;

        myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;

        myConn.Close ( ) ;

        }

        catch ( Exception ed )

        {

        MessageBox.Show ( "刪除記錄錯誤信息: " + ed.ToString ( ) , "錯誤!" ) ;

        }

        }

        }

        file://按鈕"尾記錄"對象事件程序

        protected void GoLast ( object sender , System.EventArgs e )

        {

        myBind.Position = myBind.Count - 1 ;

        }



        file://按鈕"下一條"對象事件程序

        protected void GoNext ( object sender , System.EventArgs e )

        {

        if ( myBind.Position == myBind.Count -1 )

        MessageBox.Show ( "已經(jīng)到了最后一條記錄!", "信息提示!" , MessageBoxButtons.OK , MessageBoxIcon.Information ) ;

        else

        myBind.Position += 1 ;

        }

        file://按鈕"上一條"對象事件程序

        protected void GoPrevious ( object sender , System.EventArgs e )

        {

        if ( myBind.Position == 0 )

        MessageBox.Show ( "已經(jīng)到了第一條記錄!" , "信息提示!" , MessageBoxButtons.OK , MessageBoxIcon.Information ) ;

        else

        myBind.Position -= 1 ;

        }

        file://按鈕"首記錄"對象事件程序

        protected void GoFirst ( object sender , System.EventArgs e )

        {

        myBind.Position = 0 ;

        }

        }





          對于以Sql Server 2000數(shù)據(jù)庫為模型的程序代碼,只要把Data01.cs中的數(shù)據(jù)鏈接,即:

        string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;



          改換成:

        string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ;



          注釋:此數(shù)據(jù)鏈接代表的意思是:打開Sql server數(shù)據(jù)庫,服務(wù)器名稱為server1,數(shù)據(jù)庫為data1

        就可以得到Visual C#針對Sql Server 2000數(shù)據(jù)庫為模板編程的完成源程序代碼了。所以本文就不再提供了。



          七.總結(jié):



          數(shù)據(jù)庫編程始終是程序編程內(nèi)容中的一個重點(diǎn)和難點(diǎn)。而以上介紹的這些操作又是數(shù)據(jù)庫編程中最為基本,也是最為重要的內(nèi)容。那些復(fù)雜的編程無非是以上這些處理的若干個疊加。






        溫馨提示:喜歡本站的話,請收藏一下本站!

        本類教程下載

        系統(tǒng)下載排行

        主站蜘蛛池模板: 亚洲男人第一av网站| 好看的电影网站亚洲一区| 久久久久亚洲国产AV麻豆| 免费人成在线观看播放a| 国产一区二区三区免费视频| 亚洲综合无码无在线观看| 国产免费毛不卡片| 色偷偷亚洲女人天堂观看欧| 黄页网站免费观看| 亚洲中文字幕久久精品无码APP | 亚洲中久无码永久在线观看同| 四虎成人精品在永久免费| 亚洲女女女同性video| 久久99热精品免费观看牛牛| 亚洲av无码专区国产乱码在线观看| 国产亚洲精品bv在线观看| 日本免费中文字幕| 国产成人aaa在线视频免费观看| 好看的电影网站亚洲一区| 美女视频黄的免费视频网页| 亚洲成A∨人片天堂网无码| 亚洲同性男gay网站在线观看| 青青草免费在线视频| 久久青青草原亚洲av无码app | 国产精品亚洲专区无码WEB| 免费无码又爽又刺激高潮视频 | 日本xxxx色视频在线观看免费| 2022年亚洲午夜一区二区福利| 中文字幕乱码免费视频| 亚洲AV永久无码区成人网站| xx视频在线永久免费观看| 亚洲精品无码久久久久A片苍井空| 亚洲福利视频一区二区| 国产精品99精品久久免费| avtt天堂网手机版亚洲| 四虎影视在线永久免费看黄| 青柠影视在线观看免费高清| 亚洲人成人一区二区三区| 色片在线免费观看| 一级成人生活片免费看| 亚洲人成电影网站国产精品|