程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> Visual C#中輕松浏覽數據庫記錄(2)

Visual C#中輕松浏覽數據庫記錄(2)

編輯:關於C語言

(2)如何改變記錄指針:

只有掌握如何改變記錄指針,才可以隨心所欲的浏覽記錄.Visual C#改變記錄指針是通過一個命叫BindingManagerBase對象來實現的.此對象封裝在名稱空間System.Windows.Froms中.BindingManagerBase對象是一個抽象的對象,管理所有綁定的同類的數據源和數據成員.在程序設計中主要用到BindingManagerBase對象中的二個屬性,即:Position屬性和Count屬性.第一個屬性是記錄了數據集的當前指針,後一個屬性是當前數據集中的記錄總數.由此可以得到改變記錄指針的四個按鈕對應的程序代碼:

i>.首記錄:
myBind.Position = 0 ;
ii>.尾記錄:
myBind.Position = myBind.Count - 1 ;
iii>.下一條記錄和操作後運行界面:
if (myBind.Position == myBind.Count -1)
MessageBox.Show ("已經到了最後一條記錄!") ;
else
myBind.Position += 1 ;
iV>.上一條記錄和操作後運行界面:
if (myBind.Position == 0)
MessageBox.Show ("已經到了第一條記錄!") ;
else
myBind.Position -= 1 ;
四.程序源代碼:using System ;
using System.Drawing ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data.OleDb ;
using System.Data ;
public class DataVIEw : Form
{
  private System.ComponentModel.Container components ;
  private Button lastrec ;
  private Button nextrec ;
  private Button previousrec ;
  private Button firstrec ;
  private TextBox t_books ;
  private TextBox t_bookprice ;
  private TextBox t_bookauthor ;
  private TextBox t_booktitle ;
  private TextBox t_bookid ;
  private Label l_books ;
  private Label l_bookprice ;
  private Label l_bookauthor ;
  private Label l_booktitle ;
  private Label l_bookid ;
  private Label label1 ;
  private System.Data.DataSet myDataSet ;
  private BindingManagerBase myBind ;
  public DataVIEw ( )
  {
    //連接到一個數據庫
    GetConnected ( ) ;
    // 對窗體中所需要的內容進行初始化
    InitializeComponent ( );
  }
  public override void Dispose ( )
  {
    base.Dispose ( ) ;
    components.Dispose ( ) ;
  }
  public static void Main ( )
  {
    Application.Run (new DataVIEw ( )) ;
  }
  public void GetConnected ( )
  {
    try
    {
      //創建一個 OleDbConnection
      string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ;
      Data Source = sample.mdb" ;
      OleDbConnection myConn = new OleDbConnection (strCon) ;
      string strCom = " SELECT * FROM books " ;
      //創建一個 DataSet
      myDataSet = new DataSet ( ) ;
      myConn.Open ( ) ;
      //用 OleDbDataAdapter 得到一個數據集
      OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom , myConn) ;
      //把Dataset綁定books數據表
      myCommand.Fill (myDataSet , "books") ;
      //關閉此OleDbConnection
      myConn.Close ( ) ;
    }
    catch (Exception e)
    {
      MessageBox.Show ("連接錯誤! " + e.ToString ( ) , "錯誤") ;
    }
  }
  private void
  InitializeComponent ( )
  {
    this.components = new System.ComponentModel.Container ( ) ;
    this.t_bookid = new TextBox ( ) ;
    this.nextrec = new Button ( ) ;
    this.lastrec = new Button ( ) ;
    this.l_bookid = new Label ( ) ;
    this.t_books = new TextBox ( ) ;
    this.t_booktitle = new TextBox ( ) ;
    this.t_bookprice = new TextBox ( ) ;
    this.firstrec = new Button ( ) ;
    this.l_booktitle = new Label ( ) ;
    this.l_bookprice = new Label ( ) ;
    this.l_books = new Label ( ) ;
    this.previousrec = new Button ( ) ;
    this.l_bookauthor = new Label ( ) ;
    this.t_bookauthor = new TextBox ( ) ;
    this.label1 = new Label ( ) ;
    //以下是對數據浏覽的四個按鈕進行初始化
    firstrec.Location = new System.Drawing.Po
    int (55 , 312) ;
    firstrec.ForeColor = System.Drawing.Color.Black ;
    firstrec.Size = new System.Drawing.Size (40 , 24) ;
    firstrec.TabIndex = 5 ;
    firstrec.Font = new System.Drawing.Font( "仿宋", 8f);
    firstrec.Text = "首記錄";
    firstrec.Click += new System.EventHandler( GoFirst );
    previousrec.Location = new System.Drawing.Po
    int (125 , 312) ;
    previousrec.ForeColor = System.Drawing.Color.Black ;
    previousrec.Size = new System.Drawing.Size( 40, 24 ) ;
    previousrec.TabIndex = 6 ;
    previousrec.Font = new System.Drawing.Font ("仿宋" , 8f) ;
    previousrec.Text = "上一條" ;
    previousrec.Click += new System.EventHandler (GoPrevious) ;
    nextrec.Location = new System.Drawing.Po
    int (195 , 312);
    nextrec.ForeColor = System.Drawing.Color.Black ;
    nextrec.Size = new System.Drawing.Size (40 , 24) ;
    nextrec.TabIndex = 7 ;
    nextrec.Font = new System.Drawing.Font ("仿宋" , 8f) ;
    nextrec.Text = "下一條" ;
    nextrec.Click += new System.EventHandler (GoNext);
    lastrec.Location = new System.Drawing.Po
    int (265 , 312) ;
    lastrec.ForeColor = System.Drawing.Color.Black ;
    lastrec.Size = new System.Drawing.Size (40 , 24) ;
    lastrec.TabIndex = 8 ;
    lastrec.Font = new System.Drawing.Font ("仿宋" , 8f) ;
    lastrec.Text = "尾記錄" ;
    lastrec.Click += new System.EventHandler (GoLast) ;
    //以下是對為顯示數據記錄而設定的標簽和文本框進行初始化,並把記錄綁定在不同的綁定到文本框"Text"屬性上
    t_bookid.Location = new System.Drawing.Po
    int (184 , 56) ;
    t_bookid.TabIndex = 9 ;
    t_bookid.Size = new System.Drawing.Size (80 , 20) ;
    t_bookid.DataBindings.Add ("Text" , myDataSet , "books.bookid") ;
    t_books.Location = new System.Drawing.Po
    int (184 , 264) ;
    t_books.TabIndex = 10 ;
    t_books.Size = new System.Drawing.Size (80 , 20) ;
    t_books.DataBindings.Add ("Text" , myDataSet , "books.bookstock") ;
    t_booktitle.Location = new System.Drawing.Po
    int (184 , 108) ;
    t_booktitle.TabIndex = 11 ;
    t_booktitle.Size = new System.Drawing.Size (176 , 20) ;
    t_booktitle.DataBindings.Add("Text" , myDataSet , "books.booktitle") ;
    t_bookprice.Location = new System.Drawing.Po
    int (184 , 212) ;
    t_bookprice.TabIndex = 12 ;
    t_bookprice.Size = new System.Drawing.Size (80 , 20) ;
    t_bookprice.DataBindings.Add ("Text" , myDataSet , "books.bookprice") ;
    t_bookauthor.Location = new System.Drawing.Po
    int (184 , 160) ;
    t_bookauthor.TabIndex = 18 ;
    t_bookauthor.Size = new System.Drawing.Size (128 , 20) ;
    t_bookauthor.DataBindings.Add ("Text" , myDataSet , "books.bookauthor") ;
    l_bookid.Location = new System.Drawing.Po
    int (24 , 56) ;
    l_bookid.Text = "書本序號:" ;
    l_bookid.Size = new System.Drawing.Size (112, 20) ;
    l_bookid.Font = new System.Drawing.Font ("仿宋" , 10f) ;
    l_bookid.TabIndex = 13 ;
    l_bookid.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    l_booktitle.Location = new System.Drawing.Po
    int (24 , 108) ;
    l_booktitle.Text = "書 名:";
    l_booktitle.Size = new System.Drawing.Size (112 , 20) ;
    l_booktitle.Font = new System.Drawing.Font ("仿宋" , 10f) ;
    l_booktitle.TabIndex = 14 ;
    l_booktitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    l_bookprice.Location = new System.Drawing.Po
    int (24 , 212) ;
    l_bookprice.Text = "價 格:" ;
    l_bookprice.Size = new System.Drawing.Size (112 , 20) ;
    l_bookprice.Font = new System.Drawing.Font ("仿宋" , 10f) ;
    l_bookprice.TabIndex = 15 ;
    l_bookprice.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    l_books.Location = new System.Drawing.Po
    int (24 , 264) ;
    l_books.Text = "書 架 號:" ;
    l_books.Size = new System.Drawing.Size (112 , 20) ;
    l_books.Font = new System.Drawing.Font ("仿宋" , 10f) ;
    l_books.TabIndex = 16 ;
    l_books.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    l_bookauthor.Location = new System.Drawing.Po
    int (24 , 160) ;
    l_bookauthor.Text = "作 者:" ;
    l_bookauthor.Size = new System.Drawing.Size (112 , 20) ;
    l_bookauthor.Font = new System.Drawing.Font ("仿宋" , 10f) ;
    l_bookauthor.TabIndex = 17 ;
    l_bookauthor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    label1.Location = new System.Drawing.Po
    int (49 , 8) ;
    label1.Text = "浏覽書籍信息" ;
    label1.Size = new System.Drawing.Size (296 , 24) ;
    label1.ForeColor = System.Drawing.Color.Green ;
    label1.Font = new System.Drawing.Font ("仿宋" , 15f) ;
    label1.TabIndex = 19 ;
    //對窗體進行設定
    this.Text = "用C#做浏覽數據庫中記錄的程序!";
    this.AutoScaleBaseSize = new System.Drawing.Size (5 , 13) ;
    this.FormBorderStyle = FormBorderStyle.FixedSingle ;
    this.ClIEntSize = new System.Drawing.Size (394 , 375) ;
    //在窗體中加入組件
    this.Controls.Add (lastrec) ;
    this.Controls.Add (nextrec) ;
    this.Controls.Add (previousrec) ;
    this.Controls.Add (firstrec) ;
    this.Controls.Add (t_books) ;
    this.Controls.Add (t_bookprice) ;
    this.Controls.Add (t_bookauthor) ;
    this.Controls.Add (t_booktitle) ;
    this.Controls.Add (t_bookid) ;
    this.Controls.Add (l_books) ;
    this.Controls.Add (l_bookprice) ;
    this.Controls.Add (l_bookauthor) ;
    this.Controls.Add (l_booktitle) ;
    this.Controls.Add (l_bookid) ;
    this.Controls.Add (label1) ;
    //把對象DataSet和"books"數據表綁定到此myBind對象
    myBind=
    this.BindingContext [ myDataSet , "books" ] ;
  }
  //按鈕"尾記錄"對象事件程序
  protected void GoLast (object sender , System.EventArgs e)
  {
    myBind.Position = myBind.Count - 1 ;
  }
  //按鈕"下一條"對象事件程序
  protected void GoNext (object sender , System.EventArgs e)
  {
    if (myBind.Position == myBind.Count -1)
    MessageBox.Show ("已經到了最後一條記錄!") ;
    else
    myBind.Position += 1 ;
  }
  //按鈕"上一條"對象事件程序
  protected void GoPrevious (object sender , System.EventArgs e)
  {
    if (myBind.Position == 0)
    MessageBox.Show ("已經到了第一條記錄!") ;
    else
    myBind.Position -= 1 ;
  }
  //按鈕"首記錄"對象事件程序
  protected void GoFirst (object sender , System.EventArgs e)
  {
    myBind.Position = 0 ;
  }
}

五.總結:

本文的重點就在於如何用Visual C#改變數據集的記錄指針和如何讓文本框根據記錄指針的變化而改變顯示內容.雖然此類處理在Visual C#比起用其他語言要顯得麻煩些.但對於程序設計人員卻更靈活了,使得程序設計人員有了更大的發展空間.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved