1.ADO.NET Data Service的DataServiceHost是一个WCF Service,由System.Data.Services.DataService<T>提供来自NET3.5 SP1 类库中的System.Data.Services.dll
2.DataServices会自动为加载的Entity对象生成具有增查删改功能的WCF服务
3.DataServices是以Http方式与客户端进行通信.任何可以实现HTTP访问的客户端,都可以与DataServices交互
4. NET3.5 SP1的客户端可以使用System.Data.Services.Client.DataServiceContext与DataServices交互,该类来自NET3.5 SP1 类库中的System.Data.Services.Client.dll 文件
5. Silverlight 2.0客户端可以使用System.Data.Services.Client.DataServiceContext与DataServices交互,该类来自Silverlight 2.0 类库中的System.Data.Services.Client 文件
6.DataServices提供ATOM与JSON两种格式用来描述数据结构,说明如下:
ATOM
与RSS类似,也是建立在XML数据格式的基础之上的
Atom相对RSS格式而言,进行了扩展,提供了比RSS更多的项目属性
JSON
格式如下
{
"name": "wxd",
"time": "2008/10/10 10:10",
"value": 123.456,
"list": [ "wxwinter", "lzm"]
}
tabA
tabX
public class myWebDataService : DataService<myDBEntities>
{
public static void InitializeService(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
// * :表示全部实体集
// EntitySetRights.All : 表示全部的操作权限
}
}
//查询
private void button1_Click(object sender, EventArgs e)
{
Uri url = new Uri("http://localhost:1468/myWebDataService.svc");
myService.myDBEntities server = new WinFormClient.myService.myDBEntities(url);
foreach (var v in server.tabA)
{
Console.WriteLine("{0},{1},{2}", v.a, v.b, v.c);
}
//-
foreach (var v in server.tabX.Where(p=>p.x=="002"))
{
Console.WriteLine("{0},{1},{2}", v.x, v.y, v.z);
}
}
lzm ,2 ,5
wxd ,1 ,4
wxwinter ,3 ,6
002 ,8 ,llzzmm
//添加
private void button2_Click(object sender, EventArgs e)
{
Uri url = new Uri("http://localhost:1468/myWebDataService.svc");
myService.myDBEntities server = new WinFormClient.myService.myDBEntities(url);
server.AddTotabA(new myService.tabA() { a = "wxdlzm", b = "333", c = "xxx" });
server.SaveChanges();
}
//修改
private void button3_Click(object sender, EventArgs e)
{
Uri url = new Uri("http://localhost:1468/myWebDataService.svc");
myService.myDBEntities server = new WinFormClient.myService.myDBEntities(url);
var v = server.tabA.Where(p => p.a == "wxdlzm").First();
v.b = "hello";
server.UpdateObject(v);
server.SaveChanges();
}
//删除
private void button4_Click(object sender, EventArgs e)
{
Uri url = new Uri("http://localhost:1468/myWebDataService.svc");
myService.myDBEntities server = new WinFormClient.myService.myDBEntities(url);
var v = server.tabA.Where(p => p.a == "wxdlzm").First();
server.DeleteObject(v);
server.SaveChanges();
}
//异步查询
myService.myDBEntities server1;
//开始查询
private void button5_Click(object sender, EventArgs e)
{
Uri url = new Uri("http://localhost:1468/myWebDataService.svc");
Uri urlE = new Uri("http://localhost:1468/myWebDataService.svc/tabX");
server1 = new WinFormClient.myService.myDBEntities(url);
server1.BeginExecute<myService.tabX>(urlE, new AsyncCallback(executed), null);
}
//返回结果
void executed(IAsyncResult obj)
{
var tp = server1.EndExecute<myService.tabX>(obj);
foreach (var v in tp)
{
Console.WriteLine("{0},{1},{2}", v.x, v.y, v.z);
}
}
Silverlight 2.0 可以利用两种方法与DataServices交互:
Silverlight 2.0 是使用异步方法来连接远程数据源。
LINQ 的 select ,要用 BeginExecute() 和 EndExecute()
LINQ 的 SaveChanges(),要用 BeginSaveChanges() 和 EndSaveChanges()
<StackPanel Loaded="StackPanel_Loaded">
<data:DataGrid Name="dg"></data:DataGrid>
<Button Content="DataServiceContext方式加载数据" Width="200" Click="loadData_Click" />
<Button Content="WebClient方式加载数据" Width="200" Click="loadData2_Click" />
<Button Content="添加" Width="200" Click="add_Click" />
<Button Content="修改" Width="200" Click="edit_Click" />
<Button Content="删除" Width="200" Click="del_Click" />
</StackPanel>
myService.myDBEntities server;
private void StackPanel_Loaded(object sender, RoutedEventArgs e)
{
Uri url = new Uri("http://localhost:1468/myWebDataService.svc");
server = new myService.myDBEntities(url);
}
void saveChanges_completed(IAsyncResult obj)
{
}
//WebClient方式加载数据
private void loadData2_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("http://localhost:1468/myWebDataService.svc/tabA");
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_completed);
client.OpenReadAsync(uri);
}
void client_completed(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
XmlReader reader = XmlReader.Create(e.Result);
XDocument ls = XDocument.Load(reader);
XNamespace xmlns = "http://www.w3.org/2005/Atom";
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
var list = from x in ls.Descendants(xmlns + "entry")
select new myService.tabA
{
a= x.Descendants(d + "a").First().Value, b = x.Descendants(d + "b").First().Value, c = x.Descendants(d + "c").First().Value
};
dg.ItemsSource = list;
}
}
//加载
private void loadData_Click(object sender, RoutedEventArgs e)
{
Uri urlE = new Uri("http://localhost:1468/myWebDataService.svc/tabX");
server.BeginExecute<myService.tabX>(urlE, new AsyncCallback(load_completed), null);
}
void load_completed(IAsyncResult obj)
{
var tp = server.EndExecute<myService.tabX>(obj);
this.dg.ItemsSource = tp.ToList();
}
//添加
private void add_Click(object sender, RoutedEventArgs e)
{
server.AddTotabX(new myService.tabX() { x="007",z="sss",y="sss" });
server.BeginSaveChanges(new AsyncCallback(saveChanges_completed), null);
}
//编辑
private void edit_Click(object sender, RoutedEventArgs e)
{
Uri urlE = new Uri("http://localhost:1468/myWebDataService.svc/tabX");
server.BeginExecute<myService.tabX>(urlE, new AsyncCallback(begin_edit), null);
}
void begin_edit(IAsyncResult obj)
{
var tp = server.EndExecute<myService.tabX>(obj).Where(p => p.x == "007 ").First();
tp.y = "hello";
server.UpdateObject(tp);
server.BeginSaveChanges(new AsyncCallback(saveChanges_completed), null);
}
//删除
private void del_Click(object sender, RoutedEventArgs e)
{
Uri urlE = new Uri("http://localhost:1468/myWebDataService.svc/tabX");
server.BeginExecute<myService.tabX>(urlE, new AsyncCallback(begin_del), null);
}
void begin_del(IAsyncResult obj)
{
var tp = server.EndExecute<myService.tabX>(obj).Where(p => p.x == "007 ").First();
server.DeleteObject(tp);
server.BeginSaveChanges(new AsyncCallback(saveChanges_completed), null);
}
DataService 支持URL方式访问
格式
http://[Url]/[ServiceName]/[EntityName]/[NavigationOptions]?[QueryOptions]
Url: 数据服务所在的网址
ServiceName: 数据服务的名称
EntityName: Entity 名称或是 Service 方法名称
NavigationOptions: 查询 Entity 中关联的设定
QueryOptions: 查询的选项
运算符
说明
and
or
not
eq
=
ne
!=
lt
<
gt
>
le
<=
ge
>=
add
+
sub
-
mul
*
div
/
mod
余数
()
expand
类似于LINQ中的LoadOptions,以此来指定加载此对象相关的通过expand指定的对象。如果需要加载多个对象,用逗号分隔。
orderby
指定排序方式。语法为:$orderby=Field [ASC|DESC], [Field[ASC|DESC]]
Skip
Top
类似于LINQ中的Skip(PageSize * PageIndex).Take(PageSize)用来实现分页。
Filter
通过filter这个参数可以在URL里传递过滤条件。
bool substringof(string p0, string p1)
bool startswith(string p0, string p1)
int indexof(string arg)
string remove(string p0, int pos)
string remove(string p0, string find, string replace)
string substring(string p0, int pos, int length)
string toupper(string p0)
string concat(string p0, string p1)
bool endswith(string p0, string p1)
int length(string p0)
string insert(string p0,int pos, string p1)
string remove(string p0, int pos, int length)
string substring(string p0, int pos)
string tolower(string p0)
string trim(string p0)
int day(DateTime)
int month(DateTime)
int hour(DateTime)
int second(DateTime)
int minute(DateTime)
int year(DateTime)
double round(double)
decimal floor(decimal)
decimal round(decimal)
double ceiling(double)
double floor(double)
decimal ceiling(decimal)
http://localhost:1468/myWebDataService.svc/tabA
由于返回的数据是ATOM格式的,如果浏览器支持该格式,会用阅读方式打开,如IE7用RSS阅读器方式打开
以下是返回的数据
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://localhost:1468/myWebDataService.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">tabA</title>
<id>http://localhost:1468/myWebDataService.svc/tabA</id>
<updated>2008-11-17T11:14:32Z</updated>
<link rel="self" title="tabA" href="tabA" />
<entry>
<id>http://localhost:1468/myWebDataService.svc/tabA('lzm%20%20%20%20%20%20%20')</id>
<title type="text"></title>
<updated>2008-11-17T11:14:32Z</updated>
<author>
<name />
</author>
<link rel="edit" title="tabA" href="tabA('lzm%20%20%20%20%20%20%20')" />
<category term="myDBModel.tabA" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:a xml:space="preserve">lzm </d:a>
<d:b xml:space="preserve">2 </d:b>
<d:c xml:space="preserve">5 </d:c>
</m:properties>
</content>
</entry>
<entry>
<id>http://localhost:1468/myWebDataService.svc/tabA('wxd%20%20%20%20%20%20%20')</id>
<title type="text"></title>
<updated>2008-11-17T11:14:32Z</updated>
<author>
<name />
</author>
<link rel="edit" title="tabA" href="tabA('wxd%20%20%20%20%20%20%20')" />
<category term="myDBModel.tabA" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:a xml:space="preserve">wxd </d:a>
<d:b xml:space="preserve">1 </d:b>
<d:c xml:space="preserve">4 </d:c>
</m:properties>
</content>
</entry>
<entry>
<id>http://localhost:1468/myWebDataService.svc/tabA('wxdlzm%20%20%20%20')</id>
<title type="text"></title>
<updated>2008-11-17T11:14:32Z</updated>
<author>
<name />
</author>
<link rel="edit" title="tabA" href="tabA('wxdlzm%20%20%20%20')" />
<category term="myDBModel.tabA" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:a xml:space="preserve">wxdlzm </d:a>
<d:b xml:space="preserve">333 </d:b>
<d:c xml:space="preserve">xxx </d:c>
</m:properties>
</content>
</entry>
<entry>
<id>http://localhost:1468/myWebDataService.svc/tabA('wxwinter%20%20')</id>
<title type="text"></title>
<updated>2008-11-17T11:14:32Z</updated>
<author>
<name />
</author>
<link rel="edit" title="tabA" href="tabA('wxwinter%20%20')" />
<category term="myDBModel.tabA" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:a xml:space="preserve">wxwinter </d:a>
<d:b xml:space="preserve">3 </d:b>
<d:c xml:space="preserve">6 </d:c>
</m:properties>
</content>
</entry>
</feed>
http://localhost:1468/myWebDataService.svc/tabA?$filter=a eq 'wxd'
http://localhost:1468/myWebDataService.svc/tabA?$filter=(a eq 'wxwinter') and (b eq '3')
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://localhost:1468/myWebDataService.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">tabA</title>
<id>http://localhost:1468/myWebDataService.svc/tabA</id>
<updated>2008-11-17T11:26:29Z</updated>
<link rel="self" title="tabA" href="tabA" />
<entry>
<id>http://localhost:1468/myWebDataService.svc/tabA('wxd%20%20%20%20%20%20%20')</id>
<title type="text"></title>
<updated>2008-11-17T11:26:29Z</updated>
<author>
<name />
</author>
<link rel="edit" title="tabA" href="tabA('wxd%20%20%20%20%20%20%20')" />
<category term="myDBModel.tabA" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:a xml:space="preserve">wxd </d:a>
<d:b xml:space="preserve">1 </d:b>
<d:c xml:space="preserve">4 </d:c>
</m:properties>
</content>
</entry>
</feed>
http://localhost:1468/myWebDataService.svc/tabA?$filter=a ne 'wxd' &$orderby=b
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://localhost:1468/myWebDataService.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">tabA</title>
<id>http://localhost:1468/myWebDataService.svc/tabA</id>
<updated>2008-11-17T11:28:10Z</updated>
<link rel="self" title="tabA" href="tabA" />
<entry>
<id>http://localhost:1468/myWebDataService.svc/tabA('lzm%20%20%20%20%20%20%20')</id>
<title type="text"></title>
<updated>2008-11-17T11:28:10Z</updated>
<author>
<name />
</author>
<link rel="edit" title="tabA" href="tabA('lzm%20%20%20%20%20%20%20')" />
<category term="myDBModel.tabA" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:a xml:space="preserve">lzm </d:a>
<d:b xml:space="preserve">2 </d:b>
<d:c xml:space="preserve">5 </d:c>
</m:properties>
</content>
</entry>
<entry>
<id>http://localhost:1468/myWebDataService.svc/tabA('wxwinter%20%20')</id>
<title type="text"></title>
<updated>2008-11-17T11:28:10Z</updated>
<author>
<name />
</author>
<link rel="edit" title="tabA" href="tabA('wxwinter%20%20')" />
<category term="myDBModel.tabA" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:a xml:space="preserve">wxwinter </d:a>
<d:b xml:space="preserve">3 </d:b>
<d:c xml:space="preserve">6 </d:c>
</m:properties>
</content>
</entry>
<entry>
<id>http://localhost:1468/myWebDataService.svc/tabA('wxdlzm%20%20%20%20')</id>
<title type="text"></title>
<updated>2008-11-17T11:28:10Z</updated>
<author>
<name />
</author>
<link rel="edit" title="tabA" href="tabA('wxdlzm%20%20%20%20')" />
<category term="myDBModel.tabA" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:a xml:space="preserve">wxdlzm </d:a>
<d:b xml:space="preserve">333 </d:b>
<d:c xml:space="preserve">xxx </d:c>
</m:properties>
</content>
</entry>
</feed>
http://localhost:1468/myWebDataService.svc/tabA?$filter=a ne 'wxd' &$orderby=b &$skip=2&top=2
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://localhost:1468/myWebDataService.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">tabA</title>
<id>http://localhost:1468/myWebDataService.svc/tabA</id>
<updated>2008-11-17T11:30:20Z</updated>
<link rel="self" title="tabA" href="tabA" />
<entry>
<id>http://localhost:1468/myWebDataService.svc/tabA('wxdlzm%20%20%20%20')</id>
<title type="text"></title>
<updated>2008-11-17T11:30:20Z</updated>
<author>
<name />
</author>
<link rel="edit" title="tabA" href="tabA('wxdlzm%20%20%20%20')" />
<category term="myDBModel.tabA" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:a xml:space="preserve">wxdlzm </d:a>
<d:b xml:space="preserve">333 </d:b>
<d:c xml:space="preserve">xxx </d:c>
</m:properties>
</content>
</entry>
</feed>
http://localhost:1468/myWebDataService.svc/tabA?$filter=a eq trim(' wxd ')
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://localhost:1468/myWebDataService.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">tabA</title>
<id>http://localhost:1468/myWebDataService.svc/tabA</id>
<updated>2008-11-17T12:01:49Z</updated>
<link rel="self" title="tabA" href="tabA" />
<entry>
<id>http://localhost:1468/myWebDataService.svc/tabA('wxd%20%20%20%20%20%20%20')</id>
<title type="text"></title>
<updated>2008-11-17T12:01:49Z</updated>
<author>
<name />
</author>
<link rel="edit" title="tabA" href="tabA('wxd%20%20%20%20%20%20%20')" />
<category term="myDBModel.tabA" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:a xml:space="preserve">wxd </d:a>
<d:b xml:space="preserve">1 </d:b>
<d:c xml:space="preserve">4 </d:c>
</m:properties>
</content>
</entry>
</feed>
[WebGet] 使用 GET方式访问
[WebInvoke] 使用 POST/PUT/DELETE 方式访问
public class myWebDataService : DataService<myDBEntities>
{
public static void InitializeService(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
// * :表示全部实体集
// EntitySetRights.All : 表示全部的操作权限
// config.SetServiceOperationAccessRule("getTabA", ServiceOperationRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
// * :表示全部实体集
// ServiceOperationRights.All : 表示全部的操作权限
}
[WebGet]
public IQueryable<tabA> getTabA(string a)
{
var v= CurrentDataSource.tabA.Where(p => p.a == a);
return v;
}
[WebInvoke]
public IQueryable<tabX> getTabX(string x)
{
var v = CurrentDataSource.tabX.Where(p => p.x == x);
return v;
}
}
方法名?参数='值
'
http://localhost:1468/myWebDataService.svc/getTabA?a='lzm'
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://localhost:1468/myWebDataService.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">getTabA</title>
<id>http://localhost:1468/myWebDataService.svc/getTabA</id>
<updated>2008-11-17T02:23:29Z</updated>
<link rel="self" title="getTabA" href="getTabA" />
<entry>
<id>http://localhost:1468/myWebDataService.svc/tabA('lzm%20%20%20%20%20%20%20')</id>
<title type="text"></title>
<updated>2008-11-17T02:23:29Z</updated>
<author>
<name />
</author>
<link rel="edit" title="tabA" href="tabA('lzm%20%20%20%20%20%20%20')" />
<category term="myDBModel.tabA" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:a xml:space="preserve">lzm </d:a>
<d:b xml:space="preserve">2 </d:b>
<d:c xml:space="preserve">5 </d:c>
</m:properties>
</content>
</entry>
</feed>
查询拦截:当你仅仅想把具有某种状态或者特征的数据返回给客户端时,用拦截查询就可以实现
修改拦截:可以拦截提交到服务器的的数据更新操作:Add, Change ,Delete
public class myWebDataService : DataService<myDBEntities>
{
public static void InitializeService(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
// * :表示全部实体集
// EntitySetRights.All : 表示全部的操作权限
}
[QueryInterceptor("tabX")]
public System.Linq.Expressions.Expression<Func<tabX, bool>> query_tabX()
{
return p => p.z != "wwxxdd"; //为真表示允许查询
}
}
不使用QueryInterceptor的结果
使用QueryInterceptor的结果
public class myWebDataService : DataService<myDBEntities>
{
public static void InitializeService(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
// * :表示全部实体集
// EntitySetRights.All : 表示全部的操作权限
}
[ChangeInterceptor("tabX")]
public void change_tabX(tabX en, UpdateOperations operation)
{
//-
if (operation == UpdateOperations.Add)
{
if (string.IsNullOrEmpty(en.y))
{
//如果[y]为空的处理代码
}
}
//-
if (operation == UpdateOperations.Change)
{ }
//-
if (operation == UpdateOperations.Delete)
{ }
}
}