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 build a class with database row data, which conveniently transmits the data between the form program and the database;
We create a new folder, put these two classes in this folder (APP), later, the namespace can be used to write the two classes.
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 insert= new FORM5 (); Insert. Owner = this; Insert. Show (); }
Functions in the subform:
public partial class Form5:form {public FORM5 () { InitializeComponent (); } private void Button1_Click (object sender, EventArgs e) { //The data in the text box is stored in a variable of type SqlData udata sqldata udata = New SqlData (); Udata. Name = TextBox1.Text; Udata. Code = TextBox2.Text; New SqlDA (). AddData (Udata);//Call Add Data function, 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
Method of querying data public list<sqldata> Select () { list<sqldata> list=new list<sqldata> (); Connect to database, Access data _cmd = _con. CreateCommand (); _con. Open (); _cmd.commandtext = "Select *from users"; SqlDataReader dr= _cmd. ExecuteReader (); Converts the database data into a sqldata type and puts it into the list collection while (Dr. Read ()) { sqldata data=new sqldata (); Data. Code = dr["Code"]. ToString (); Data. Name = dr["Name"]. ToString (); Data. PWD = dr["pwd"]. ToString (); List. ADD (data); } _con. Close (); return list; }
Then define a binding data function that binds the data returned by the above member method to ListView1, which is displayed
Bind data function public void binddata (list<sqldata> list) { //Build table Structure LISTVIEW1.COLUMNS.ADD ("number" ); LISTVIEW1.COLUMNS.ADD ("User name"); LISTVIEW1.COLUMNS.ADD ("password"); Put the data in the collection into ListView1 foreach (sqldata data in list) { ListViewItem it = new ListViewItem ();// Build a ListViewItem object that puts the data into ListView1 it. Text = data. Code; It. SubItems.Add (data. Name); It. SubItems.Add (data. PWD); LISTVIEW1.ITEMS.ADD (it);//Put data into ListView1 's 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 data requires a string parameter, which is overloaded with the above function public list<sqldata> Select (String uname) { list<sqldata> List = new list<sqldata> (); Connect to database, Access 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 = new SqlData (); Data. Code = dr["Code"]. ToString (); Data. Name = dr["Name"]. ToString (); Data. PWD = dr["pwd"]. ToString (); List. ADD (data); } _con. Close (); return list; }
Click on the "Query" button
Query data by entering the 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 of the main window to true to make the row data selectable
Click "Modify" to bring up the following "modify" Subform
Modifying the data does not require a number modification, and the Enabled property of the first text box of the Modify subform is set to false.
Code for "Modify" the Subform
public partial class Form6:form {public Form6 () { InitializeComponent (); } Overrides the Modify window's constructor, which displays the data for the selected item public FORM6 (sqldata data) { InitializeComponent (); Textbox3.text=data. Code; Textbox1.text=data. Name; Textbox2.text=data. PWD; } private void Button1_Click (object sender, EventArgs e) { //converts the input into SqlData format sqldata data = new SqlData ( ); Data. Code = TextBox3.Text; Data. Name = TextBox1.Text; Data. PWD = TextBox2.Text; Call Modify Data function new SqlDA (). Update (data); }
}
Functions to modify data
Method of modifying data public void Update (sqldata data) { //open connection, stitching command _con. Open (); _cmd = _con. CreateCommand (); _cmd.commandtext = "Update users set [email protected],[email protected] where [email protected]"; _cmd. Parameters.Add ("@name", data. Name); _cmd. Parameters.Add ("@pwd", data. PWD); _cmd. Parameters.Add ("@code", data. Code); Execute the command to close the connection _cmd. ExecuteNonQuery (); _con. Close (); }
After entering the username and password, click "Modify" and click "Query" in the main window to see that the data has been modified.
㈢ Deleting data
Method of deleting data public void Delete (SqlData data) { //open connection, stitching command _con. Open (); _cmd = _con. CreateCommand (); _cmd.commandtext = "Delete from users where [email protected]"; _cmd. Parameters.Add ("@code", data. Code); Execute the command to close the connection _cmd. ExecuteNonQuery (); _con. Close (); }
C # Operations database with form application (add and revise)