【问题标题】:MVC3 Models without Web.Config Connection Strings没有 Web.Config 连接字符串的 MVC3 模型
【发布时间】:2012-01-11 16:24:08
【问题描述】:

我正在从事一个 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 为空,因为其中没有任何内容可供我添加..

【问题讨论】:

  • 如果您正在创建要使用的 SearchOptions 对象的新实例,我看不到任何地方?在您的准备部分周围添加一个块以创建一个新的 SearchOptions 实例,它不会为空。

标签: asp.net-mvc-3 data-binding model connection-string


【解决方案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>

如果您有更好的解决方案,在这种情况下,我会全神贯注......或眼睛。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多