2012-01-11 6 views
3

私は複数の企業で使用するために1つのサイトを開発しているMVC3プロジェクトに取り組んでいます。各社には独自のデータベースカタログがあります。サイトのログイン情報はすべて1つの「マスター」データベースに格納され、そのデータベースには各ユーザーに使用するカタログ名が含まれています。しかし、これらのカタログは、それぞれの構造とは少し異なる。私がしようとしているのは、標準モデルをセットアップすることですが、ユーザーのカタログに基づいてこれらのモデルにデータを別々にバインドします。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; 
     } 
    } 
} 

これを行う最善の方法は何ですか?ああ、今、これは私にObjectOrrorを投げています。SearchOptionsは何もないので、私のために追加されていません。

+0

使用するSearchOptionsオブジェクトの新しいインスタンスを作成する場合は、どこにも表示されません。あなたのprepセクションの周りにチャンクを追加して、SearchOptionsの新しいインスタンスを作成し、それはnullにはなりません。 –

+0

反射を使用できますか? –

答えて

1

最終的にはうまくいきました....ここに私の解決策がありますそれは動作します)。

public class UserSearchDBLayer : UserSearchEntityLayer 
{ 
    public IEnumerable<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 
     var DataBuilderList = new List<SearchOptionsList>(); 
     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); 
        DataBuilderList.Add(new SearchOptionsList { SearchOptionText = "Region", SearchOptionValue = "0" }); 
        if (SearchOptionsDS.Tables.Count != 0) 
        { 
         if (SearchOptionsDS.Tables[0].Rows.Count > 0) 
         { 
          foreach (DataRow R in SearchOptionsDS.Tables[0].Rows) 
          { 
           DataBuilderList.Add(new SearchOptionsList { SearchOptionText = R["Text"].ToString(), SearchOptionValue = R["Value"].ToString() }); 
          } 
         } 
        } 
        DataBuilderList.Add(new SearchOptionsList { SearchOptionText = "States", SearchOptionValue = "States" }); 
        DataBuilderList.Add(new SearchOptionsList { SearchOptionText = "Locations", SearchOptionValue = "Locations" }); 
        DataBuilderList.Add(new SearchOptionsList { SearchOptionText = "Levels", SearchOptionValue = "Levels" }); 
        DataBuilderList.Add(new SearchOptionsList { SearchOptionText = "Name", SearchOptionValue = "Name" }); 
        SQLSelect = string.Empty; 
        SearchOptionsDS.Dispose(); 
        cmd.Dispose(); 
        break; 
       default: 
        //Cool Stuff 
        break; 
      } 
     } 
     catch 
     { 
     } 
     finally 
     { 
      SearchOptions = DataBuilderList; 
      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; 
     } 
    } 
} 

次に、あなたのコントローラー:

public class TestController : Controller 
{ 
    public UserSearchDBLayer model = new UserSearchDBLayer("AAG"); 
    // 
    // GET: /Test/ 

    public ActionResult Index() 
    { 

     return View(model); 
    } 

} 

は最後に見る:

@model PlayGround.Models.UserSearchDBLayer 

@{ 
Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
<title>Index</title> 
</head> 
<body> 

@Html.ListBox("Test", new SelectList(Model.SearchOptions, "SearchOptionValue", "SearchOptionText"), new { size = "25" }) 

</body> 
</html> 

あなたがよりよい解決策を持っている場合、私はこのケースではすべての耳...や目をしています。

関連する問題