初学WCF,一个简单的示例(增删改查,实体类)

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-08-10 02:33:57

初学WCF,一个简单的示例(增删改查,实体类)的相关文章

完整增删改查 实体类 数据访问

完整修改和查询:中间变量运用. 1.先查 2.执行操作 查询                Console.WriteLine("学号" + "\t" + "姓名" + "\t" + "性别" + "\t" + "    " + "生日" + "\t" + "民族" + "\t");   

【Mybatis】简单的mybatis增删改查模板

简单的mybatis增删改查模板: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.sunee

Mybatis实现简单的数据库增删改查操作

Mybatis实现简单的数据库增删改查操作 框架:mybatis(3.5.2) 数据库:mysql 工具:idea 1.新建一个maven项目,在pom文件中添加mybatis依赖及MySQL依赖 <!-- mybatis核心依赖 --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId&g

受老师邀请给学院国创队伍培训php,以一个实战新闻cms增删改查demo为例,写的文档讲义供大家参考

PHP实战基础——以一个新闻cms的增删改查为例 一.        环境配置 二.        数据库创建 三.        增删改查demo 连接数据库 <?php $link=mysql_connect("localhost","root","root"); mysql_select_db("demo",$link); mysql_query("set names utf8"); ?>

一个初学者对于jdbc增删改查的一点看法和困惑

我们今周学习了 jdbc的增删改查,总之是各种恍然大悟然后又接着陷入新一轮的苦恼中(相信你们一定深有体会),由于本人的java基础较差,所以是在实践中是各种稀奇古怪的错误频发,接下来我列举几个这几天我犯得让我的老师哭笑不得的错误. 首先是错误:com.microsoft.sqlserver.jdbc.SQLServerException: 已生成用于更新的结果集. 这是我运行我的删除操作之后报的错误,结果是原表无变化,经打断点检查id已经上传,换句话来说程序没有问题,各位小伙伴们能从这只言片语中

php对数据库增删改查操作类

1 <?php 2 3 /** 4 * 函数名称:SqlTool.class.php 5 * 函数功能:php对数据库增删改查操作类 6 * 函数作者:张真贵 7 * 创建时间:2015-01-05 8 * 修改时间: 9 */ 10 header("Content-Type:text/html;charset=utf-8"); 11 class SqlTool{ 12 private $conn; 13 private $host = 'localhost'; 14 priva

富文本内容简单的的增删改查

由于html本身的textarea标签的文本编辑功能较为简单,不能设置文字的样式,因此需要富文本控件来增强textarea的功能.       一些常见的富文本控件有:UEditor.kindeditor.simditor.bootstrap-wysiwyg.wangEditor.CKEditor.tinymce,各有优缺点,网上也有对不介绍,不再赘述. 此处选用tinymce,因其兼容性较好,插入页面也较为简单,此外还有丰富的插件可以扩展功能. 首先,在页面上使用tinymce:1.引入js文

Salesforce零基础(三)简单的数据增删改查页面的构建(Ajax样式)

VisualForce封装了很多的标签用来进行页面设计 下面以一个单一的表进行数据增删改查.表结构如图1所示.通过图可以看出GOODS表自己定义的参数主要包括以下: GoodsName__c,GoodsType__c,GoodsBrand__c,GoodsDescribe__c,GoodsPrice__c. 图1 VF每个页面都是以<apex:page>标签起始</apex:page>结束,每个VF页面都有一个Controller用来控制其业务逻辑.本篇例子中主要用到的控件包括如下

增删改查——QueryRunner类

利用QueryRunner类实现对数据库的增删改查操作,需要先导入jar包:commons-dbutils-1.6.利用QueryRunner类可以实现对数据步骤的简化. 1.添加 运用JDBC工具类实现连接: package JDBCUtils; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import jav

Asp.net简单三层+Sqllite 增删改查

新建项目à新建一个空白解决方案 在Model新建一个实体类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;     namespace factory.Model {     public class factorys     {     //ID INTEGER PRIMARY KEY AUTOINCREMENT