Research on bindingmanagerbase

Source: Internet
Author: User
Tags connectionstrings

Source code: http://download.csdn.net/source/1048586

App. config<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <configuration> <br/> <connectionstrings> <br/> <Add name = "connectionstring" <br/> connectionstring = "provider = Microsoft. jet. oledb.4.0; Data Source = | datadirectory |/appdata/DB. MDB; persist Security info = true "<br/> providername =" system. data. oledb "/> <br/> </connectionstrings> <br/> </configuration>Dataaccess. CSUsing system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> using system. data; <br/> using system. data. oledb; </P> <p> namespace hellovs2008.appcode <br/> {<br/> public class dataaccess <br/>{< br/> // Add reference system here. configuration, not just using. <br/> static string strcon = system. configuration. configurationmanager. connectionstrings ["connectionstring"]. tostring (); </P> <p> // <summary> <br/> // execute the query, return dataset <br/> /// </Summary> <br/> // <Param name = "sqlstr"> SQL statement </param> <br/> // /<Param name = "tablename"> table name </param> <br/> // <returns> </returns> <br/> Public static dataset getdataset (string sqlstr, string tablename) <br/>{< br/> oledbconnection conn = new oledbconnection (strcon); <br/> dataset DS = new dataset (); <br/> Conn. open (); <br/> oledbdataadapter da = new oledbdataadapter (sqlstr, Conn); <br/> da. fill (DS, tablename); <br/> Conn. close (); <br/> return Ds; <br/>}</P> <p> // <summary> <br/> // execute a non-query SQL statement <br/> /// </ summary> <br/> /// <Param name = "sqlstr"> </param> <br/> /// <returns> </returns> <br/> Public static int executesql (string sqlstr) <br/>{< br/> using (oledbconnection conn = new oledbconnection (strcon) <br/>{< br/> oledbcommand cmd = new oledbcommand (); <br/> cmd. connection = conn; <br/> cmd. commandtext = sqlstr; <br/> Conn. open (); <br/> cmd. executenonquery (); <br/> Conn. close (); <br/> return 1; <br/>}< br/>Dataform. CSUsing system; <br/> using system. collections. generic; <br/> using system. componentmodel; <br/> using system. data; <br/> using system. drawing; <br/> using system. LINQ; <br/> using system. text; <br/> using system. windows. forms; </P> <p> using system. data. oledb; </P> <p> namespace hellovs2008 <br/> {<br/> Public partial class dataform: Form <br/>{< br/> private dataset Ds; <br/> private bindingmanagerbase mybind; <Br/> Public dataform () <br/>{< br/> initializecomponent (); <br/>}</P> <p> private void dataform_load (Object sender, eventargs e) <br/>{< br/> DS = appcode. dataaccess. getdataset ("select * From workerinf", "workerinf"); <br/> // Add ("attributes to be bound ", "Control bound to the source", "attributes bound to the source"); <br/> this.txt bind. databindings. add ("text", DS, "workerinf. name "); <br/> // bind the title to the content in a textbox and synchronize it. <Br/> This. databindings. add ("text", txttext, "text"); <br/> // use form's bindingcontext to obtain bindingmanagerbase <br/> mybind = This. bindingcontext [ds, "workerinf"]; <br/>}</P> <p> private void btnnext_click (Object sender, eventargs E) <br/>{< br/> If (mybind. position = mybind. count-1) <br/> MessageBox. show ("the last record has been reached! "," Message prompt! ", Messageboxbuttons. OK, messageboxicon. information); <br/> else <br/> mybind. position + = 1; <br/>}</P> <p> private void btnprep_click (Object sender, eventargs e) <br/>{< br/> If (mybind. position = 0) <br/> MessageBox. show ("the first record has been reached! "," Message prompt! ", Messageboxbuttons. OK, messageboxicon. information); <br/> else <br/> mybind. position-= 1; <br/>}</P> <p> private void btnfirst_click (Object sender, eventargs e) <br/>{< br/> mybind. position = 0; <br/>}</P> <p> private void btnlast_click (Object sender, eventargs e) <br/>{< br/> mybind. position = mybind. count-1; <br/>}</P> <p> private void btnexecutesql_click (Object sender, eventargs e) <br/> {<Br/> // the key point is to execute database updates and update the data in the memory. Datarow <br/> // insertrecord (); <br/> // deleterecord (); <br/> UpdateRecord (); <br/>}</P> <p> protected void insertrecord () <br/>{< br/> try <br/>{< br/> // construct an SQL statement based on the input control value <br/> string strinsert = "insert into workerinf (name, sex) values ('gv ', 'male') "; <br/> // perform database operations <br/> appcode. dataaccess. executesql (strinsert); <br/> // updates memory data <br/> DS. tables ["workerinf"]. rows [mybind. position]. beginedit (); <Br/> // construct datarow <br/> datarow DR = Ds. tables ["workerinf"]. newrow (); <br/> Dr ["name"] = "hello"; <br/> Dr ["sex"] = "male"; <br/> DS. tables ["workerinf"]. rows. add (DR); <br/> DS. tables ["workerinf"]. rows [mybind. position]. endedit (); <br/> DS. tables ["workerinf"]. acceptchanges (); <br/>}< br/> catch (exception ed) <br/>{< br/> MessageBox. show ("adding data records" + ed. tostring (), "error! "); <Br/>}< br/> protected void deleterecord () <br/> {<br/> try <br/> {<br/> // delete a condition and obtain it from bindingmanagerbase <br/> // MessageBox. show (Ds. tables ["workerinf"]. rows [mybind. position] ["name"]. tostring (); </P> <p> // perform database operations <br/> string strinsert = "delete from workerinf where name = 'gv '"; <br/> appcode. dataaccess. executesql (strinsert); <br/> // update internal data <br/> DS. tables ["workerinf"]. rows [mybind. positi On]. beginedit (); <br/> DS. tables ["workerinf"]. rows [mybind. position]. delete (); <br/> DS. tables ["workerinf"]. rows [mybind. position]. endedit (); <br/> DS. tables ["workerinf"]. acceptchanges (); <br/>}< br/> catch (exception ed) <br/>{< br/> MessageBox. show ("deleting data records occurred" + ed. tostring (), "error! "); <Br/>}< br/> protected void UpdateRecord () <br/> {<br/> try <br/> {<br/> // perform database operations <br/> // string strinsert = "Update workerinf set name =' gv1 ', sex = 'female 'Where name = 'tree' "; <br/> // appcode. dataaccess. executesql (strinsert); <br/> // update internal data <br/> DS. tables ["workerinf"]. rows [mybind. position]. beginedit (); <br/> datarow DR = Ds. tables ["workerinf"]. rows [mybind. position]; <br/> Dr ["name"] =" Gv1 "; <br/> Dr [" sex "] =" female "; <br/> DS. tables ["workerinf"]. rows [mybind. position]. endedit (); <br/> DS. tables ["workerinf"]. acceptchanges (); <br/>}< br/> catch (exception ed) <br/>{< br/> MessageBox. show ("updating data records occurred" + ed. tostring (), "error! "); <Br/>}< br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.