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; } } }
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 [email protected]", 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 [email protected],[email protected],[email protected],[email protected] where [email protected]"; 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 [email protected]", 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; } } }
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);
(2)用户列表:
Service1Client ent = new Service1Client(); this.Repeater1.DataSource = ent.UserList(); this.Repeater1.DataBind();
(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"); }
(4)删除用户:
//keyIdx 为从repeater获得的用户id Service1Client sc = new Service1Client(); sc.UserDelete(int.Parse(keyIdx));
(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();
时间: 2024-10-10 19:30:34