C # Operations database with form application (add and revise)

Source: Internet
Author: User

In order to embody the idea of object-oriented, we put "adding and deleting" These functions into a database operation class;

In order to facilitate the data interaction between the form program and the database, we built a class with database row data, which conveniently transmits data between the form program and the database.

First, build the main form of the program

㈠ Adding data

When you click "Add", a subform pops up to add a piece of data to the database

Private void button1_click (object  sender, EventArgs e)        {            FORM5 insertnew  FORM5 ();              This ;            Insert. Show ();                }

Functions in the subform:

 Public Partial classForm5:form { PublicFORM5 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {    //storing data in a text box in a variable udata of type SqlDataSqlData Udata =NewSqlData (); Udata. Name=TextBox1.Text; Udata. Code=TextBox2.Text; NewSqlDA (). AddData (Udata);//call Add Data function to add data udata        }          }

Run programs, add data, view results

㈡ Querying data

The first query form,
First we need the window to load, to show all the data in the database

First, in the Sqlda class that holds the method, add a member method that gets the data, which returns a collection of data

//ways to query data         PublicList<sqldata>Select() {List<SqlData> list=NewList<sqldata>(); //Connect to the database, access the data_cmd =_con.            CreateCommand (); _con.            Open (); _cmd.commandtext="Select *from users"; SqlDataReader Dr=_cmd.            ExecuteReader (); //converts database data to SqlData type and into the list collection             while(Dr. Read ()) {SqlData data=NewSqlData (); Data. Code= dr["Code"].                ToString (); Data. Name= dr["name"].                ToString (); Data. PWD= dr["pwd"].                ToString (); List.            ADD (data); } _con.            Close (); returnlist; }

Then define a binding data function that binds the data returned by the above member method to ListView1, which is displayed

//Binding Data Functions         Public voidBinddata (list<sqldata>list) {            //Building a table structureLISTVIEW1.COLUMNS.ADD ("numbering"); LISTVIEW1.COLUMNS.ADD ("User name"); LISTVIEW1.COLUMNS.ADD ("Password"); //to put data in a collection into ListView1            foreach(SqlData datainchlist) {ListViewItem It=NewListViewItem ();//build a ListViewItem object that puts the data into the ListView1It. Text =data.                Code; It. SubItems.Add (data.                Name); It. SubItems.Add (data.                PWD); LISTVIEW1.ITEMS.ADD (it);//put the data in the ListView1 items collection,            }                    }

Then, in the form Load event, the delegate that binds the data function is appended

// form one load, bind data        in ListView1 Private void Form4_load (object  sender, EventArgs e)        {            binddata (new SqlDA ().  Select());        }

Run results

A second query form,
Enter the user name in the window and make a fuzzy query based on the user name

First write a query function that requires a parameter (user name), we can write a select overload function, as follows

//Method 2 for querying the data requires a string argument that is overloaded with the function above         PublicList<sqldata>Select(stringuname) {List<SqlData> list =NewList<sqldata>(); //Connect to the database, access the data_cmd =_con.            CreateCommand (); _con.            Open (); _cmd.commandtext="Select *from users where name like '%"+uname+"% '"; SqlDataReader Dr=_cmd.            ExecuteReader (); //converts the accessed database data to the SqlData type and into the list collection             while(Dr. Read ()) {SqlData data=NewSqlData (); Data. Code= dr["Code"].                ToString (); Data. Name= dr["name"].                ToString (); Data. PWD= dr["pwd"].                ToString (); List.            ADD (data); } _con.            Close (); returnlist; }

Click on the "Query" button

// querying data        by entering a user name Private void button4_click (object  sender, EventArgs e)        {            listView1.Items.Clear ();            Binddata (new SqlDA (). Select (TextBox1.Text));        }

㈢ Modifying data

First, set the Fullrowselect property to True

C # Operations database with form application (add and revise)

Related Article

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.