.NET MVC同页面显示从不同数据库(mssql、mysql)的数据
控制器:
private readonly VipViewModel _model = new VipViewModel(); public static string Msg;// GET: SystemManage/RenZheng [HttpGet] public ActionResult Index(int page = 1) { #region 数据填充到自定义Model var db = Congfig.MySqlConn; const string sql = "SELECT S,W FROM SW"; var result = db.ExecuteDataReader(sql); MySqlConnection con = new MySqlConnection("server=012.345.678.910;port=0123;User Id=UserID;password=Pwd;database=DB"); MySqlDataAdapter da = new MySqlDataAdapter(sql, con); DataSet ds = new DataSet(); da.Fill(ds); DataTable dt = ds.Tables[0];//获取另一数据库(MYSQL)的数据 //从数据库中读取 var categoryList = new List<Comm2ViewModel>(); for (int i = 0; i < dt.Rows.Count; i++) { categoryList.Add(new Comm2ViewModel() { Id = dt.Rows[i][0].ToString(), Name = dt.Rows[i][1].ToString() }); } _model.Comm2List = categoryList;//填充 #endregion var pageSize = Request["pagesize"] == null ? 10 : int.Parse(Request["pagesize"]); OQLCompare Fun(OQLCompare cmp, Users u) { cmp = cmp.Comparer(u.IsDelete, "=", false); cmp = cmp.Comparer(u.Status, "=", 1);//申请认证的用户 return cmp; } var total = Curd.GetRecordCounts((OQLCompareFunc<Users>)Fun); _model.UsersList = OQL.FromObject<Users>().Select().Where(Fun).OrderBy((o, b) => o.Desc(b.Id)).Limit(pageSize, page, total).ToList(); ViewBag.Paging = Paging.PageNavigate(pageSize, page, total, ""); ViewBag.Msg = Msg; Msg = ""; ViewBag.DeleteUrl = "地址"; return View(_model); }
View视图:
@model ../.Models.VipViewModel <!--这里是引用的自定义Model,地址根据实际来改-->
<table class="table"> <thead> <tr> <th>Id</th> <th>公司名称</th> <th>职位</th> <th>认证姓名</th> <th>联系电话</th> <th>社区</th> <th>房间号</th> <th>增加日期</th> <th>操作</th> </tr> </thead> <tbody> @if (Model.UsersList.Count > 0) { foreach (var item in Model.UsersList) { <tr> <td> <p>@item.Id</p> </td> <td> <p>@item.CompanyName</p> </td> <td> <p>@item.Type</p> </td> <td> <p>@item.Name</p> </td> <td> <p>@item.Phone</p> </td> <td> @{ foreach (var item2 in Model.Comm2List) { if (item2.Id == item.Community2) { <p>@item2.Name</p> break; } } } </td> <td> <p>@item.Community</p> </td> <td> <p>@item.CreateTime.ToString("F")</p> </td> <td> <div class="operate"> <a href="" class="gray">通过认证</a> <a href="javascript:;" class="red" onclick="">拒绝认证</a> </div> </td> </tr> } } </tbody> </table>
VipViewModel: public class VipViewModel { public List<Users> UsersList { get; set; } public List<Comm2ViewModel> Comm2List { get; set; } } Comm2ViewModel: public class Comm2ViewModel { public string Id { get; set; } public string Name { get; set; } }
最后附上结构: