今天學了一下.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; } } }