Create a two class:
Users Category:
Using system;using system.collections.generic;using system.linq;using System.text;namespace entity Class _ Data Access class. app_code{public class Users {private int _ids; <summary>//IDs//</summary> public int IDs {get {return _ids ; } set {_ids = value;} } private string _username; <summary>///user name///</summary> public string UserName {get {ret Urn _username; } set {_username = value;} } private string _password; <summary>//password///</summary> public string PassWord {get {Retu RN _password; } set {_password = value;} } private string _nickname; <summary>//Nickname///</summary> public string Nickname {get {Retu RN _nickname; } set {_nickname = ValUe }} private bool _sex; <summary>///gender///</summary> public bool Sex {get {return _sex ; } set {_sex = value;} } private DateTime _birthday; <summary>//Birthdays///</summary> public DateTime Birthday {get {RE Turn _birthday; } set {_birthday = value;} } private string _nation; <summary>//National///</summary> public string Nation {get {return} _nation; } set {_nation = value;} } private string _class; <summary>///class///</summary> public string class {get {return _class; } set {_class = value;} } }}
Usersdata:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Data.SqlClient; Namespace entity class _ Data Access class. app_code{public class Usersdata {SqlConnection conn = null; SqlCommand cmd = null; Public Usersdata () {conn = new SqlConnection ("server=.; Database=data0928;user=sa;pwd=123 "); CMD = conn. CreateCommand (); }///<summary>///Add data to the Users table, return True to add success///</summary>//<param name= " U "> Users object to add to datasheet </param>//<returns></returns> public bool Insert (Users u) {bool OK = false; int count = 0; Cmd.commandtext = "INSERT into Users values (@a,@b,@c,@d,@e,@f,@g)"; Cmd. Parameters.clear (); Cmd. Parameters.addwithvalue ("@a", u.username); Cmd. Parameters.addwithvalue ("@b", U.password); Cmd. Parameters.addwithvalue ("@c", u.nickname); CmD.parameters.addwithvalue ("@d", u.sex); Cmd. Parameters.addwithvalue ("@e", u.birthday); Cmd. Parameters.addwithvalue ("@f", u.nation); Cmd. Parameters.addwithvalue ("@g", U.class); try {Conn. Open (); Count = cmd. ExecuteNonQuery (); } catch {ok = false;} finally {Conn. Close (); } if (Count > 0) ok = true; return OK; }//Querying all data in database tables public list<users> Select () {list<users> List = new List<u Sers> (); Cmd.commandtext = "Select *from Users"; Conn. Open (); SqlDataReader dr = cmd. ExecuteReader (); if (Dr. HasRows) {while (Dr. Read ()) {Users u = new Users (); U.ids = Convert.ToInt32 (dr["Ids"]); U.username = dr["UsErname "]. ToString (); U.password = dr["PassWord"]. ToString (); U.nickname = dr["nickname"]. ToString (); U.sex = Convert.toboolean (dr["Sex"]); U.birthday = Convert.todatetime (dr["Birthday"]); U.nation = dr["Nation"]. ToString (); U.class = dr["Class"]. ToString (); List. ADD (U); }} conn. Close (); return list; }//Query the data for the specified user name bool Select (string username) {bool ok = false; Cmd.commandtext = "Select *from Users where UserName = @a"; Cmd. Parameters.clear (); Cmd. Parameters.Add ("@a", username); Users u = new users (); Conn. Open (); SqlDataReader dr = cmd. ExecuteReader (); if (Dr. HasRows) {while (Dr. Read ()) {u.ids = Convert.toInt32 (dr["IDs"]); U.username = dr["UserName"]. ToString (); U.password = dr["PassWord"]. ToString (); U.nickname = dr["nickname"]. ToString (); U.sex = Convert.toboolean (dr["Sex"]); U.birthday = Convert.todatetime (dr["Birthday"]); U.nation = dr["Nation"]. ToString (); U.class = dr["Class"]. ToString (); OK = true; }} conn. Close (); return OK; }//delete data public void Delete (string uname) {cmd. CommandText = "Delete from Users where UserName = @a"; Cmd. Parameters.clear (); Cmd. Parameters.Add ("@a", uname); Conn. Open (); Cmd. ExecuteNonQuery (); Conn. Close (); } }}
The method is called in the main function:
Using system;using system.collections.generic;using system.linq;using system.text;using entity Class _ Data Access class. App_code;namespace entity Class _ Data Access class {class program {static void Main (string[] args) {//list<u Sers> ulist=new list<users>; UserData da=new UserData (); Ulist=da.select (); list<users> ulist = new Usersdata (). Select (); foreach (Users uuu in Ulist) {Console.WriteLine (UUU. Ids + "" + UUU. UserName + "" + UUU. PassWord + "" + UUU. Nickname + "" + UUU. Sex + "" + UUU. Birthday + "" + UUU. Nation + "" + UUU. Class); } console.write ("Please enter the user name to delete:"); String uname = Console.ReadLine (); if (new Usersdata (). Select (uname)) {Console.WriteLine ("This user information has been found, do you want to delete it?") (y/n) "); String yn = Console.ReadLine (); if (yn. ToUpper () = = "Y") {new Usersdata (). DelEte (uname); Console.WriteLine ("Delete succeeded! "); }} The users user = new users (); Console.Write ("Please enter user name:"); User. UserName = Console.ReadLine (); Console.Write ("Please enter password:"); User. PassWord = Console.ReadLine (); Console.Write ("Please enter a nickname:"); User. Nickname = Console.ReadLine (); Console.Write ("Please enter Gender:"); User. Sex = Convert.toboolean (Console.ReadLine ()); Console.Write ("Please enter your Birthday:"); User. Birthday = Convert.todatetime (Console.ReadLine ()); Console.Write ("Please enter the Nation:"); User. Nation = Console.ReadLine (); Console.Write ("Please enter class:"); User. Class = Console.ReadLine (); Usersdata ud = new Usersdata (); BOOL isOK = UD. Insert (user); if (isOK) Console.WriteLine ("Add success! "); else Console.WriteLine ("Add failed! "); Console.readkey (); } }}
Important!!! entity classes, data access classes