Initial database
WinForm Design Diagram
In object-oriented thinking, we first encapsulate two classes: Userdate class deposit attribute, Userda class deposit method
Userdate class:
public class Userdate
{
private int _code;
public int Code//Encapsulation class
{
get {return _code;}
set {_code = value;}
}
private string _uname;
public string Uname
{
get {return _uname;}
set {_uname = value;}
}
private string _upass;
public string UPass
{
get {return _upass;}
set {_upass = value;}
}
}
Userda class:
Class Userda
{
Private SqlConnection _conn;
Private SqlCommand _cmd;
Public Userda ()
{
_conn = new SqlConnection ("server=.; Database=data1220;user=sa;pwd=sa ");
}
Add to
public void Insert (userdate data)
{
}
Modify
public void Update (userdate data)
{
}
Delete
public void Delete (int code)
{
}
Search All
Public list<userdate> Select2 ()
{
}
Query by user name
Public list<userdate> Select2 (string uname)
{
}
}
First, Increase:
Core code:
When you click Add:
private void Button1_Click (object sender, EventArgs e)
{
Userdate data = new Userdate ();
Data. Uname = TextBox2.Text;
Data. UPass = TextBox1.Text;
New Userda (). Insert (data);
}
Userda class: Insert method
public void Insert (userdate data)
{
_conn.open ();
_cmd = _conn.createcommand ();
_cmd.commandtext = "INSERT INTO table1 VALUES (@uname, @upass)";
_cmd.parameters.clear ();
_cmd.parameters.add ("@uname", data. Uname);
_cmd.parameters.add ("@upass", data. UPass);
_cmd.executenonquery ();
_conn.close ();
}
1. Increase the point by 2. Check it and add it.
3. There's a database.
Second, query:
Core code:
When you click on the query:
private void Select2_click (object sender, EventArgs e)
{
list<userdate> list= New Userda (). Select2 (TextBox1.Text);
Binding (list);
}
Encapsulating data functions
private void Binding (list<userdate> List)
{
ListView1.Columns.Clear ();
LISTVIEW1.COLUMNS.ADD ("number");
LISTVIEW1.COLUMNS.ADD ("User name");
LISTVIEW1.COLUMNS.ADD ("password");
ListView1.Items.Clear ();
foreach (userdate date in list)
{
ListViewItem lt = new ListViewItem ();
Lt. Text = date. Code.tostring ();
Lt. SubItems.Add (date. Uname);
Lt. SubItems.Add (date. UPass);
LISTVIEW1.ITEMS.ADD (LT);
}
}
Userda class: Two query functions (overloaded)
Search All
Public list<userdate> Select2 ()
{
List<userdate> list=new list<userdate> ();
_conn.open ();
_cmd = _conn.createcommand ();
_cmd.commandtext = "Select*from table1";
SqlDataReader dr = _cmd.executereader ();
while (Dr. Read ())
{
Userdate data = new Userdate ();
Data. Code = Int. Parse (dr["code"). ToString ());
Data. Uname = dr["Uname"]. ToString ();
Data. UPass = dr["UPass"]. ToString ();
List. ADD (data);
}
return list;
}
Query by user name (conditional query)
Public list<userdate> Select2 (string uname)
{
list<userdate> list = new list<userdate> ();
_conn.open ();
_cmd = _conn.createcommand ();
_cmd.commandtext = "SELECT * FROM table1 where uname like '%" +uname+ "% '";
SqlDataReader dr = _cmd.executereader ();
while (Dr. Read ())
{
Userdate data = new Userdate ();
Data. Code = Int. Parse (dr["code"). ToString ());
Data. Uname = dr["Uname"]. ToString ();
Data. UPass = dr["UPass"]. ToString ();
List. ADD (data);
}
return list;
}
1. Show all: 2. Fuzzy query by user name
Third, modify
Core code:
When clicked:
private void Button1_Click (object sender, EventArgs e)
{
Userdate data = new Userdate ();
Data. Code = Int. Parse (TextBox3.Text);
Data. Uname = TextBox2.Text;
Data. UPass = TextBox1.Text;
New Userda (). Update (data);
}
In the Userda class:
public void Update (userdate data)
{
_conn.open ();
_cmd = _conn.createcommand ();
_cmd.commandtext = "Update table1 set uname= '" +data. Uname+ "', upass= '" +data. Upass+ "' Where code=" +data. Code;
_cmd.executenonquery ();
_conn.close ();
}
1. Click Modify 2. The query changed after 3. The database has changed, too.
Iv. deletion
Core code:
When clicked:
private void Delete1_click (object sender, EventArgs e)
{
if (ListView1.SelectedItems.Count > 0)//SelectedItems selected
{
int code = Int. Parse (Listview1.selecteditems[0]. Text);
New Userda (). Delete (code);
}
Else
{
MessageBox.Show ("Please select First, then delete!") ");
}
list<userdate> list = new Userda (). Select2 ();
Binding (list);
}
Userda class:
public void Delete (int code)
{
_conn.open ();
_cmd = _conn.createcommand ();
_cmd.commandtext = "Delete from table1 where code=" +code;
_cmd.executenonquery ();
_conn.close ();
}
1. Click Delete: 2. After clicking Delete:
March 12 increase and deletion check