首页 > 数据库 > Access > 正文

续(这个例子是针对ACCESS的更新、删除、还有定位的,希望能对你有所帮助)

2024-09-07 19:04:55
字体:
来源:转载
供稿:网友
            // btn_help
            //
            this.btn_help.forecolor = system.drawing.systemcolors.activecaptiontext;
            this.btn_help.location = new system.drawing.point(360, 280);
            this.btn_help.name = "btn_help";
            this.btn_help.size = new system.drawing.size(88, 24);
            this.btn_help.tabindex = 3;
            this.btn_help.text = "帮 助(&h)";
            this.btn_help.click += new system.eventhandler(this.btn_help_click);
            //
            // label5
            //
            this.label5.backcolor = system.drawing.color.olive;
            this.label5.borderstyle = system.windows.forms.borderstyle.fixedsingle;
            this.label5.forecolor = system.drawing.systemcolors.activecaptiontext;
            this.label5.location = new system.drawing.point(48, 192);
            this.label5.name = "label5";
            this.label5.size = new system.drawing.size(152, 24);
            this.label5.tabindex = 1;
            this.label5.text = "市场价格:";
            this.label5.textalign = system.drawing.contentalignment.middlecenter;
            //
            // txt_bookstock
            //
            this.txt_bookstock.borderstyle = system.windows.forms.borderstyle.fixedsingle;
            this.txt_bookstock.location = new system.drawing.point(232, 240);
            this.txt_bookstock.name = "txt_bookstock";
            this.txt_bookstock.size = new system.drawing.size(216, 21);
            this.txt_bookstock.tabindex = 2;
            this.txt_bookstock.text = "";
            //
            // dataedit
            //
            this.autoscalebasesize = new system.drawing.size(6, 14);
            this.autoscroll = true;
            this.backcolor = system.drawing.color.olive;
            this.clientsize = new system.drawing.size(536, 389);
            this.controls.addrange(new system.windows.forms.control[] {
                                                                          this.statusbar,
                                                                          this.btn_help,
                                                                          this.txt_bookid,
                                                                          this.label2,
                                                                          this.label1,
                                                                          this.label3,
                                                                          this.label4,
                                                                          this.label5,
                                                                          this.label6,
                                                                          this.txt_bookname,
                                                                          this.txt_authorname,
                                                                          this.txt_bookprice,
                                                                          this.txt_bookstock,
                                                                          this.btn_delete,
                                                                          this.btn_update,
                                                                          this.btn_movenext,
                                                                          this.btn_movefirst,
                                                                          this.btn_moveprevious,
                                                                          this.btn_movelast});
            this.name = "dataedit";
            this.startposition = system.windows.forms.formstartposition.centerscreen;
            this.text = "dataedit";
            this.windowstate = system.windows.forms.formwindowstate.maximized;
            this.resumelayout(false);

        }
        #endregion

        private void moveprevious()
        {
            if (mybind.position == 0)
            {
                statusbar.text = "数据已经到头";
            }
            else
            {
                mybind.position -= 1;
                statusbar.text = "当前位置:" +mybind.position.tostring();
            }
        }

        private void movefirst()
        {
            mybind.position = 0;
            statusbar.text = "当前位置:" +mybind.position.tostring();
        }

        private void movenext()
        {
            if (mybind.position == mybind.count-1)
            {
                statusbar.text = "数据已经到尾";
            }
            else
            {
                mybind.position += 1;
                statusbar.text = "当前位置:" +mybind.position.tostring();
            }
        }

        private void movelast()
        {
            mybind.position = mybind.count-1;
            statusbar.text = "当前位置:" +mybind.position.tostring();
        }

        private void btn_movefirst_click(object sender, system.eventargs e)
        {
            movefirst();
        }

        private void btn_moveprevious_click(object sender, system.eventargs e)
        {
            moveprevious();
        }

        private void btn_movenext_click(object sender, system.eventargs e)
        {
            movenext();
        }

        private void btn_movelast_click(object sender, system.eventargs e)
        {
            movelast();
        }

        private void btn_help_click(object sender, system.eventargs e)
        {
            messagebox.show("数据库操作练习——定位、更新、删除",system.datetime.now.toshortdatestring());
        }

        private void btn_delete_click(object sender, system.eventargs e)
        {
            try
            {
                string strconn = "provider=microsoft.jet.oledb.4.0;data source=book.mdb";
                oledbconnection conn = new oledbconnection(strconn);
                
                conn.open();
                string strdelete = "select * from bookstock";
                oledbdataadapter adapter = new oledbdataadapter(strdelete,conn);

                mydataset.tables["bookstock"].rows[mybind.position].delete();
                adapter.update(mydataset,"bookstock");

                statusbar.text = "记录已经删除";
                conn.close();
            }
            catch(exception ed)
            {
                statusbar.text = "发生错误,原因:" + ed.message;
                messagebox.show(ed.tostring());
            }
        }

        private void btn_update_click(object sender, system.eventargs e)
        {
            try
            {
                string strconn = "provider=microsoft.jet.oledb.4.0;data source=book.mdb";
                oledbconnection conn = new oledbconnection(strconn);

                conn.open();

                string strupdt = "update bookstock set booktitle = '"
                    + txt_bookname.text + "',bookauthor = '"
                    + txt_authorname.text + "',bookprice = "
                    + txt_bookprice.text +",bookstock = "
                    + txt_bookstock.text +" where bookid = " + txt_bookid.text;
                oledbcommand comm = new oledbcommand(strupdt,conn);
                comm.executenonquery();

                statusbar.text = "更新成功!";
                conn.close();

                mydataset = null;
                mybind = null;

                if(isbound)
                {
                    txt_bookid.databindings.clear();
                    txt_bookname.databindings.clear();
                    txt_authorname.databindings.clear();
                    txt_bookprice.databindings.clear();
                    txt_bookstock.databindings.clear();
                    isbound = false;
                }

                getconnected();
            }
            catch(exception eu)
            {
                statusbar.text = "发生错误,原因:" + eu.message;
            }
        }
    }
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表