A simple example (add, delete, modify, query, and object class) is provided for beginners in WCF.

Source: Internet
Author: User

A simple example (add, delete, modify, query, and object class) is provided for beginners in WCF.

1. Contract (Interface): defines User entity class users and services to be implemented

Using System; using System. collections. generic; using System. linq; using System. runtime. serialization; using System. serviceModel; using System. serviceModel. web; using System. text; using System. data; namespace WcfService {[ServiceContract] public interface IService1 {// Add User [OperationContract] int UserAdd (User model); // User list [OperationContract] DataTable UserList (); // obtain the User object [OperationContract] User UserGet (int idx) based on the id; // edit the User [OperationContract] bool UserUpdate (User model ); // Delete the user [OperationContract] bool UserDelete (int idx); // obtain the user list [OperationContract] DataTable UserSearch (Dictionary <string, string> parameters) according to the filtering conditions );} // User entity class [DataContract] public class User {[DataMember] public int idx {get; set;} [DataMember] public string uName {get; set ;} [DataMember] public string uPwd {get; set;} [DataMember] public string discription {get; set ;}[ DataMember] public DateTime createdate {get; set ;}}}View Code


2. Service: services defined in the contract

Using System; using System. collections. generic; using System. linq; using System. runtime. serialization; using System. serviceModel; using System. serviceModel. web; using System. text; using System. data; using System. data. sqlClient; namespace WcfService {public class Service1: IService1 {// Add public int UserAdd (User model) {string SQL = "insert into wcfUser (uName, uPwd, discription, createdate) values (@ uName ,@ UPwd, @ discription, @ createdate); select @ identity; "; int idx = Convert. toInt32 (SqlHelper. executeScalar (CommandType. text, SQL, new SqlParameter ("@ uName", model. uName), new SqlParameter ("@ uPwd", model. uPwd), new SqlParameter ("@ discription", model. discription), new SqlParameter ("@ createdate", model. createdate); return idx;} // user list public DataTable UserList () {string SQL = "select * from wcf User "; return SqlHelper. executeDataset (SQL ). tables [0];} // obtain the User object public User UserGet (int idx) {DataTable dt = SqlHelper according to the id. executeDataset (CommandType. text, "select * from dbo. wcfUser where idx = @ idx ", new SqlParameter (" @ idx ", idx )). tables [0]; if (dt. rows. count> 1) {throw new Exception ("more than 1 row was found");} if (dt. rows. count <= 0) {return null;} DataRow row = dt. rows [0]; User model = ToModel (row); return model;} // edit User public bool UserUpdate (User model) {string SQL = "update wcfUser set uName = @ uName, uPwd = @ uPwd, discription = @ discription, createdate = @ createdate where idx = @ idx "; int rows = SqlHelper. executeNonQuery (CommandType. text, SQL, new SqlParameter ("@ uName", model. uName), new SqlParameter ("@ uPwd", model. uPwd), new SqlParameter ("@ discription", model. discription), new S QlParameter ("@ createdate", model. createdate), new SqlParameter ("@ idx", model. idx); return rows> 0;} // Delete the user public bool UserDelete (int idx) {int rows = SqlHelper. executeNonQuery (CommandType. text, "delete from dbo. wcfUser where idx = @ idx ", new SqlParameter (" @ idx ", idx); return rows> 0 ;} // obtain the user list public DataTable UserSearch (Dictionary <string, string> parameters) {string SQL = "select * From wcfUser where 1 = 1 "; string strWhere =" "; if (! String. isNullOrEmpty (parameters ["keywords"]) {strWhere = strWhere + "and (uName like '%" + parameters ["keywords"] + "%' or discription like '%" + parameters ["keywords"] + "% ') ";} SQL = SQL + strWhere; return SqlHelper. executeDataset (SQL ). tables [0];} // DataRow is converted to model private static User ToModel (DataRow row) {User model = new User (); model. idx = row. isNull ("idx ")? 0: (System. Int32) row ["idx"]; model. uName = row. IsNull ("uName ")? Null: (System. String) row ["uName"]; model. uPwd = row. IsNull ("uPwd ")? Null: (System. String) row ["uPwd"]; model. discription = row. IsNull ("discription ")? "": (System. String) row ["discription"]; model. createdate = row. IsNull ("createdate ")? DateTime. Now: (System. DateTime) row ["createdate"]; return model ;}}}View Code


3. Client

(1) New users:

Service1Client SC = new Service1Client (); User ent = new User (); ent. uName = this. uName. text; ent. uPwd = this. uPwd. text; ent. discription = this. discription. text; ent. createdate = DateTime. now; int idx = SC. userAdd (ent );View Code

(2) User List:

Service1Client ent = new Service1Client (); this. Repeater1.DataSource = ent. UserList (); this. Repeater1.DataBind ();View Code

(3) edit the user:

// Obtain the User idstring strIdx = Request ["idx"]; Service1Client SC = new Service1Client (); // obtain the User Object User ent = SC by id. userGet (int. parse (strIdx); ent. uName = this. uName. text; ent. discription = this. discription. text; // edit the user if (SC. userUpdate (ent) {Response. redirect ("User_manage.aspx ");}View Code

(4) Delete A User:

// KeyIdx is the user idService1Client SC = new Service1Client () obtained from repeater; SC. UserDelete (int. Parse (keyIdx ));View Code

(5) query:

Service1Client SC = new Service1Client (); // query condition Dictionary <string, string> dic = new Dictionary <string, string> (); dic. add ("keywords", this.txt Keywords. text); // rebind repeater this. repeater1.DataSource = SC. userSearch (dic); this. repeater1.DataBind ();View Code

 

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.