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)