실버라이트 초간단 데이터 바인딩
1.ADO.NET Entity Data Model 을 만듭니다 (서버측)
2. 그런다음 데이터베이스에 접근할수 있는 ado.net Data Service 를 만들어줍니다. (서버측)
3.실버라이트 프로젝트에서 웹서비스를 참조합니다.
4.데이터그리드에 데이터를 바인딩 합니다.
5.테스트 끝~
1.ADO.NET Entity Data Model 을 만듭니다
그런다음 데이터베이스테 테이블을 추가시키면 자동으로 만들어 줍니다
BOOK , BookOrderHistory 는 테이블 명입니다
만들어진 ADO.NET Entity Data Model 에 접근한기위한 ado.net Data Service 를 만들어줍니다
using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
namespace BM.Web
{
public class BookWebDataService : DataService< CITY_GAS_BOOKEntities>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(IDataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.SetEntitySetAccessRule("*", EntitySetRights.All);
//config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
//config.UseVerboseErrors = true;
}
}
}
실버라이트 프로젝트에서 사용하기 위해서 서비스를 참조 시킵니다
BOOK 을 담는 ObservableCollection 클래스를 구현합니다
using System.ComponentModel;
using System.Collections.ObjectModel;
using BM.ServiceReference1;
namespace BM.CodeTest
{
public class BooksDS : ObservableCollection<BOOK>
{
public BooksDS() : base()
{
}
}
}
실버라이트에서 데이터 그리드 (grdList 로 이름을 주세요 ) 를 하나 만들고
CLR Object Data Source 를추가 시켜줍니다. 정상적으로 추가가 되면
BooksDS 찾아서 바인딩 시켜주세요
데이터 그리드에 자동으로 컬럼이 표시됩니다 (자동컬럼으로 설정되었을 경우 )
실제 코드 상의 셀렉트
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Services.Client;
using System.Collections.ObjectModel;
using System.ComponentModel;
using BM.ServiceReference1;
namespace BM.CodeTest
{
public partial class Delete : UserControl
{
ServiceReference1.CITY_GAS_BOOKEntities svcContext;
ObservableCollection<ServiceReference1.BOOK> ObsvBookCollrection;
BooksDS bookDs;
public Delete()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Delete_Loaded);
}
void Delete_Loaded(object sender, RoutedEventArgs e)
{
svcContext = new CITY_GAS_BOOKEntities(new Uri("BookWebDataService.svc", UriKind.Relative));
ObsvBookCollrection = new ObservableCollection<BOOK>();
bookDs = new BooksDS();
DataServiceQuery<BOOK> query = (DataServiceQuery<BOOK>)
(from c in svcContext.BOOK select c);
query.BeginExecute(GetDataCallback, query);
}
#region 셀렉트
void GetDataCallback(IAsyncResult result)
{
try
{
DataServiceQuery<BOOK> queryResult =
(DataServiceQuery<BOOK>)result.AsyncState;
IEnumerable<BOOK> results =
queryResult.EndExecute(result);
foreach (var item in results)
{
bookDs.Add(item);
}
grdList.ItemsSource = bookDs;
MessageBox.Show("도서목록가져오기 완료");
}
catch (DataServiceRequestException ex)
{
}
}
#endregion
}
}