用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#比起用其他语言要显得麻烦些但对于程序设计人员却更灵活了使得程序设计人员有了更大的发展空间