3
我正在開發一個MVC3項目,我們正在開發一個站點供多家公司使用。每家公司都有自己的數據庫目錄。站點登錄信息全部存儲在單個「主」數據庫中,該數據庫包含用於每個用戶的目錄名稱。但是,這些目錄與其他結構明顯不同。我想要做的是設置標準模型,但根據用戶的目錄將數據綁定到這些模型。沒有Web.Config連接字符串的MVC3模型
public class UserSearchEntityLayer
{
public class SearchOptionsList
{
public virtual string SearchOptionText { get; set; }
public virtual string SearchOptionValue { get; set; }
}
}
public class UserSearchDBLayer : UserSearchEntityLayer
{
DbSet<SearchOptionsList> SearchOptions { get; set; }
public UserSearchDBLayer(string ClientCode)
{
//Connection Strings
var ClientConn = "Data Source=HelloWorld;Initial Catalog=" + ClientCode + ";Integrated Security=True;Persist Security Info=True";
//Prep Work
DataSet SearchOptionsDS = new DataSet();
SqlConnection cn = null;
SqlDataAdapter cmd = null;
SqlDataReader dr = null;
string SQLSelect = string.Empty;
//Start Work
try
{
cn = new SqlConnection(ClientConn);
cn.Open();
switch (ClientCode)
{
case "AAG":
//SearchOptions
SQLSelect = "SELECT [Report_Level] as 'Value',[Report_Level_Name] as 'Text' FROM [MASTER_REPORTING_LEVELS] Order By 'Value' DESC";
cmd = new SqlDataAdapter(SQLSelect, cn);
cmd.Fill(SearchOptionsDS);
if (SearchOptionsDS.Tables.Count != 0)
{
if (SearchOptionsDS.Tables[0].Rows.Count > 0)
{
foreach (DataRow R in SearchOptionsDS.Tables[0].Rows)
{
SearchOptions.Add(new SearchOptionsList { SearchOptionText = R["Text"].ToString(), SearchOptionValue = R["Value"].ToString() });
}
}
}
SQLSelect = string.Empty;
SearchOptionsDS.Dispose();
cmd.Dispose();
break;
default:
//Do more stuff here
break;
}
}
catch
{
}
finally
{
SearchOptions.Add(new SearchOptionsList { SearchOptionText = "States", SearchOptionValue = "States" });
SearchOptions.Add(new SearchOptionsList { SearchOptionText = "Locations", SearchOptionValue = "Locations" });
SearchOptions.Add(new SearchOptionsList { SearchOptionText = "Levels", SearchOptionValue = "Levels" });
SearchOptions.Add(new SearchOptionsList { SearchOptionText = "Name", SearchOptionValue = "Name" });
if ((dr != null))
{
if (!dr.IsClosed)
dr.Close();
dr = null;
}
if (cn != null)
{
if (cn.State != System.Data.ConnectionState.Closed)
cn.Close();
cn.Dispose();
cn = null;
}
if (cmd != null)
{
cmd.Dispose();
cmd = null;
}
if (SQLSelect != null)
SQLSelect = null;
}
}
}
要做到這一點,最好的方法是什麼?哦,現在,這是我折騰的對象錯誤,因爲SearchOptions爲null,因爲沒有什麼是在它的我加太..
我看不到任何地方,如果你正在創建一個新的實例的SearchOptions反對使用?添加塊在你準備部分創建SearchOptions的新實例,它不會是空的。 – 2012-01-12 10:29:34
你能用反射嗎? – 2012-01-12 10:50:36