ADO.NET Data Service之CRUD操作

来源:互联网 发布:fotor mac版 编辑:程序博客网 时间:2024/05/22 03:20

1.选择数据

(这是采用GET方法)

            localhost.NorthwindEntities context = new localhost.NorthwindEntities(                new Uri("http://localhost:1000/NorthwindService.svc/"));            //查询            var query = from c in context.Customers                        where c.City.Equals("London")                        select c;                        Console.WriteLine(query.ToString());            foreach (var item in query)            {                Console.WriteLine(item.CompanyName);            }
这个查询,发给服务的查询字串是:
image 
查询数据库的语法是
image 
返回的数据是
HTTP/1.1 200 OKServer: ASP.NET Development Server/9.0.0.0Date: Tue, 09 Feb 2010 12:33:14 GMTX-AspNet-Version: 2.0.50727DataServiceVersion: 1.0;Cache-Control: no-cacheContent-Type: application/atom+xml;charset=utf-8Content-Length: 7604Connection: Closexml version="1.0" encoding="utf-8" standalone="yes"?><feed xml:base="http://localhost:1000/NorthwindService.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">Customerstitle>  <id>http://localhost:1000/NorthwindService.svc/Customersid>  <updated>2010-02-09T12:33:14Zupdated>  <link rel="self" title="Customers" href="Customers" />  <entry>    <id>http://localhost:1000/NorthwindService.svc/Customers('AROUT')id>    <title type="text">title>    <updated>2010-02-09T12:33:14Zupdated>    <author>      <name />    author>    <link rel="edit" title="Customers" href="Customers('AROUT')" />    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Orders" type="application/atom+xml;type=feed" title="Orders" href="Customers('AROUT')/Orders" />    <category term="NorthwindModel.Customers" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />    <content type="application/xml">      <m:properties>        <d:CustomerID>AROUTd:CustomerID>        <d:CompanyName>Around the Hornd:CompanyName>        <d:ContactName>Thomas Hardyd:ContactName>        <d:ContactTitle>Sales Representatived:ContactTitle>        <d:Address>120 Hanover Sq.d:Address>        <d:City>Londond:City>        <d:Region m:null="true" />        <d:PostalCode>WA1 1DPd:PostalCode>        <d:Country>UKd:Country>        <d:Phone>(171) 555-7788d:Phone>        <d:Fax>(171) 555-6750d:Fax>      m:properties>    content>  entry>  <entry>    <id>http://localhost:1000/NorthwindService.svc/Customers('BSBEV')id>    <title type="text">title>    <updated>2010-02-09T12:33:14Zupdated>    <author>      <name />    author>    <link rel="edit" title="Customers" href="Customers('BSBEV')" />    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Orders" type="application/atom+xml;type=feed" title="Orders" href="Customers('BSBEV')/Orders" />    <category term="NorthwindModel.Customers" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />    <content type="application/xml">      <m:properties>        <d:CustomerID>BSBEVd:CustomerID>        <d:CompanyName>B's Beveragesd:CompanyName>        <d:ContactName>Victoria Ashworthd:ContactName>        <d:ContactTitle>Sales Representatived:ContactTitle>        <d:Address>Fauntleroy Circusd:Address>        <d:City>Londond:City>        <d:Region m:null="true" />        <d:PostalCode>EC2 5NTd:PostalCode>        <d:Country>UKd:Country>        <d:Phone>(171) 555-1212d:Phone>        <d:Fax m:null="true" />      m:properties>    content>  entry>  <entry>    <id>http://localhost:1000/NorthwindService.svc/Customers('CONSH')id>    <title type="text">title>    <updated>2010-02-09T12:33:14Zupdated>    <author>      <name />    author>    <link rel="edit" title="Customers" href="Customers('CONSH')" />    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Orders" type="application/atom+xml;type=feed" title="Orders" href="Customers('CONSH')/Orders" />    <category term="NorthwindModel.Customers" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />    <content type="application/xml">      <m:properties>        <d:CustomerID>CONSHd:CustomerID>        <d:CompanyName>Consolidated Holdingsd:CompanyName>        <d:ContactName>Elizabeth Brownd:ContactName>        <d:ContactTitle>Sales Representatived:ContactTitle>        <d:Address>Berkeley Gardens 12  Breweryd:Address>        <d:City>Londond:City>        <d:Region m:null="true" />        <d:PostalCode>WX1 6LTd:PostalCode>        <d:Country>UKd:Country>        <d:Phone>(171) 555-2282d:Phone>        <d:Fax>(171) 555-9199d:Fax>      m:properties>    content>  entry>  <entry>    <id>http://localhost:1000/NorthwindService.svc/Customers('EASTC')id>    <title type="text">title>    <updated>2010-02-09T12:33:14Zupdated>    <author>      <name />    author>    <link rel="edit" title="Customers" href="Customers('EASTC')" />    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Orders" type="application/atom+xml;type=feed" title="Orders" href="Customers('EASTC')/Orders" />    <category term="NorthwindModel.Customers" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />    <content type="application/xml">      <m:properties>        <d:CustomerID>EASTCd:CustomerID>        <d:CompanyName>Eastern Connectiond:CompanyName>        <d:ContactName>Ann Devond:ContactName>        <d:ContactTitle>Sales Agentd:ContactTitle>        <d:Address>35 King Georged:Address>        <d:City>Londond:City>        <d:Region m:null="true" />        <d:PostalCode>WX3 6FWd:PostalCode>        <d:Country>UKd:Country>        <d:Phone>(171) 555-0297d:Phone>        <d:Fax>(171) 555-3373d:Fax>      m:properties>    content>  entry>  <entry>    <id>http://localhost:1000/NorthwindService.svc/Customers('NORTS')id>    <title type="text">title>    <updated>2010-02-09T12:33:14Zupdated>    <author>      <name />    author>    <link rel="edit" title="Customers" href="Customers('NORTS')" />    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Orders" type="application/atom+xml;type=feed" title="Orders" href="Customers('NORTS')/Orders" />    <category term="NorthwindModel.Customers" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />    <content type="application/xml">      <m:properties>        <d:CustomerID>NORTSd:CustomerID>        <d:CompanyName>North/Southd:CompanyName>        <d:ContactName>Simon Crowtherd:ContactName>        <d:ContactTitle>Sales Associated:ContactTitle>        <d:Address>South House 300 Queensbridged:Address>        <d:City>Londond:City>        <d:Region m:null="true" />        <d:PostalCode>SW7 1RZd:PostalCode>        <d:Country>UKd:Country>        <d:Phone>(171) 555-7733d:Phone>        <d:Fax>(171) 555-2530d:Fax>      m:properties>    content>  entry>  <entry>    <id>http://localhost:1000/NorthwindService.svc/Customers('SEVES')id>    <title type="text">title>    <updated>2010-02-09T12:33:14Zupdated>    <author>      <name />    author>    <link rel="edit" title="Customers" href="Customers('SEVES')" />    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Orders" type="application/atom+xml;type=feed" title="Orders" href="Customers('SEVES')/Orders" />    <category term="NorthwindModel.Customers" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />    <content type="application/xml">      <m:properties>        <d:CustomerID>SEVESd:CustomerID>        <d:CompanyName>Seven Seas Importsd:CompanyName>        <d:ContactName>Hari Kumard:ContactName>        <d:ContactTitle>Sales Managerd:ContactTitle>        <d:Address>90 Wadhurst Rd.d:Address>        <d:City>Londond:City>        <d:Region m:null="true" />        <d:PostalCode>OX15 4NBd:PostalCode>        <d:Country>UKd:Country>        <d:Phone>(171) 555-1717d:Phone>        <d:Fax>(171) 555-5646d:Fax>      m:properties>    content>  entry>feed>
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

