2012-01-11 2 views
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; 
     } 
    } 
} 

이렇게하는 가장 좋은 방법은 무엇입니까? 아 그리고 지금은 내게 너무 ObjectOrror가 있기 때문에 아무 것도 나를 추가 할 수 없기 때문에 ObjectOrror를 던지고있다 ..

+0

사용할 SearchOptions 개체의 새 인스턴스를 만드는 경우 아무데도 볼 수 없습니까? 준비 섹션 주위에 청크를 추가하여 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> 

더 나은 해결책을 가지고 있다면,이 경우 모든 귀 ... 또는 눈입니다.