新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → 用Visual C#来增加数据记录(1) 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 2254 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 用Visual C#来增加数据记录(1) 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     admin 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      头衔:W3China站长
      等级:计算机硕士学位(管理员)
      文章:5255
      积分:18406
      门派:W3CHINA.ORG
      注册:2003/10/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给admin发送一个短消息 把admin加入好友 查看admin的个人资料 搜索admin在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给admin  访问admin的主页 引用回复这个贴子 回复这个贴子 查看admin的博客楼主
    发贴心情 用Visual C#来增加数据记录(1)


    发信人: cataract (大瀑布来啦), 信区: DotNET        
    标  题: 用Visual C#来增加数据记录(1)  
    发信站: BBS 水木清华站 (Mon Dec 10 18:12:58 2001)


    发信人: xixixi (wenwen), 信区: dotNET
    标  题: 用Visual C#来增加数据记录(1)
    发信站: 飘渺水云间 (Sun Dec  9 21:13:10 2001), 转信

    在本篇文章中,我们将介绍Visual C#对数据库的一个基本操作,即:如何往数据
    库中
    添加记录。我们将通过一些数据库操作的例子,来具体说明一下。为了更清楚的说
    明这
    个问题,在选用数据库方面采用了二种当前比较典型的数据库,其一是本地数据库
    --Ac
    cess 2000,另外一个是远程数据库--SQL SERVER 7.0。首先介绍如何用Visual  
    C#来添
    加Access 2000数据库的记录。
    一.用Visual C#来添加Access 2000数据库的记录
    (一).程序设计和运行的环境设置:
    (1)视窗2000服务器版
    (2)Microsoft Data Acess Component 2.6 以上版本 ( MDAC 2.6 )
    (3)本文程序使用的数据库的介绍:
    程序中使用的数据库名称为sample.mdb,在此数据库中有一张数据表books。此数
    据表的
    结构如下:
    字段名称 字段类型 代表意思
    Bookid 数字 序号
    booktitle 文本 书籍名称
    bookauthor 文本 书籍作者
    bookprice 数字 价格
    bookstock 数字 书架号
    (二).程序设计难点和应该注意的问题:
    如何正确的往数据库中添加记录是本文要讨论的一个重点和难点,下面就是解决这
    一问
    题的具体思路:
    (1)创建并打开一个 OleDbConnection对象。
    (2)创建一个插入一条记录的SQL语句。
    (3)创建一个OleDbCommand对象。
    (4)通过此OleDbCommand对象完成对插入一条记录到数据库的操作。
    以下是在程序中实现的具体语句:
    string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =  
    sample
    .mdb " ;
    OleDbConnection myConn = new OleDbConnection ( strConn ) ;
    myConn.Open ( ) ;
    string strInsert = " INSERT INTO books ( bookid , booktitle , bookauthor
    , b
    ookprice , bookstock ) VALUES ( " ;
    strInsert += t_bookid.Text + ", '" ;
    strInsert += t_booktitle.Text + "', '" ;
    strInsert += t_bookauthor.Text + "', " ;
    strInsert += t_bookprice.Text + ", " ;
    strInsert += t_bookstock.T
    bookauthor 文本 书籍作者
    bookprice 数字 价格
    bookstock 数字 书架号
    (二).程序设计难点和应该注意的问题:
    如何正确的往数据库中添加记录是本文要讨论的一个重点和难点,下面就是解决这
    一问
    题的具体思路:
    (1)创建并打开一个 OleDbConnection对象。
    (2)创建一个插入一条记录的SQL语句。
    (3)创建一个OleDbCommand对象。
    (4)通过此OleDbCommand对象完成对插入一条记录到数据库的操作。
    以下是在程序中实现的具体语句:
    string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =  
    sample
    .mdb " ;
    OleDbConnection myConn = new OleDbConnection ( strConn ) ;
    myConn.Open ( ) ;
    string strInsert = " INSERT INTO books ( bookid , booktitle , bookauthor
    , b
    ookprice , bookstock ) VALUES ( " ;
    strInsert += t_bookid.Text + ", '" ;
    strInsert += t_booktitle.Text + "', '" ;
    strInsert += t_bookauthor.Text + "', " ;
    strInsert += t_bookprice.Text + ", " ;
    strInsert += t_bookstock.Text + ")" ;
    OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;
    inst.ExecuteNonQuery ( ) ;
    myConn.Close ( ) ;
    (三).用Visual C#来插入记录的程序源代码( add.cs )和执行后的界面:
    下图是add.cs编译后的执行界面:
    add.cs源程序代码:
    using System ;
    using System.Drawing ;
    using System.ComponentModel ;
    using System.Windows.Forms ;
    using System.Data.OleDb ;
    using System.Data ;
    //导入程序中使用到的名称空间
    public class DataAdd : Form {
    private Button lastrec ;
    private Button nextrec ;
    private Button previousrec ;
    private Button firstrec ;
    private Container components ;
    private Label title ;
    private Button t_new ;
    private Button save ;
    private TextBox t_bookstock ;
    private TextBox t_bookprice ;
    private TextBox t_bookauthor ;
    private TextBox t_booktitle ;
    下图是add.cs编译后的执行界面:
    add.cs源程序代码:
    using System ;
    using System.Drawing ;
    using System.ComponentModel ;
    using System.Windows.Forms ;
    using System.Data.OleDb ;
    using System.Data ;
    //导入程序中使用到的名称空间
    public class DataAdd : Form {
    private Button lastrec ;
    private Button nextrec ;
    private Button previousrec ;
    private Button firstrec ;
    private Container components ;
    private Label title ;
    private Button t_new ;
    private Button save ;
    private TextBox t_bookstock ;
    private TextBox t_bookprice ;
    private TextBox t_bookauthor ;
    private TextBox t_booktitle ;
    }
    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 ( )
    {
    components = new System.ComponentModel.Container ( ) ;
    nextrec = new Button ( ) ;
    lastrec = new Button ( ) ;
    previousrec = new Button ( ) ;
    firstrec = new Button ( ) ;
    t_bookprice = new TextBox ( ) ;
    l_booktitle = new Label ( ) ;
    l_bookprice = new Label ( ) ;
    l_bookauthor = new Label ( ) ;
    t_bookid = new TextBox ( ) ;
    save = new Button ( ) ;
    title = new Label ( ) ;
    t_bookauthor = new TextBox ( ) ;
    t_booktitle = new TextBox ( ) ;
    t_new = new Button ( ) ;
    l_bookstock = new Label ( ) ;
    t_bookstock = new TextBox ( ) ;
    l_bookid = new Label ( ) ;
    //以下是对数据浏览的四个按钮进行初始化
    firstrec.Location = new System.Drawing.Point ( 65 , 312 ) ;
    firstrec.ForeColor = System.Drawing.Color.Black ;
    firstrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
    firstrec.Font = new System.Drawing.Font("仿宋", 8f );
    firstrec.Text = "首记录";
    firstrec.Click += new System.EventHandler(GoFirst);
    previousrec.Location = new System.Drawing.Point ( 135 , 312 ) ;
    previousrec.ForeColor = System.Drawing.Color.Black ;
    previousrec.Size = new System.Drawing.Size(40, 24) ;
    previousrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
    previousrec.Text = "上一条" ;
    previousrec.Click += new System.EventHandler ( GoPrevious ) ;
    nextrec.Location = new System.Drawing.Point ( 205 , 312 );
    nextrec.ForeColor = System.Drawing.Color.Black ;
    nextrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
    nextrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
    nextrec.Text = "下一条" ;
    nextrec.Click += new System.EventHandler ( GoNext );
    lastrec.Location = new System.Drawing.Point ( 275 , 312 ) ;
    lastrec.ForeColor = System.Drawing.Color.Black ;
    lastrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
    lastrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
    lastrec.Text = "尾记录" ;
    lastrec.Click += new System.EventHandler ( GoLast ) ;
    //以下是对显示标签进行初始化
    l_bookid.Location = new System.Drawing.Point ( 24 , 56 ) ;
    l_bookid.Text = "书本序号:" ;
    l_bookid.Size = new System.Drawing.Size ( 112, 20 ) ;
    l_bookid.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
    l_bookid.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    l_booktitle.Location = new System.Drawing.Point ( 24 , 108 ) ;
    l_booktitle.Text = "书 名:";
    l_booktitle.Size = new System.Drawing.Size ( 112 , 20 ) ;
    l_booktitle.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
    l_booktitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    l_bookprice.Location = new System.Drawing.Point ( 24 , 2
    nextrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
    nextrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
    nextrec.Text = "下一条" ;
    nextrec.Click += new System.EventHandler ( GoNext );
    lastrec.Location = new System.Drawing.Point ( 275 , 312 ) ;
    lastrec.ForeColor = System.Drawing.Color.Black ;
    lastrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
    l_bookstock.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
    l_bookstock.TabIndex = 16 ;
    l_bookstock.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    l_bookauthor.Location = new System.Drawing.Point ( 24 , 160 ) ;
    l_bookauthor.Text = "作 者:" ;
    l_bookauthor.Size = new System.Drawing.Size ( 112 , 20 ) ;
    l_bookauthor.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
    l_bookauthor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;

    title.Location = new System.Drawing.Point ( 32 , 16 ) ;
    title.Text = "利用Vsiual C#来增加数据记录!" ;
    title.Size = new System.Drawing.Size ( 336 , 24 ) ;
    title.ForeColor = System.Drawing.Color.Green ;
    title.Font = new System.Drawing.Font ( "仿宋" , 14f , System.Drawing.
    FontSty
    le.Bold ) ;
    //以下是对为显示数据记录而设定的标签和文本框进行初始化,并把记录绑定在不
    同的
    绑定到文本框"Text"属性上
    t_bookid.Location = new System.Drawing.Point ( 184 , 56 ) ;
    t_bookid.Size = new System.Drawing.Size ( 80 , 20 ) ;
    t_bookid.DataBindings.Add ( "Text" , myDataSet , "books.bookid" ) ;
    t_bookstock.Location = new System.Drawing.Point ( 184 , 264 ) ;
    t_bookstock.Size = new System.Drawing.Size ( 80 , 20 ) ;
    t_bookstock.DataBindings.Add ( "Text" , myDataSet , "books.bookstock"  
    ) ;
    t_booktitle.Location = new System.Drawing.Point ( 184 , 108 ) ;
    t_booktitle.Size = new System.Drawing.Size ( 176 , 20 ) ;
    t_booktitle.DataBindings.Add( "Text" , myDataSet , "books.booktitle" )  
    ;
    t_bookprice.Location = new System.Drawing.Point ( 184 , 212 ) ;
    t_bookprice.Size = new System.Drawing.Size ( 80 , 20 ) ;
    t_bookprice.DataBindings.Add ( "Text" , myDataSet , "books.bookprice"  
    ) ;
    t_bookauthor.Location = new System.Drawing.Point ( 184 , 160 ) ;
    t_bookauthor.Size = new System.Drawing.Size ( 128 , 20 ) ;
    t_bookauthor.DataBindings.Add ( "Text" , myDataSet , "books.
    bookauthor" ) ;
    t_new.Location = new System.Drawing.Point ( 62 , 354 ) ;
    t_new.Size = new System.Drawing.Size ( 96 , 32 ) ;
    t_new.Text = "新建记录" ;
    t_new.Click += new System.EventHandler ( t_newClick ) ;
    save.Location = new System.Drawing.Point ( 222 , 354 ) ;
    save.Size = new System.Drawing.Size ( 96 , 32 ) ;
    save.TabIndex = 4 ;
    save.Text = "保存记录" ;
    save.Click += new System.EventHandler ( saveClick ) ;
    this.Text = "利用Vsiual C#来增加数据记录的程序窗口!" ;
    this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
    thisokstock.Size = new System.Drawing.Size ( 80 , 20 ) ;
    t_bookstock.DataBindings.Add ( "Text" , myDataSet , "books.bookstock"  
    ) ;
    t_booktitle.Location = new System.Drawing.Point ( 184 , 108 ) ;
    t_booktitle.Size = new System.Drawing.Size ( 176 , 20 ) ;
    t_booktitle.DataBindings.Add( "Text" , myDataSet , "books.booktitle" )  
    ;
    t_bookprice.Location = new System.Drawing.Point ( 184 , 212 ) ;
    t_bookprice.Size = new System.Drawing.Size ( 80 , 20 ) ;
    t_bookprice.DataBindings.Add ( "Text" , myDataSet , "books.bookprice"  
    ) ;
    t_bookauthor.Location = new System.Drawing.Point ( 184 , 160 ) ;
    t_bookauthor.Size = new System.Drawing.Size ( 128 , 20 ) ;
    t_bookauthor.DataBindings.Add ( "Text" , myDataSet , "books.
    bookauthor" ) ;
    t_new.Location = new System.Drawing.Point ( 62 , 354 ) ;
    t_new.Size = new System.Drawing.Size ( 96 , 32 ) ;
    t_new.Text = "新建记录" ;
    t_new.Click += new System.EventHandler ( t_newClick ) ;
    save.Location = new System.Drawing.Point ( 222 , 354 ) ;
    save.Size = new System.Drawing.Size ( 96 , 32 ) ;
    save.TabIndex = 4 ;
    save.Text = "保存记录" ;
    save.Click += new System.EventHandler ( saveClick ) ;
    this.Text = "利用Vsiual C#来增加数据记录的程序窗口!" ;
    this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
    this.FormBorderStyle = FormBorderStyle.Fixed3D ;
    this.ClientSize = new System.Drawing.Size ( 390 , 400 ) ;
    {
    try
    {
    //判断所有字段是否添完,添完则执行,反之弹出提示
    if ( t_bookid.Text != "" && t_booktitle.Text != "" &&
    this.Controls.Add ( title ) ;
    this.Controls.Add ( t_new ) ;
    this.Controls.Add ( save ) ;
    this.Controls.Add ( t_bookstock ) ;
    this.Controls.Add ( t_bookprice ) ;
    this.Controls.Add ( t_bookauthor ) ;
    this.Controls.Add ( t_booktitle ) ;
    this.Controls.Add ( t_bookid ) ;
    this.Controls.Add ( l_bookstock ) ;
    this.Controls.Add ( l_bookprice ) ;
    this.Controls.Add ( l_bookauthor ) ;
    this.Controls.Add ( l_booktitle ) ;
    this.Controls.Add ( l_bookid ) ;
    //把对象DataSet和"books"数据表绑定到此myBind对象
    myBind= this.BindingContext [ myDataSet , "books" ] ;
    }
    protected void saveClick ( object sender , System.EventArgs e )
    {
    try
    {
    //判断所有字段是否添完,添完则执行,反之弹出提示
    if ( t_bookid.Text != "" && t_booktitle.Text != "" && t_bookauthor.
    Text != "
    " && t_bookprice.Text != "" && t_bookstock.Text != "" )
    {
    string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =  
    sample
    .mdb " ;
    OleDbConnection myConn = new OleDbConnection ( strConn ) ;
    myConn.Open ( ) ;
    string strInsert = " INSERT INTO books ( bookid , booktitle , bookauthor
    , b
    ookprice , bookstock ) VALUES ( " ;
    strInsert += t_bookid.Text + ", '" ;
    strInsert += t_booktitle.Text + "', '" ;
    strInsert += t_bookauthor.Text + "', " ;
    strInsert += t_bookprice.Text + ", " ;
    strInsert += t_bookstock.Text + ")" ;
    OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;
    inst.ExecuteNonQuery ( ) ;
    myConn.Close ( ) ;
    }
    }
    //按钮"下一条"对象事件程序
    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 ( "已经到了第一条记录?
    t_bookauthor.Text = "" ;
    t_bookprice.Text = "" ;
    t_bookstock.Text = "" ;
    }
    //按钮"尾记录"对象事件程序
    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#来增加数据记录(2)
    发信站: 飘渺水云间 (Sun Dec  9 21:13:35 2001), 转信

    二.用Visual C#往SQL SERVER数据库中插入记录:
    (1)用Visual C#往Access 2000和SQL SERVER添加记录的主要区别在于使用了不
    同的数
    据库引擎。在编写程序之前,首先假设数据库服务器名称为:server1,要访问的
    数据库
    名称为:data1,数据表名称为:books。用户名为:sa。其中数据表的数据结构和
    Acce
    ss 2000的表的结构相同。下面是程序中打开SQL SERVER的数据引擎程序代码:
    // 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称
    为ser
    ver1,数据库为data1
    string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ;
    Use
    r ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ;
    OleDbConnection myConn = new OleDbConnection ( strCon ) ;
    myConn.Open ( ) ;
    (2).用Visual C#往SQL SERVER 数据库中插入记录的源程序代码( add02.cs ):

    using System ;

    using System.Drawing ;
    using System.ComponentModel ;
    using System.Windows.Forms ;
    using System.Data.OleDb ;
    using System.Data ;
    //导入程序中使用到的名称空间
    public class DataAdd : Form {
    private Button lastrec ;
    private Button nextrec ;
    private Button previousrec ;
    private Button firstrec ;
    private Container components ;
    private Label title ;
    private Button t_new ;
    private Button save ;
    private TextBox t_bookstock ;
    private TextBox t_bookprice ;
    private TextBox t_bookauthor ;
    private TextBox t_booktitle ;
    private TextBox t_bookid ;
    private Label l_bookstock ;
    private Label l_bookprice ;
    private Label l_bookauthor ;
    private Label l_booktitle ;
    private Label l_bookid ;
    private DataSet myDataSet ;
    private BindingManagerBase myBind ;
    //定义在程序中要使用的组件
    public DataAdd ( ) {
    //连接到一个数据库
    GetConnected ( ) ;
    // 对窗体中所需要的内容进行初始化
    InitializeComponent ( );
    }
    //释放程序使用过的所以资源
    public override void Dispose ( ) {
    base.Dispose ( ) ;
    components.Dispose ( ) ;
    }
    public static void Main ( ) {
    Application.Run ( new DataAdd ( ) ) ;
    }
    public void GetConnected ( )
    {
    try{
    // 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称
    为ser
    ver1,数据库为data1,用户名为sa。
    string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ;
    Use
    r ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ;
    OleDbConnection myConn = new OleDbConnection ( strCon ) ;
    myConn.Open ( ) ;
    string strCom = " SELECT * FROM books " ;
    //创建一个 DataSet
    myDataSet = new DataSet ( ) ;
    //用 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 ( )
    {
    components = new System.ComponentModel.Container ( ) ;
    nextrec = new Button ( ) ;
    lastrec = new Button ( ) ;
    previousrec = new Button ( ) ;
    firstrec = new Button ( ) ;
    t_bookprice = new TextBox ( ) ;
    l_booktitle = new Label ( ) ;
    l_bookprice = new Label ( ) ;
    l_bookauthor = new Label ( ) ;
    t_bookid = new TextBox ( ) ;
    save = new Button ( ) ;
    title = new Label ( ) ;
    t_bookauthor = new TextBox ( ) ;
    t_booktitle = new TextBox ( ) ;
    t_new = new Button ( ) ;
    l_bookstock = new Label ( ) ;
    t_bookstock = new TextBox ( ) ;
    l_bookid = new Label ( ) ;
    //以下是对数据浏览的四个按钮进行初始化
    firstrec.Location = new System.Drawing.Point ( 65 , 312 ) ;
    firstrec.ForeColor = System.Drawing.Color.Black ;
    firstrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
    firstrec.Font = new System.Drawing.Font("仿宋", 8f );
    firstrec.Text = "首记录";
    firstrec.Click += new System.EventHan
    previousrec = new Button ( ) ;
    firstrec = new Button ( ) ;
    t_bookprice = new TextBox ( ) ;
    l_booktitle = new Label ( ) ;
    l_bookprice = new Label ( ) ;
    l_bookauthor = new Label ( ) ;
    t_bookid = new TextBox ( ) ;
    save = new Button ( ) ;
    title = new Label ( ) ;
    t_bookauthor = new TextBox ( ) ;
    t_booktitle = new TextBox ( ) ;
    t_new = new Button ( ) ;
    l_bookstock = new Label ( ) ;
    t_bookstock = new TextBox ( ) ;
    l_bookid = new Label ( ) ;
    //以下是对数据浏览的四个按钮进行初始化
    firstrec.Location = new System.Drawing.Point ( 65 , 312 ) ;
    firstrec.ForeColor = System.Drawing.Color.Black ;
    firstrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
    firstrec.Font = new System.Drawing.Font("仿宋", 8f );
    firstrec.Text = "首记录";
    firstrec.Click += new System.EventHandler(GoFirst);
    previousrec.Location = new System.Drawing.Point ( 135 , 312 ) ;
    previousrec.ForeColor = System.Drawing.Color.Black ;
    previousrec.Size = new System.Drawing.Size(40, 24) ;
    previousrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
    previousrec.Text = "上一条" ;
    previousrec.Click += new System.EventHandler ( GoPrevious ) ;
    nextrec.Location = new System.Drawing.Point ( 205 , 312 );
    nextrec.ForeColor = System.Drawing.Color.Black ;
    nextrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
    nextrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
    nextrec.Text = "下一条" ;
    nextrec.Click += new System.EventHandler ( GoNext );
    lastrec.Location = new System.Drawing.Point ( 275 , 312 ) ;
    lastrec.ForeColor = System.Drawing.Color.Black ;
    lastrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
    lastrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
    lastrec.Text = "尾记录" ;
    lastrec.Click += new System.EventHandler ( GoLast ) ;
    //以下是对显示标签进行初始化
    l_bookid.Location = new System.Drawing.Point ( 24 , 56 ) ;
    l_bookid.Text = "书本序号:" ;
    l_bookid.Size = new System.Drawing.Size ( 112, 20 ) ;
    l_bookid.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
    title.Location = new System.Drawing.Point ( 32 , 16 ) ;
    title.Text = "利用Vsiual C#来增加数据记录!" ;
    title.Size = new System.Drawing.Size ( 336 , 24 ) ;
    title.ForeColor = System.Drawing.Color.Green ;
    title.Font = new System.Drawing.Font ( "仿宋" , 14f , System.Drawing.
    FontSty
    le.Bold ) ;
    //以下是对为显示数据记录而设定的标签和文本框进行初始化,并把记录绑定在不
    同的
    绑定到文本框"Text"属性上
    t_bookid.Location = new System.Drawing.Point ( 184 , 56 ) ;
    t_bookid.Size = new System.Drawing.Size ( 80 , 20 ) ;
    t_bookid.DataBindings.Add ( "Text" , myDataSet , "books.bookid" ) ;
    t_bookstock.Location = new System.Drawing.Point ( 184 , 264 ) ;
    t_bookstock.Size = new System.Drawing.Size ( 80 , 20 ) ;
    t_bookstock.DataBindings.Add ( "Text" , myDataSet , "books.bookstock"  
    ) ;
    t_booktitle.Location = new System.Drawing.Point ( 184 , 108 ) ;
    t_booktitle.Size = new System.Drawing.Size ( 176 , 20 ) ;
    t_booktitle.DataBindings.Add( "Text" , myDataSet , "books.booktitle" )  
    ;
    t_bookprice.Location = new System.Drawing.Point ( 184 , 212 ) ;
    t_bookprice.Size = new System.Drawing.Size ( 80 , 20 ) ;
    t_bookprice.DataBindings.Add ( "Text" , myDataSet , "books.bookprice"  
    ) ;
    t_bookauthor.Location = new System.Drawing.Point ( 184 , 160 ) ;
    t_bookauthor.Size = new System.Drawing.Size ( 128 , 20 ) ;
    t_bookauthor.DataBindings.Add ( "Text" , myDataSet , "books.
    bookauthor" ) ;
    t_new.Location = new System.Drawing.Point ( 62 , 354 ) ;
    t_new.Size = new System.Drawing.Size ( 96 , 32 ) ;
    t_new.Text = "新建记录" ;
    t_new.Click += new System.EventHandler ( t_newClick ) ;
    save.Location = new System.Drawing.Point ( 222 , 354 ) ;
    save.Size = new System.Drawing.Size ( 96 , 32 ) ;
    save.TabIndex = 4 ;
    save.Text = "保存记录" ;
    save.Click += new System.EventHandler ( saveClick ) ;
    this.Text = "利用Vsiual C#来增加数据记录的程序窗口!" ;
    this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
    this.FormBorderStyle = FormBorderStyle.Fixed3D ;
    this.ClientSize = new System.Drawing.Size ( 390 , 400 ) ;
    //在窗体中加入下列组件
    this.Controls.Add ( lastrec ) ;
    this.Controls.Add ( nextrec ) ;
    this.Controls.Add ( previousrec ) ;
    this.Controls.Add ( firstrec ) ;
    this.Controls.Add ( title ) ;
    this.Controls.Add ( t_new ) ;
    this.Controls.Add ( save ) ;
    this.Controls.Add ( t_bookstock ) ;
    this.Controls.Add ( t_bookprice ) ;
    this.Controls.Add ( t_bookauthor ) ;
    this.Controls.Add ( t_booktitle ) ;
    this.Controls.Add ( t_bookid ) ;
    this.Controls.Add ( l_bookstock ) ;
    this.Controls.Add ( l_bookprice ) ;
    this.Controls.Add ( l_bookauthor ) ;
    this.Controls.Add ( l_booktitle ) ;
    this.Controls.Add ( l_bookid ) ;
    //把对象DataSet和"books"数据表绑定到此myBind对象
    myBind= this.BindingContext [ myDataSet , "books" ] ;
    }
    protected void saveClick ( object sender , System.EventArgs e )
    {
    try
    {
    //判断所有字段是否添完,添完则执行,反之弹出提示
    if ( t_bookid.Text != "" && t_booktitle.Text != "" && t_bookauthor.
    Text != "
    " && t_bookprice.Text != "" && t_bookstock.Text != "" )
    {
    this.Controls.Add ( save ) ;
    this.Controls.Add ( t_bookstock ) ;
    this.Controls.Add ( t_bookprice ) ;
    this.Controls.Add ( t_bookauthor ) ;
    this.Controls.Add ( t_booktitle ) ;
    this.Controls.Add ( t_bookid ) ;
    this.Controls.Add ( l_bookstock ) ;
    this.Controls.Add ( l_bookprice ) ;
    this.Controls.Add ( l_bookauthor ) ;
    this.Controls.Add ( l_booktitle ) ;
    this.Controls.Add ( l_bookid ) ;
    //把对象DataSet和"books"数据表绑定到此myBind对象
    myBind= this.BindingContext [ myDataSet , "books" ] ;
    }
    protected void saveClick ( object sender , System.EventArgs e )
    {
    try
    {
    //判断所有字段是否添完,添完则执行,反之弹出提示
    if ( t_bookid.Text != "" && t_booktitle.Text != "" && t_bookauthor.
    Text != "
    " && t_bookprice.Text != "" && t_bookstock.Text != "" )
    {
    // 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称
    为ser
    {
    MessageBox.Show ( "保存数据记录发生 " + ed.ToString ( ) , "错误!" ) ;
    }
    }
    protected void t_newClick ( object sender , System.EventArgs
    string strInsert = " INSERT INTO books ( bookid , booktitle , bookauthor
    , b
    ookprice , bookstock ) VALUES ( " ;
    strInsert += t_bookid.Text + ", '" ;
    strInsert += t_booktitle.Text + "', '" ;
    strInsert += t_bookauthor.Text + "', " ;
    strInsert += t_bookprice.Text + ", " ;
    strInsert += t_bookstock.Text + ")" ;
    OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;
    inst.ExecuteNonQuery ( ) ;

    myConn.Close ( ) ;
    }
    else
    {
    MessageBox.Show ( "必须填满所有字段值!" , "错误!" ) ;
    }
    }
    catch ( Exception ed )
    {
    MessageBox.Show ( "保存数据记录发生 " + ed.ToString ( ) , "错误!" ) ;
    }
    }
    protected void t_newClick ( object sender , System.EventArgs e )
    {
    t_bookid.Text = "" ;
    t_booktitle.Text = "" ;
    t_bookauthor.Text = "" ;
    t_bookprice.Text = "" ;
    t_bookstock.Text = "" ;
    }
    //按钮"尾记录"对象事件程序
    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#如何往远程数据库--SQL  
    SERVE
    R和本地数据库-- Access 2000中插入记录。对于其他类型的数据库也可以比照这
    二个这
    二个数据库来处理,一般来说,用Visual C#处理数据库只是在选用数据库引擎上
    有较大
    的差别,在程序中的具体设计和处理上,还是很类似的。最后希望此篇文章能对你
    的数
    标  题: 用Visual C#中轻松浏览数据库记录
    发信站: 飘渺水云间 (Sun Dec  9 21:14:00 2001), 转信

    用Delphi或者VB编程,在对数据库中的记录进行操作的时候,经常用到一个名称为
    数据
    导航器的组件,通过这个组件,可以非常方便的实现对已经绑定到此组件的数据表
    中的
    记录进行浏览。就是所谓的上一条记录、下一条记录、首记录、尾记录等。那么在
    Visu
    al C#是否也存在这样的组件呢?答案是否定的。但由于Visual C#有着强大的数据
    库处
    理能力,所以可以比较方便的做一个类似于此组件的程序。本文就是来介绍此程序
    的具
    体制作过程。
    一、 程序的主要功能介绍:
    程序打开本地Acess数据库(sample.mdb)中的book数据表,然后把book数据表中

    字段绑定到程序提供的文本框中,显示出来。通过程序中的四个按钮"首记录"、"
    尾记录
    "、"上一条"、"下一条",实现对book数据表中的记录浏览。程序的运行界面如下

    图01:对数据表中记录浏览程序的运行界面
    二、程序设计和运行的环境设置:
    (1)视窗2000服务器版
    (2)Microsoft Acess Data Component 2.6 ( MADC 2.6 )
    三、程序设计难点和应该注意的问题:
    (1)如何实现把数据表中的字段用文本框来显示:
    如果直接把字段的值赋值给文本框,这时如果用"下一条"等按钮来浏览数据记录的
    时候
    ,文本框的值是不会变化的。如何让文本框根据数据表中的记录指针来动态的显示
    要字
    段值,这是本文的一个重点,也是一个难点。
    本文是通过把数据表中的字段值绑定到文本框的"Text"属性上,来实现动态显示字
    段数
    值的。实现这种处理要用到文本框的DataBindings属性和其中的Add方法。具体语
    法如下

    文本组件名称.DataBindings.Add ( "Text" , DataSet对象 , 数据表和字段名称
    ) ;
    在程序具体如下:
    t_bookid.DataBindings.Add ( "Text" , myDataSet , "books.bookid" ) ;
    这样就可以根据记录指针来实现要显示的字段值了。
    (2)如何改变记录指针:
    只有掌握如何改变记录指针,才可以随心所欲的浏览记录。Visual C#改变记录指
    针是通
    过一个命叫BindingManagerBase对象来实现的。此对象封装在名称空间System.
    Windows
    .Froms中。BindingManagerBase对象是一个抽象的对象,管理所有绑定的同类的数
    据源
    和数据成员。在程序设计中主要用到BindingManagerBase对象中的二个属性,即:
    Posi
    tion属性和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.Point ( 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.Point ( 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.Point ( 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.Point ( 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.Point ( 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.Point ( 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.Point ( 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.Point ( 184 , 212 ) ;
    t_bookpriccation = new System.Drawing.Point ( 265 , 312 ) ;
    t_bookprice.Size = new System.Drawing.Size ( 80 , 20 ) ;
    t_bookprice.DataBindings.Add ( "Text" , myDataSet , "books.bookprice"  
    ) ;
    t_bookauthor.Location = new System.Drawing.Point ( 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.Point ( 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.Point ( 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.Point ( 24 , 212 ) ;
    l_bookprice.Text = "价 格:" ;
    l_bookprice.Size = new System.Drawing.Size ( 112 , 20 ) ;
    l_bookprice.Font = new 12 ;
    l_bookprice.TabIndex = 15 ;
    l_bookprice.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    l_books.Location = new System.Drawing.Point ( 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.Point ( 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.Point ( 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#做浏览数据库中记录的程序!";
    l_bookprice.TabIndex = 15 ;
    l_bookprice.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    l_books.Location = new System.Drawing.Point ( 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.Point ( 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.Point ( 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 GoFirst ( object sender , System.EventArgs e )
    {
    myBind.Position = 0 ;
    }
    }
    五.总结:
    本文的重点就在于如何用Visual C#改变数据集的记录指针和如何让文本框根据记
    录指针
    的变化而改变显示内容。虽然此类处理在Visual C#比起用其他语言要显得麻烦些
    。但对
    于程序设计人员却更灵活了,使得程序设计人员有了更大的发展空间。

    --
    我早已习惯无情的风雨
    但不能没有你……


    ※ 来源:·飘渺水云间 Freecity.dhs.org·[FROM: xixixi]

    --

    ※ 来源:·BBS 水木清华站 smth.org·[FROM: 166.111.214.13]
    上一篇
    返回上一页
    回到目录
    回到页首
    下一篇


       收藏   分享  
    顶(0)
      




    ----------------------------------------------

    -----------------------------------------------

    第十二章第一节《用ROR创建面向资源的服务》
    第十二章第二节《用Restlet创建面向资源的服务》
    第三章《REST式服务有什么不同》
    InfoQ SOA首席编辑胡键评《RESTful Web Services中文版》
    [InfoQ文章]解答有关REST的十点疑惑

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/11/9 2:25:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给Google AdSense  访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/3 11:30:47

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    6,046.875ms