2.插入数据

(这个操作是POST)

            //增加新客户            localhost.Customers newCustomer = new localhost.Customers()            {                CustomerID = "FFFFF",                CompanyName = "Thinker Inc"            };            context.AddToCustomers(newCustomer);
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

发送给服务器的数据是

POST /NorthwindService.svc/Customers HTTP/1.1User-Agent: Microsoft ADO.NET Data ServicesAccept: application/atom+xml,application/xmlAccept-Charset: UTF-8DataServiceVersion: 1.0;NetFxMaxDataServiceVersion: 1.0;NetFxContent-Type: application/atom+xmlHost: localhost:1000Content-Length: 978Expect: 100-continuexml version="1.0" encoding="utf-8" standalone="yes"?><entry 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">  <category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="NorthwindModel.Customers" />  <title />  <updated>2010-02-09T12:34:10.5029331Zupdated>  <author>    <name />  author>  <id />  <content type="application/xml">    <m:properties>      <d:Address m:null="true" />      <d:City m:null="true" />      <d:CompanyName>Thinker Incd:CompanyName>      <d:ContactName m:null="true" />      <d:ContactTitle m:null="true" />      <d:Country m:null="true" />      <d:CustomerID>FFFFFd:CustomerID>      <d:Fax m:null="true" />      <d:Phone m:null="true" />      <d:PostalCode m:null="true" />      <d:Region m:null="true" />    m:properties>  content>entry>
返回的数据是
HTTP/1.1 100 ContinueServer: ASP.NET Development Server/9.0.0.0Date: Tue, 09 Feb 2010 12:34:10 GMTContent-Length: 0HTTP/1.1 201 CreatedServer: ASP.NET Development Server/9.0.0.0Date: Tue, 09 Feb 2010 12:34:10 GMTX-AspNet-Version: 2.0.50727DataServiceVersion: 1.0;Location: http://localhost:1000/NorthwindService.svc/Customers('FFFFF')Cache-Control: no-cacheContent-Type: application/atom+xml;charset=utf-8Content-Length: 1347Connection: Closexml version="1.0" encoding="utf-8" standalone="yes"?><entry xml:base="http://localhost:1000/NorthwindService.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">  <id>http://localhost:1000/NorthwindService.svc/Customers('FFFFF')id>  <title type="text">title>  <updated>2010-02-09T12:34:10Zupdated>  <author>    <name />  author>  <link rel="edit" title="Customers" href="Customers('FFFFF')" />  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Orders" type="application/atom+xml;type=feed" title="Orders" href="Customers('FFFFF')/Orders" />  <category term="NorthwindModel.Customers" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />  <content type="application/xml">    <m:properties>      <d:CustomerID>FFFFFd:CustomerID>      <d:CompanyName>Thinker Incd:CompanyName>      <d:ContactName m:null="true" />      <d:ContactTitle m:null="true" />      <d:Address m:null="true" />      <d:City m:null="true" />      <d:Region m:null="true" />      <d:PostalCode m:null="true" />      <d:Country m:null="true" />      <d:Phone m:null="true" />      <d:Fax m:null="true" />    m:properties>  content>entry>
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
 
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

