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

用VisualC#中輕松浏覽數據庫記錄

2013-11-13 09:58:59  來源: .NET編程 

  用Delphi或者VB編程在對數據庫中的記錄進行操作的時候經常用到一個名稱為數據導航器的組件通過這個組件可以非常方便的實現對已經綁定到此組件的數據表中的記錄進行浏覽就是所謂的上一條記錄下一條記錄首記錄尾記錄等那麼在Visual C#是否也存在這樣的組件呢?答案是否定的但由於Visual C#有著強大的數據庫處理能力所以可以比較方便的做一個類似於此組件的程序本文就是來介紹此程序的具體制作過程

   程序的主要功能介紹

  程序打開本地Acess數據庫(samplemdb)中的book數據表然後把book數據表中的字段綁定到程序提供的文本框中顯示出來通過程序中的四個按鈕首記錄尾記錄上一條下一條實現對book數據表中的記錄浏覽程序的運行界面如下

  二程序設計和運行的環境設置

  ()視窗服務器版

  ()Microsoft Acess Data Component ( MADC )

  程序設計難點和應該注意的問題

  ()如何實現把數據表中的字段用文本框來顯示

  如果直接把字段的值賦值給文本框這時如果用下一條等按鈕來浏覽數據記錄的時候文本框的值是不會變化的如何讓文本框根據數據表中的記錄指針來動態的顯示要字段值這是本文的一個重點也是一個難點

  本文是通過把數據表中的字段值綁定到文本框的Text屬性上來實現動態顯示字段數值的實現這種處理要用到文本框的DataBindings屬性和其中的Add方法具體語法如下

  文本組件名稱DataBindingsAdd ( Text DataSet對象 數據表和字段名稱 ) ;

  在程序具體如下

  t_bookidDataBindingsAdd ( Text myDataSet booksbookid ) ;

  這樣就可以根據記錄指針來實現要顯示的字段值了

  ()如何改變記錄指針

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

  i>首記錄
myBindPosition = ;
ii>尾記錄
myBindPosition = myBindCount ;
iii>下一條記錄和操作後運行界面
if ( myBindPosition == myBindCount )
MessageBoxShow ( 已經到了最後一條記錄! ) ;
else
myBindPosition += ;

  iV>上一條記錄和操作後運行界面
if ( myBindPosition == )
MessageBoxShow ( 已經到了第一條記錄! ) ;
else
myBindPosition = ;

  程序源代碼:

  using System ;
