今天学了一下.net的WCF组件,边心血来潮,想着现在不都是前后分离,调接口开发不,于是赶紧写了一简单的后台数据,哈哈 废话不多说,直接上代码;
注意需要导入库!
实体类:Customer
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace domain{ ???public class Customer ???{ ???????public string CustomerId { set; get; } ???????public string CompanyName { set; get; } ???????public string ContactName { set; get; } ???????public string Address { set; get; } ???????public string test1 { set; get; } ???};}
WCF接口
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.ServiceModel.Web;using System.Text;namespace 接口测试学习1{ ???// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。 ???[ServiceContract] ???public interface IService1 ???{ ???????[OperationContract] ???????string GetData(int value); ???????[OperationContract] ???????CompositeType GetDataUsingDataContract(CompositeType composite); ???????// TODO: 在此添加您的服务操作 ???????[OperationContract] ???????string GetDataJson(string customer); ???} ???// 使用下面示例中说明的数据约定将复合类型添加到服务操作。 ???[DataContract] ???public class CompositeType ???{ ???????bool boolValue = true; ???????string stringValue = "Hello "; ?????[DataMember] ???????public bool BoolValue ???????{ ???????????get { return boolValue; } ???????????set { boolValue = value; } ???????} ?????????[DataMember] ???????public string StringValue ???????{ ???????????get { return stringValue; } ???????????set { stringValue = value; } ???????} ?????????}}
WCF接口的实现类
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.SqlClient;using domain;using Newtonsoft.Json;namespace 接口测试学习1{ ???// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。 ???// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs,然后开始调试。 ???public class Service1 : IService1 ???{ ??????????????public string GetData(int value) ???????{ ???????????return string.Format("You entered: {0}", value); ???????} ???????public CompositeType GetDataUsingDataContract(CompositeType composite) ???????{ ???????????if (composite == null) ???????????{ ???????????????throw new ArgumentNullException("composite"); ???????????} ???????????if (composite.BoolValue) ???????????{ ???????????????composite.StringValue += "Suffix"; ???????????} ???????????return composite; ???????} ???????public string GetDataJson(string customer) ???????{ ???????????string strcon = "Data Source=192.168.99.28;Initial Catalog=EDU;User Id=Pos;Password=Pos;"; ???????????SqlConnection con = new SqlConnection(strcon); ???????????List<Customer> list = new List<Customer>(); ???????????try ???????????{ ??????????????????????????????con.Open(); ???????????????//将执行的sql ????????????// ??string sql = "select * from Customer"; ???????????????string sql = string.Format("select * from {0}", customer); ???????????????//创建命令对象,指定要执行sql语句与连接对象conn ???????????????SqlCommand cmd = new SqlCommand(sql, con); ???????????????Console.WriteLine("打开成功,状态" + con.State); ???????????????//执行查询返回结果集 ???????????????SqlDataReader sdr = cmd.ExecuteReader(); ???????????????while (sdr.Read()) ???????????????{ ???????????????????Customer c = new Customer(); ???????????????????c.CustomerId = sdr["CustomerId"].ToString(); ???????????????????c.CompanyName = sdr["CompanyName"].ToString(); ???????????????????c.ContactName = sdr["ContactName"].ToString(); ???????????????????c.Address = sdr["Address"].ToString(); ???????????????????c.test1 = sdr["test1"].ToString(); ???????????????????list.Add(c); ???????????????} ???????????} catch (Exception ex) ???????????{ ???????????????Console.WriteLine(ex.Message); ???????????} ???????????finally { ???????????//关闭连接 ???????????con.Close(); ??????????// Console.WriteLine("总共查询了" + count +"条数据"); ????????// ??Console.ReadKey(); ???????????Console.WriteLine(list.Capacity); ???????????} ???????????//再把list集合进行序列化,转json ???????????string json = JsonConvert.SerializeObject(list); ???????????Console.WriteLine(json); ???????????Console.ReadKey(); ???????????return json; ??????????????????} ???????????????????????} ???????????}
利用jsonconvert来转换json数据格式 ???(对象转为json)
原文地址:https://www.cnblogs.com/cb1186512739/p/9504887.html