3.更新数据

(这个操作是用MERGE)

            var c = context.Customers.Where(w => w.CustomerID == "FFFFF");            var customer = c.FirstOrDefault();            customer.CompanyName = "Microsoft";            context.UpdateObject(customer);

.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

 

发送给服务的数据是

MERGE /NorthwindService.svc/Customers('FFFFF') HTTP/1.1User-Agent: Microsoft ADO.NET Data ServicesAccept: application/atom+xml,application/xmlAccept-Charset: UTF-8DataServiceVersion: 1.0;NetFxMaxDataServiceVersion: 1.0;NetFxContent-Type: application/atom+xmlHost: localhost:1000Content-Length: 1040Expect: 100-continuexml version="1.0" encoding="utf-8" standalone="yes"?><entry 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">  <category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="NorthwindModel.Customers" />  <title />  <updated>2010-02-09T12:48:12.3845677Zupdated>  <author>    <name />  author>  <id>http://localhost:1000/NorthwindService.svc/Customers('FFFFF')id>  <content type="application/xml">    <m:properties>      <d:Address m:null="true" />      <d:City m:null="true" />      <d:CompanyName>Microsoftd:CompanyName>      <d:ContactName m:null="true" />      <d:ContactTitle m:null="true" />      <d:Country m:null="true" />      <d:CustomerID>FFFFFd:CustomerID>      <d:Fax m:null="true" />      <d:Phone m:null="true" />      <d:PostalCode m:null="true" />      <d:Region m:null="true" />    m:properties>  content>entry>

.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

发送给数据库的指令是

exec sp_executesql N'update [dbo].[Customers]set [CompanyName] = @0, [ContactName] = null, [ContactTitle] = null, [Address] = null, [City] = null, [Region] = null, [PostalCode] = null, [Country] = null, [Phone] = null, [Fax] = nullwhere ([CustomerID] = @1)',N'@0 nvarchar(9),@1 nchar(5)',@0=N'Microsoft',@1=N'FFFFF'
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

 

回复给客户端的数据是

HTTP/1.1 204 No ContentServer: ASP.NET Development Server/9.0.0.0Date: Tue, 09 Feb 2010 12:48:12 GMTX-AspNet-Version: 2.0.50727DataServiceVersion: 1.0;Cache-Control: no-cacheContent-Length: 0Connection: Close
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

4.删除数据

(这个操作是DELETE)

            var c = context.Customers.Where(w => w.CustomerID == "FFFFF");            var customer = c.FirstOrDefault();            customer.CompanyName = "Microsoft";            context.DeleteObject(customer);
发送给服务的数据是
DELETE /NorthwindService.svc/Customers('FFFFF') HTTP/1.1User-Agent: Microsoft ADO.NET Data ServicesAccept: application/atom+xml,application/xmlAccept-Charset: UTF-8DataServiceVersion: 1.0;NetFxMaxDataServiceVersion: 1.0;NetFxContent-Type: application/atom+xmlHost: localhost:1000Content-Length: 0
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

发送给数据库服务器的指令是

exec sp_executesql N'delete [dbo].[Customers]where ([CustomerID] = @0)',N'@0 nchar(5)',@0=N'FFFFF'

.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

得到的回复是

HTTP/1.1 204 No Content
Server: ASP.NET Development Server/9.0.0.0
Date: Tue, 09 Feb 2010 13:01:04 GMT
X-AspNet-Version: 2.0.50727
DataServiceVersion: 1.0;
Cache-Control: no-cache
Content-Length: 0
Connection: Close

原创粉丝点击