using SystemDrawing ;
using SystemComponentModel ;
using SystemWindowsForms ;
using SystemDataOleDb ;
using SystemData ;

  public class DataView : Form {
private SystemComponentModelContainer 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 label ;
private SystemDataDataSet myDataSet ;
private BindingManagerBase myBind ;

  public DataView ( )
{
//連接到一個數據庫
GetConnected ( ) ;
// 對窗體中所需要的內容進行初始化
InitializeComponent ( );
}
public override void Dispose ( ) {
baseDispose ( ) ;
componentsDispose ( ) ;
}
public static void Main ( ) {
ApplicationRun ( new DataView ( ) ) ;
}
public void GetConnected ( )
{
try{
//創建一個 OleDbConnection
string strCon = Provider = MicrosoftJetOLEDB ; Data Source = samplemdb ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = SELECT * FROM books ;
//創建一個 DataSet
myDataSet = new DataSet ( ) ;

  myConnOpen ( ) ;
//用 OleDbDataAdapter 得到一個數據集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom myConn ) ;
//把Dataset綁定books數據表
myCommandFill ( myDataSet books ) ;
//關閉此OleDbConnection
myConnClose ( ) ;
}
catch ( Exception e )
{
MessageBoxShow ( 連接錯誤! + eToString ( ) 錯誤 ) ;
}
}
private void InitializeComponent ( )
{
ponents = new SystemComponentModelContainer ( ) ;
thist_bookid = new TextBox ( ) ;
thisnextrec = new Button ( ) ;

  thislastrec = new Button ( ) ;
thisl_bookid = new Label ( ) ;
thist_books = new TextBox ( ) ;
thist_booktitle = new TextBox ( ) ;
thist_bookprice = new TextBox ( ) ;
thisfirstrec = new Button ( ) ;
thisl_booktitle = new Label ( ) ;
thisl_bookprice = new Label ( ) ;
thisl_books = new Label ( ) ;
thispreviousrec = new Button ( ) ;
thisl_bookauthor = new Label ( ) ;
thist_bookauthor = new TextBox ( ) ;
thislabel = new Label ( ) ;

  //以下是對數據浏覽的四個按鈕進行初始化
firstrecLocation = new SystemDrawingPoint ( ) ;
firstrecForeColor = SystemDrawingColorBlack ;
firstrecSize = new SystemDrawingSize ( ) ;
firstrecTabIndex = ;
firstrecFont = new SystemDrawingFont(仿宋 f );
firstrecText = 首記錄;
firstrecClick += new SystemEventHandler(GoFirst);
previousrecLocation = new SystemDrawingPoint ( ) ;
previousrecForeColor = SystemDrawingColorBlack ;
previousrecSize = new SystemDrawingSize( ) ;
previousrecTabIndex = ;
previousrecFont = new SystemDrawingFont ( 仿宋 f ) ;
previousrecText = 上一條 ;
previousrecClick += new SystemEventHandler ( GoPrevious ) ;
nextrecLocation = new SystemDrawingPoint ( );
nextrecForeColor = SystemDrawingColorBlack ;
nextrecSize = new SystemDrawingSize ( ) ;
nextrecTabIndex = ;
nextrecFont = new SystemDrawingFont ( 仿宋 f ) ;
nextrecText = 下一條 ;
nextrecClick += new SystemEventHandler ( GoNext );

  lastrecLocation = new SystemDrawingPoint ( ) ;
lastrecForeColor = SystemDrawingColorBlack ;
lastrecSize = new SystemDrawingSize ( ) ;
lastrecTabIndex = ;
lastrecFont = new SystemDrawingFont ( 仿宋 f ) ;
lastrecText = 尾記錄 ;
lastrecClick += new SystemEventHandler ( GoLast ) ;
//以下是對為顯示數據記錄而設定的標簽和文本框進行初始化並把記錄綁定在不同的綁定到文本框Text屬性上
t_bookidLocation = new SystemDrawingPoint ( ) ;

  t_bookidTabIndex = ;
t_bookidSize = new SystemDrawingSize ( ) ;
t_bookidDataBindingsAdd ( Text myDataSet booksbookid ) ;

  t_booksLocation = new SystemDrawingPoint ( ) ;
t_booksTabIndex = ;
t_booksSize = new SystemDrawingSize ( ) ;
t_booksDataBindingsAdd ( Text myDataSet booksbookstock ) ;

  t_booktitleLocation = new SystemDrawingPoint ( ) ;
t_booktitleTabIndex = ;
t_booktitleSize = new SystemDrawingSize ( ) ;
t_booktitleDataBindingsAdd( Text myDataSet booksbooktitle ) ;

  t_bookpriceLocation = new SystemDrawingPoint ( ) ;
t_bookpriceTabIndex = ;
t_bookpriceSize = new SystemDrawingSize ( ) ;
t_bookpriceDataBindingsAdd ( Text myDataSet booksbookprice ) ;

  t_bookauthorLocation = new SystemDrawingPoint ( ) ;
t_bookauthorTabIndex = ;
t_bookauthorSize = new SystemDrawingSize ( ) ;
t_bookauthorDataBindingsAdd ( Text myDataSet booksbookauthor ) ;
l_bookidLocation = new SystemDrawingPoint ( ) ;
l_bookidText = 書本序號 ;
l_bookidSize = new SystemDrawingSize ( ) ;
l_bookidFont = new SystemDrawingFont ( 仿宋 f ) ;
l_bookidTabIndex = ;
l_bookidTextAlign = SystemDrawingContentAlignmentMiddleCenter ;
l_booktitleLocation = new SystemDrawingPoint ( ) ;
l_booktitleText = 書 名;
l_booktitleSize = new SystemDrawingSize ( ) ;
l_booktitleFont = new SystemDrawingFont ( 仿宋 f ) ;
l_booktitleTabIndex = ;
l_booktitleTextAlign = SystemDrawingContentAlignmentMiddleCenter ;

  l_bookpriceLocation = new SystemDrawingPoint ( ) ;
l_bookpriceText = 價 格 ;
l_bookpriceSize = new SystemDrawingSize ( ) ;
l_bookpriceFont = new SystemDrawingFont ( 仿宋 f ) ;

  l_bookpriceTabIndex = ;
l_bookpriceTextAlign = SystemDrawingContentAlignmentMiddleCenter ;

  l_booksLocation = new SystemDrawingPoint ( ) ;
l_booksText = 書 架 號 ;
l_booksSize = new SystemDrawingSize ( ) ;
l_booksFont = new SystemDrawingFont ( 仿宋 f ) ;
l_booksTabIndex = ;
l_booksTextAlign = SystemDrawingContentAlignmentMiddleCenter ;

  l_bookauthorLocation = new SystemDrawingPoint ( ) ;
l_bookauthorText = 作 者 ;
l_bookauthorSize = new SystemDrawingSize ( ) ;
l_bookauthorFont = new SystemDrawingFont ( 仿宋 f ) ;
l_bookauthorTabIndex = ;
l_bookauthorTextAlign = SystemDrawingContentAlignmentMiddleCenter ;

  labelLocation = new SystemDrawingPoint ( ) ;
labelText = 浏覽書籍信息 ;
labelSize = new SystemDrawingSize ( ) ;
labelForeColor = SystemDrawingColorGreen ;
labelFont = new SystemDrawingFont ( 仿宋 f ) ;
labelTabIndex = ;
//對窗體進行設定
thisText = 用C#做浏覽數據庫中記錄的程序!;
thisAutoScaleBaseSize = new SystemDrawingSize ( ) ;
thisFormBorderStyle = FormBorderStyleFixedSingle ;
thisClientSize = new SystemDrawingSize ( ) ;
//在窗體中加入組件
thisControlsAdd ( lastrec ) ;
thisControlsAdd ( nextrec ) ;
thisControlsAdd ( previousrec ) ;
thisControlsAdd ( firstrec ) ;
thisControlsAdd ( t_books ) ;
thisControlsAdd ( t_bookprice ) ;
thisControlsAdd ( t_bookauthor ) ;
thisControlsAdd ( t_booktitle ) ;
thisControlsAdd ( t_bookid ) ;
thisControlsAdd ( l_books ) ;
thisControlsAdd ( l_bookprice ) ;
thisControlsAdd ( l_bookauthor ) ;
thisControlsAdd ( l_booktitle ) ;
thisControlsAdd ( l_bookid ) ;
thisControlsAdd ( label ) ;
//把對象DataSet和books數據表綁定到此myBind對象
myBind= thisBindingContext [ myDataSet books ] ;
}
//按鈕尾記錄對象事件程序

  protected void GoLast ( object sender SystemEventArgs e )
{
myBindPosition = myBindCount ;
}

  //按鈕下一條對象事件程序
protected void GoNext ( object sender SystemEventArgs e )
{
if ( myBindPosition == myBindCount )
MessageBoxShow ( 已經到了最後一條記錄! ) ;
else
myBindPosition += ;
}
//按鈕上一條對象事件程序
protected void GoPrevious ( object sender SystemEventArgs e )
{
if ( myBindPosition == )
MessageBoxShow ( 已經到了第一條記錄! ) ;
else
myBindPosition = ;
}
//按鈕首記錄對象事件程序
protected void GoFirst ( object sender SystemEventArgs e )
{
myBindPosition = ;
}
}

  總結

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


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