<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
// This sample requires the .NET Framework 3.5 SP1.
// To get this the sample to work, perform the
// following steps:
//
// 1. Create a new Web Site in Visual Studio 2008 SP1
// 2. Add a new Web Form (name it what you like)
// 3. Make sure the "Place code in separate file" is unchecked in the
// Add New Item dialog
// 4. Replace the entire contents of the .aspx file with the entire
// text of this sample
// 5. Right click on the project node in Solution Explorer
// 6. Select Add Service Reference
// 7. Enter service endpoint URL into the Address field
// For this example, we are using the dc container and the url is http://ogdi.cloudapp.net/v1/dc
//
// The Ogdi.DataServiceContexts sample makes it
// very easy for .NET developers to query
// for data using LINQ. This sample is uses code
// generation and will enable you to automatically
// generate updated classes as new data is published
// into OGDI.
//以下程式碼僅做為開發時之參考範例,開發人員請依照實際程式應用需求修改相關程式碼及參數。謝謝!
//此範例需要 .NET Framework 3.5 SP1以上版本。
//為了得到這個樣本工作,執行/ /以下步驟://
// 1.在Visual Studio中建立一個新的網站
// 2.新增一個 Web Form(請依需求自行命名)
// 3.確保“將代碼放在單獨的文件中”未選中在//添加新項對話框 // 4。更換的全部內容的。aspx文件與整個//文本此示例/ / 5。右鍵單擊項目節點在解決方案資源管理器// 6。選擇添加服務引用/ /7。輸入服務的端點 URL到地址欄/ /在這個例子中,我們使用的是直流容器和URL是http://ogdi.cloudapp.net/v1/dc/ // /的Ogdi.DataServiceContexts樣本使得/ /很容易的。NET開發人員查詢 //數據使用LINQ。這個範例是使用代碼//產生,將使您自動/ /生成新的數據更新的類發布//到OGDI。
/ / 此示例要求。NET 框架 3.5 SP1。/ / 要得到這個工作的示例,請執行 / / 以下步驟: / / / / 1。在 Visual Studio 2008 SP1 中創建新的 Web 網站 / / 2。添加新的 Web 表單 (命名它,你喜歡什麼) / / 3。請確保在未選中"將代碼放在單獨的檔中"則 / / 添加新項對話方塊 / / 4。.Aspx 檔的整個內容替換整個 / / 此示例文本 / / 5。按右鍵解決方案資源管理器中的專案節點 / / 6。請選擇添加服務引用 / / 7。在位址欄輸入服務端點 URL / / 為此示例中,我們使用的直流容器和 url 是 http://ogdi.cloudapp.net/v1/dc / / / / Ogdi.DataServiceContexts 示例使它 / / 很容易。網商查詢 / / 使用 LINQ 的資料。此示例是使用代碼 / / 一代將使您能夠自動 / / 生成更新後的類,如發佈新的資料 / / 到 OGDI。
protected void Page_Load(object sender, EventArgs e)
{
var context = new Ogdi.dcDataService(new Uri("http://taipeicityopendata.cloudapp.net/v1/TaipeiOGDI/EPA01"));
//var context = new Ogdi.DataServiceContexts.dc.dcDataServiceContext();
// NOTE: You can replace BankLocations with any EntitySet available
// See http://ogdisdk.cloudapp.net/DataCatalog.aspx
// for a full list.
var query = from locations in context.BankLocations
select locations;
// The query above translates into the following URL:
// http://ogdi.cloudapp.net/v1/dc/BankLocations()
// Execute query
List<Ogdi.BankLocationsItem> results = query.ToList();
// Bind the reults to the GridView.
GridView1.DataSource = results;
GridView1.DataBind();
// Execute the same query, but return KML
var wc = new System.Net.WebClient();
var queryUrl = query.ToString() + "?format=kml";
TextBox1.Text = wc.DownloadString(new Uri(queryUrl));
// Currently, the OGDI service only supports the $filter
// and $top WCF Data Services querystring paramaters.
// Therefore, the following queries are valid:
query = (from locations in context.BankLocations
select locations).Take(5);
// The query above translates into the following URL:
// http://ogdi.cloudapp.net/v1/dc/BankLocations()?$top=5
query = from locations in context.BankLocations
where locations.name == "Adams Bank"
select locations;
// The query above translates into the following URL:
// http://ogdi.cloudapp.net/v1/dc/BankLocations()?$filter=name eq 'Adams Bank'
query = (from locations in context.BankLocations
where locations.name == "Adams Bank"
select locations).Take(5);
// The query above translates into the following URL:
// http://ogdi.cloudapp.net/v1/dc/BankLocations()?$filter=name eq 'Adams Bank'&$top=5
// Although $filter is supported, not all $filter options
// are available. For example, substringof() is not supported.
query = from locations in context.BankLocations
where locations.name.Contains("Adam")
select locations;
// The query above translates into the following URL:
// http://ogdi.cloudapp.net/v1/dc/BankLocations()?$filter=substringof('Adam',name)
// Any LINQ queries that translate into usage of unsupported
// WCF Data Services querystring parameters will
// result in a DataServiceQueryException.
// As another example, the query below will result in the usage of $skip.
// Therefore a DataServiceQueryException will be thrown.
query = (from locations in context.BankLocations
where locations.name == "Adams Bank"
select locations).Skip(5).Take(5);
// The query above translates into the following URL:
// {http://ogdi.cloudapp.net/v1/dc/BankLocations()?$filter=name eq 'Adams Bank'&$skip=5&$top=5
// NOTE: To implement paging, see "C#/.NET Paging" sample
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Width="100%" Height="200px"></asp:TextBox>
</div>
</form>
</body>
</html>