public bool CheckData(Customer t)
{
bool isSuccess = false;
using (PetStoreEntities entities = new PetStoreEntities())
{
var SelectData = from a in entities.Customers
where a.Name.Contains("")
select a;
foreach (var a in SelectData)
{
entities.AddToCustomers(t);
entities.SaveChanges();
}
isSuccess = true;
return isSuccess;
}
}
ผมใช้ vs2008 + sql2008 นะครับ
1.สร้างโปรเจ๊ก เป็น windows app (c#) ชื่อว่า WCFUsingEF
2. คลิกขวาที่ solution --> new project--> visual c# -->web -->Wcf Service application ตั้งชื่อว่า NorthwindWcfService
3.ที่โปรเจ็ก NorthwindWcfService คลิกขวา เลือก add->new item->ADO.NET Entity Data Model ตามรูป
4. กด new connection เพื่อ connection data base
5.เลือก yes และตั้งชื่อ ด้งรูป
6.เลือก เทเบิล และตั้งชื่อ ดังรูป
7. ที่หน้า IService.cs เขียนโค้ด ตามนี้ครับ Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace NorthwindWcfService
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
[ServiceContract]
public interface IService
{
#region "ส่วนที่ เขียนโค้ด เพิ่ม"
[OperationContract]
bool GetCustomerById(string customerid);
[OperationContract]
bool SaveCustomers(Customers cust);
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace NorthwindWcfService
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
public class Service1 : IService
{
#region IService Members
public bool GetCustomerById(string customerid)
{
using (var entity = new NorthwindEntities())
{
List<Customers> cust = (from c in entity.Customers where c.CustomerID == customerid select c).ToList();
if (cust.Count ==0 )
{
return false ;
}
}
return true ;
}
public bool SaveCustomers(Customers cust)
{
using (var entity = new NorthwindEntities())
{
try
{
entity.AddToCustomers(cust);
entity.SaveChanges();
}
catch (Exception ex)
{
string error = ex.ToString();
return false;
}
}
return true;
}
#endregion
}
}