2011-07-21 17 views
0

我有一個場景,其中有多個dbs具有相同的架構,客戶端可以選擇要查詢的數據庫。有沒有辦法在從silverlight執行oData查詢時包含數據庫名稱,以便它可以重用相同的服務?使用oData和EF設置數據庫名稱

假設我有在客戶端(silverlight/wp7)執行的這個查詢(見下文),我怎樣才能讓這個查詢運行在用戶首次啓動應用程序時選擇的數據庫上?

private DataServiceCollection _employees;
私人無效LoadEmployees()
{
DataModelContainer DMC =新DataModelContainer(新的URI( 「HTTP://本地主機:63832/DataService.svc」));
var query =(from d in dmc.Employees
where e.StartDate == BaseDate select e);
_employees = new DataServiceCollection(dmc);
_employees.LoadCompleted + = new EventHandler(_employees_LoadCompleted);
_employees.LoadAsync(query);
}

回答

0

這裏是我想出了:該AddQueryOption將添加值到查詢字符串,在我的情況下,我想運行查詢的數據庫的關鍵。 var dquc =(從dmc.Employees.AddQueryOption(「dbKey」,SelectedDB中的e)
其中e.StartDate == BaseDate select e);
_employees = new DataServiceCollection(dmc);
_employees.LoadCompleted + = new EventHandler(_employees_LoadCompleted);
_employees.LoadAsync(query);
}

在DataService類我overrride所述的createDataSource方法和用於該查詢
保護覆蓋DataModelContainer的createDataSource()正確的連接字符串返回DataModelContainer {
DataModelContainer DMC =新DataModelContainer(GetConnectionString()) ;
return dmc;
}
私人字符串GetConnectionString()
{
串DBKEY = HttpContext.Current.Request.Params [ 「DBKEY」]。的ToString();
string cnnKey =「DataModelContainer」+ dbKey;
return ConfigurationManager.ConnectionStrings [cnnKey] .ToString();
}

0

你應該在你的配置文件中通過connectionstrings元素來做到這一點。

如:

<configuration> 
<!-- Other configuration settings --> 

<connectionStrings> 

    <add name="Sales" 
     providerName="System.Data.SqlClient" 
     connectionString= "server=myserver;database=Products;uid=<user name>;pwd=<secure password>" /> 

    <add name="NorthWind" 
     providerName="System.Data.SqlClient" 
     connectionString="server=.;database=NorthWind;Integrated Security=SSPI" /> 

</connectionStrings> 

要檢索的連接字符串動態的基礎上,從客戶端的查詢字符串,利用ConfigurationManager中類。下面的鏈接將幫助您在這方面:

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

www.dotnet-guide.com/configurationmanager-class.html

+0

但是,當查詢來自客戶端時,我該如何動態地選擇正在使用的連接? – Tyler

相關問題