分类

  • 软件天地

  • 用Visual C#往数据库中添加记录(上)‖
      (一).程序设计和运行的环境设置:

      (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 = Mi
    sample.mdb " ;
    crosoft.Jet.OLEDB.4.0 ; Data Source =

      OleDbConnection myConn = new Ole
    DbConnection ( strConn ) ;
      myConn.Open ( ) ;                                           
      string strInsert = "
    bookprice , bookstock )
    INSERT INTO books ( bookid
    VALUES ( " ;
    , booktitle , bookauthor ,

      strInsert += t_bookid.Text + ", '" ;     
      strInsert += t_booktitle.Text +
    "', '" ;

      strInsert += t_booka
    uthor.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 ;                   
      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{                                                                     
      //创建一个 OleDbConnection对象                             
      string strCon = " Pr
    sample.mdb" ;
    ovider = Microsoft.Jet.OLEDB

    .4.0 ; Data Source =

      OleDbConnection myConn = new Ole
    DbConnection ( strCon ) ;
      string strCom = " SELECT * FROM
    books " ;
      //创建一个 DataSet                                                 
      myDataSet = new DataSet ( ) ;                   
      myConn.Open ( ) ;                                           
      //用 OleDbDataAdapter 得到一个数据集                     
      OleDbDataAdapter myC
    ommand = new OleDbDataAdapte
    r ( strCom , myConn ) ;
      //把Dataset绑定books数据表                                     

      myCommand.Fill ( myD
    ataSet , "books" ) ;
      //关闭此OleDbConnection                                     
      myConn.Close ( ) ;                                         
      }                                                                           
      catch ( Exception e )                                   
      {                                                                           
      MessageBox.Show ( "连接错误! " +
    e.ToString ( ) , "错误" ) ;
      }                                                                           
      }                                                                           
      private void InitializeComponent ( )     
      {                                                                           
      components = new Sys
    tem.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 ( 6
    5 , 312 ) ;
      firstrec.ForeColor = System.Draw
    ing.Color.Black ;
      firstrec.Size = new
    System.Drawing.Size ( 40 , 2
    4 ) ;
      firstrec.Font = new System.Drawi
    ng.Font("仿宋", 8f );
      firstrec.Text = "首记录";                                 
      firstrec.Click += ne
    w System.EventHandler(GoFirs
    t);
      previousrec.Location
    = new System.Drawing.Point
    ( 135 , 312 ) ;
      previousrec.ForeColor = System.D
    rawing.Color.Black ;
      previousrec.Size = n
    ew System.Drawing.Size(40, 2
    4) ;
      previousrec.Font = new System.Dr
    awing.Font ( "仿宋" , 8f ) ;
      previousrec.Text = "上一条" ;                         
      previousrec.Click += new System.
    EventHandler ( GoPrevious ) ;
      nextrec.Location = n
    ew System.Drawing.Point ( 20
    5 , 312 );
      nextrec.ForeColor =
    System.Drawing.Color.Black ;

      nextrec.Size = new System.Drawin
    g.Size ( 40 , 24 ) ;
      nextrec.Font = new System.Drawin
    g.Font ( "仿宋" , 8f ) ;
      nextrec.Text = "下一条" ;                                 
      nextrec.Click += new
    System.EventHandler ( GoNex
    t );
      lastrec.Location = n
    ew System.Drawing.Point ( 27
    5 , 312 ) ;
      lastrec.ForeColor =
    System.Drawing.Color.Black ;

      lastrec.Size = new S
    ystem.Drawing.Size ( 40 , 24
    ) ;
      lastrec.Font = new S
    ystem.Drawing.Font ( "仿宋"
    , 8f ) ;
      lastrec.Text = "尾记录" ;                                 
      lastrec.Click += new System.Even
    tHandler ( GoLast ) ;
      //以下是对显示标签进行初始化                                               
      l_bookid.Location =
    new System.Drawing.Point ( 2
    4 , 56 ) ;
      l_bookid.Text = "书本序号:" ;                           
      l_bookid.Size = new
    System.Drawing.Size ( 112, 2
    0 ) ;
      l_bookid.Font = new System.Drawi
    ng.Font ( "仿宋" , 10f ) ;
      l_bookid.TextAlign =
    System.Drawing.ContentAlign
    ment.MiddleCenter ;
      l_booktitle.Location = new Syste
    m.Drawing.Point ( 24 , 108 ) ;
      l_booktitle.Text = "书 名:";                         
      l_booktitle.Size = n
    ew System.Drawing.Size ( 112
    , 20 ) ;
      l_booktitle.Font = n
    ew System.Drawing.Font ( "仿
    宋" , 10f ) ;
      l_booktitle.TextAlign = System.D
    rawing.ContentAlignment.MiddleCenter ;
      l_bookprice.Location
    = new System.Drawing.Point
    ( 24 , 212 ) ;
      l_bookprice.Text = "价 格:" ;                       
      l_bookprice.Size = new System.Dr
    awing.Size ( 112 , 20 ) ;
      l_bookprice.Font = new System.Dr
    awing.Font ( "仿宋" , 10f ) ;
      l_bookprice.TextAlig
    n = System.Drawing.ContentAl
    ignment.MiddleCenter ;
      l_bookstock.Location = new Syste
    m.Drawing.Point ( 24 , 264 ) ;
      l_bookstock.Text = "书 架 号:" ;                   
      l_bookstock.Size = new System.Dr
    awing.Size ( 112 , 20 ) ;
      l_bookstock.Font = new System.Dr
    awing.Font ( "仿宋" , 10f ) ;
      l_bookstock.TabIndex = 16 ;                       
      l_bookstock.TextAlign = System.D
    rawing.ContentAlignment.MiddleCenter ;
      l_bookauthor.Location = new Syst
    em.Drawing.Point ( 24 , 160 ) ;
      l_bookauthor.Text = "作 者:" ;                     
      l_bookauthor.Size =
    new System.Drawing.Size ( 11
    2 , 20 ) ;
      l_bookauthor.Font =
    new System.Drawing.Font ( "
    仿宋" , 10f ) ;
      l_bookauthor.TextAlign = System.
    Drawing.ContentAlignment.MiddleCenter ;
      title.Location = new System.Draw
    ing.Point ( 32 , 16 ) ;
      title.Text = "利用Vsiual C#来增加数据记录!" ;     
      title.Size = new System.Drawing.
    Size ( 336 , 24 ) ;
      title.ForeColor = System.Drawing
    .Color.Green ;
      title.Font = new Sys
    wing.FontStyle.Bold ) ;
    tem.Drawing.Font ( "仿宋" ,

    14f , System.Dra

      //以下是对为显示数据记录而设定的
    定到文本框"Text"属性上
    标签和文本框进行初始化,并把记录绑定在不同的绑

      t_bookid.Location = new System.D
    rawing.Point ( 184 , 56 ) ;
      t_bookid.Size = new
    System.Drawing.Size ( 80 , 2
    0 ) ;
      t_bookid.DataBindings.Add ( "Tex
    t" , myDataSet , "books.bookid" ) ;
      t_bookstock.Location
    = new System.Drawing.Point
    ( 184 , 264 ) ;
      t_bookstock.Size = new System.Dr
    awing.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.Dr
    awing.Size ( 176 , 20 ) ;
      t_booktitle.DataBindings.Add( "T
    ext" , myDataSet , "books.booktitle" ) ;
      t_bookprice.Location
    = new System.Drawing.Point
    ( 184 , 212 ) ;
      t_bookprice.Size = new System.Dr
    awing.Size ( 80 , 20 ) ;
      t_bookprice.DataBind
    ings.Add ( "Text" , myDataSe
    t , "books.bookprice" ) ;
      t_bookauthor.Location = new Syst
    em.Drawing.Point ( 184 , 160 ) ;
      t_bookauthor.Size = new System.D
    rawing.Size ( 128 , 20 ) ;
      t_bookauthor.DataBin
    dings.Add ( "Text" , myDataS
    et , "books.bookauthor" ) ;
      t_new.Location = new
    System.Drawing.Point ( 62 ,
    354 ) ;
      t_new.Size = new Sys
    tem.Drawing.Size ( 96 , 32 )
    ;
      t_new.Text = "新建记录" ;                                   
      t_new.Click += new System.EventH
    andler ( t_newClick ) ;
      save.Location = new System.Drawi
    ng.Point ( 222 , 354 ) ;
      save.Size = new Syst
    em.Drawing.Size ( 96 , 32 )
    ;
      save.TabIndex = 4 ;                                       
      save.Text = "保存记录" ;                                     
      save.Click += new Sy
    stem.EventHandler ( saveClic
    k ) ;
      this.Text = "利用Vsi
    ual C#来增加数据记录的程序窗
    口!" ;
      this.AutoScaleBaseSi
    ze = new System.Drawing.Size
    ( 5 , 13 ) ;
      this.FormBorderStyle = FormBorde
    rStyle.Fixed3D ;
      this.ClientSize = ne
    w 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.Binding
    Context [ myDataSet , "books
    " ] ;
      }                                                                           
      protected void saveClick ( objec
    t sender , System.EventArgs e )
      {                                                                           
      try                                                                       
      {                                                                           
      //判断所有字段是否添完,添完则执行,反之弹出提示                           
      if ( t_bookid.Text !
    && t_bookprice.Text != "
    = "" && t_booktitle.Text !=
    " && t_bookstock.Text != ""
    "" && t_bookauthor.Text != ""
    )
      {                                                                           
      string strConn = " P
    sample.mdb " ;
    rovider = Microsoft.Jet.OLED

    B.4.0 ; Data Source =

      OleDbConnection myCo
    nn = new OleDbConnection ( s
    trConn ) ;
      myConn.Open ( ) ;                                           
      string strInsert = " INSERT INTO
    bookprice , bookstock ) VALUES ( " ;
    books ( bookid , booktitle , bookauthor ,

      strInsert += t_bookid.Text + ", '" ;     
      strInsert += t_booktitle.Text +
    "', '" ;
      strInsert += t_bookauthor.Text +
    "', " ;
      strInsert += t_bookprice.Text + ", " ;
      strInsert += t_bookstock.Text + ")" ;   
      OleDbCommand inst = new OleDbCom
    mand ( strInsert , myConn ) ;
      inst.ExecuteNonQuery ( ) ;                         
      myConn.Close ( ) ;                                         
      }                                                                           
      else                                                                     
      {                                                                           
      MessageBox.Show ( "必须填满所有
    字段值!" , "错误!" ) ;
      }                                                                           
      }                                                                           
      catch ( Exception ed )                                 
      {                                                                           
      MessageBox.Show ( "保存数据记录
    发生 " + ed.ToString ( ) , "错误!" ) ;
      }                                                                           
      }                                                                           
      protected void t_new
    Click ( object sender , Syst
    em.EventArgs e )
      {                                                                           
      t_bookid.Text = "" ;                                     
      t_booktitle.Text = "" ;                               
      t_bookauthor.Text = "" ;                             
      t_bookprice.Text = "" ;                               
      t_bookstock.Text = "" ;                               
      }                                                                           
      //按钮"尾记录"对象事件程序                                               
      protected void GoLast ( object s
    ender , System.EventArgs e )
      {                                                                           
      myBind.Position = myBind.Count - 1 ;     
      }                                                                           

      //按钮"下一条"对象事件程序                                               
      protected void GoNext ( object s
    ender , System.EventArgs e )
      {                                                                           
      if ( myBind.Position == myBind.C
    ount -1 )
      MessageBox.Show ( "已经到了最后一条记录!" ) ;       
      else                                                                     
      myBind.Position += 1 ;                                 
      }                                                                           
      //按钮"上一条"对象事件程序                                               
      protected void GoPrevious ( obje
    ct 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 ;                                   
      }                                                                           
      }                                                                            

    上一页 下一页




    map