1、契約(介面):定義用戶實體類User、需要實現的服務 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Servi ...
1、契約(介面):定義用戶實體類User、需要實現的服務
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 { //新增用戶 [OperationContract] int UserAdd(User model); //用戶列表 [OperationContract] DataTable UserList(); //根據id獲得用戶對象 [OperationContract] User UserGet(int idx); //編輯用戶 [OperationContract] bool UserUpdate(User model); //刪除用戶 [OperationContract] bool UserDelete(int idx); //根據篩選條件獲得用戶列表 [OperationContract] DataTable UserSearch(Dictionary<string,string> parameters); } //用戶實體類 [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、服務:實現契約定義的服務
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 { //新增用戶 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; } //用戶列表 public DataTable UserList() { string sql = "select * from wcfUser"; return SqlHelper.ExecuteDataset(sql).Tables[0]; } //根據id獲得用戶對象 public User UserGet(int idx) { DataTable dt = SqlHelper.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; } //編輯用戶 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 SqlParameter("@createdate", model.createdate) , new SqlParameter("@idx", model.idx) ); return rows > 0; } //刪除用戶 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; } //根據搜索條件獲得用戶列表 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 轉換成 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、客戶端
(1)新增用戶:
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)用戶列表:
Service1Client ent = new Service1Client(); this.Repeater1.DataSource = ent.UserList(); this.Repeater1.DataBind();View Code
(3)編輯用戶:
//獲取用戶id string strIdx = Request["idx"]; Service1Client sc = new Service1Client(); //根據id獲得用戶對象 User ent = sc.UserGet(int.Parse(strIdx)); ent.uName = this.uName.Text; ent.discription = this.discription.Text; //編輯用戶 if (sc.UserUpdate(ent)) { Response.Redirect("User_manage.aspx"); }View Code
(4)刪除用戶:
//keyIdx 為從repeater獲得的用戶id Service1Client sc = new Service1Client(); sc.UserDelete(int.Parse(keyIdx));View Code
(5)查詢:
Service1Client sc = new Service1Client(); //查詢條件 Dictionary<string, string> dic = new Dictionary<string, string>(); dic.Add("keywords", this.txtKeywords.Text); //重新綁定repeater this.Repeater1.DataSource = sc.UserSearch(dic); this.Repeater1.DataBind();View Code