从数据库中获取数据

时间:2017-01-31 04:14:54

标签: c# sql

我在一个名为'shop'的数据库中创建了一个名为'Gift_Info'的表。在表中,有3列(ID,UserEmail,BDayDate)。我在/ Views / Home中创建了Events.cshtml:

var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
  var db = Database.Open ("shop");
  var sa = WebSecurity.CurrentUserName;
  var sql = "SELECT BDayDate, ID FROM Gift_Info WHERE UserEmail = @0";
  var result = db.Query(sql, sa);
  var data = result.Select(x => new
  {
      id = x.Giftee,
      title = x.Giftee,
      allDay = true
  }).ToArray();

  Json.Write(data, Response.Output);
  Response.ContentType = "application/json";

2 个答案:

答案 0 :(得分:0)

这会奏效。您将转到数据库,而不是手动创建对象。这将调用控制器操作或方法。如果你想调用asmx,但一定要打开脚本属性:

控制器方法或您的方法:

public class HomeController : Controller
{
    public class eventsClass {
        public IList<YouWillGetThisFromDB> events = new List<YouWillGetThisFromDB>();
    }

    public class YouWillGetThisFromDB
    {
        public string id { get; set; }
        public string title {get; set;}
        public string start {get; set;}
        public bool allDay {get; set;}
    }

    public JsonResult GetCalendarEvents()
    {
        YouWillGetThisFromDB you = new YouWillGetThisFromDB
        {
            id = "theId",
            title = "theTitle",
            start = "1/5/2017",
            allDay = true
        };
        eventsClass ec = new eventsClass();
        ec.events.Add(you);

        return Json(ec.events, JsonRequestBehavior.AllowGet); 
    }

    public ActionResult Index15()
    {
        return View();
    }

观点:

<!DOCTYPE html>
<html>
<head title="apdh">
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.1.0/fullcalendar.min.js"></script>

    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.1.0/fullcalendar.min.css">
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.1.0/fullcalendar.print.css ">
    <script>
        $(document).ready(function () {
            $('#calendar').fullCalendar({
                editable: false
                , events: '/Home/GetCalendarEvents'
            });
        }); </script>
</head>
<body>
    <div id="calendar"></div>
</body>
</html>

答案 1 :(得分:0)

Here是一个完整日历工作的.NET小提琴。它显示了如何调用Controller,它提供了可用于限制从数据库返回的数据的开始和结束日期。然后它返回日历上显示的事件。