我正在尝试使用Google Charts API来显示数据库中的数据。我正在使用以下教程:https://www.c-sharpcorner.com/article/asp-net-mvc5-google-charts-api-integration/
我已经在控制器中注意到,它们使用txt文件加载数据。string srcFilePath = "Content/files/SalesOrderDetail.txt";
是否有一种使用实体框架加载数据的方法,我该怎么做?
我是mvc的新手,并且使用api,因此我不确定如何实现此目标。
我首先在项目中使用了数据库。
控制器:
using HolidayTracker.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
namespace Graphs.Controllers
{
public class MetricsController : Controller
{
#region Index method
/// <summary>
/// GET: Home/Index method.
/// </summary>
/// <returns>Returns - index view page</returns>
public ActionResult Index()
{
// Info.
return this.View();
}
#endregion
#region Get data method.
/// <summary>
/// GET: /Home/GetData
/// </summary>
/// <returns>Return data</returns>
public ActionResult GetData()
{
// Initialization.
JsonResult result = new JsonResult();
try
{
// Loading.
List<Employee> data = this.LoadData();
// Setting.
var graphData = data.GroupBy(p => new
{
p.FullName,
p.HoursTaken,
p.SickLeaveTaken
})
.Select(g => new
{
g.Key.FullName,
g.Key.HoursTaken,
g.Key.SickLeaveTaken
}).OrderByDescending(q => q.FullName).ToList();
// Top 10
graphData = graphData.Take(10).Select(p => p).ToList();
// Loading drop down lists.
result = this.Json(graphData, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
// Info
Console.Write(ex);
}
// Return info.
return result;
}
#endregion
#region Helpers
#region Load Data
/// <summary>
/// Load data method.
/// </summary>
/// <returns>Returns - Data</returns>
private List<Employee> LoadData()
{
// Initialization.
List<Employee> lst = new List<Employee>();
try
{
// Initialization.
string line = string.Empty;
string srcFilePath = "Content/files/SalesOrderDetail.txt";
var rootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
var fullPath = Path.Combine(rootPath, srcFilePath);
string filePath = new Uri(fullPath).LocalPath;
StreamReader sr = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read));
// Read file.
while ((line = sr.ReadLine()) != null)
{
// Initialization.
Employee infoObj = new Employee();
string[] info = line.Split(',');
// Setting.
infoObj.FullName = info[3].ToString();
infoObj.HoursTaken = Convert.ToDecimal(info[0].ToString());
infoObj.SickLeaveTaken = Convert.ToDecimal(info[0].ToString());
// Adding.
lst.Add(infoObj);
}
// Closing.
sr.Dispose();
sr.Close();
}
catch (Exception ex)
{
// info.
Console.Write(ex);
}
// info.
return lst;
}
#endregion
#endregion
}
}
答案 0 :(得分:0)
我只是为此创建了数据库。
首先,您需要创建一个与SalesOrderDetail类进行映射的表
CREATE TABLE [dbo].[SalesOrderDetail](
[Sr] [int] PRIMARY KEY NOT NULL,
[OrderTrackNumber] [nvarchar](50) NULL,
[Quantity] [int] NULL,
[ProductName] [nvarchar](50) NULL,
[SpecialOffer] [nvarchar](50) NULL,
[UnitPrice] [decimal](18, 0) NULL,
[UnitPriceDiscount] [decimal](18, 0) NULL,
)
第二,您创建一个ADO.NET实体数据模型。并添加代码以将数据从文件保存到数据库。更改“加载数据”方法中的代码
private List<SalesOrderDetail> LoadData()
{
// Initialization.
List<SalesOrderDetail> lst = new List<SalesOrderDetail>();
CustomerEntities ctx = new CustomerEntities();
//lst = ctx.SalesOrderDetails.ToList();
try
{
// Initialization.
string line = string.Empty;
string srcFilePath = "Content/files/SalesOrderDetail.txt";
var rootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
var fullPath = Path.Combine(rootPath, srcFilePath);
string filePath = new Uri(fullPath).LocalPath;
StreamReader sr = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read));
// Read file.
while ((line = sr.ReadLine()) != null)
{
// Initialization.
SalesOrderDetail infoObj = new SalesOrderDetail();
string[] info = line.Split(',');
// Setting.
infoObj.Sr = Convert.ToInt32(info[0].ToString());
infoObj.OrderTrackNumber = info[1].ToString();
infoObj.Quantity = Convert.ToInt32(info[2].ToString());
infoObj.ProductName = info[3].ToString();
infoObj.SpecialOffer = info[4].ToString();
infoObj.UnitPrice = Convert.ToDecimal(info[5].ToString());
infoObj.UnitPriceDiscount = Convert.ToDecimal(info[6].ToString());
// Adding.
lst.Add(infoObj);
ctx.SalesOrderDetails.Add(infoObj);
}
ctx.SaveChanges();
// Closing.
sr.Dispose();
sr.Close();
}
catch (Exception ex)
{
// info.
Console.Write(ex);
}
// info.
return lst;
}
第一次运行后,将文本文件中的数据插入到DB中,然后注释掉代码以从文件中获取数据,并插入代码行lst = ctx.SalesOrderDetails.ToList();
以从DB中读取。
private List<SalesOrderDetail> LoadData()
{
// Initialization.
List<SalesOrderDetail> lst = new List<SalesOrderDetail>();
CustomerEntities ctx = new CustomerEntities();
lst = ctx.SalesOrderDetails.ToList();
try
{
// Initialization.
//string line = string.Empty;
//string srcFilePath = "Content/files/SalesOrderDetail.txt";
//var rootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
//var fullPath = Path.Combine(rootPath, srcFilePath);
//string filePath = new Uri(fullPath).LocalPath;
//StreamReader sr = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read));
//// Read file.
//while ((line = sr.ReadLine()) != null)
//{
// // Initialization.
// SalesOrderDetail infoObj = new SalesOrderDetail();
// string[] info = line.Split(',');
// // Setting.
// infoObj.Sr = Convert.ToInt32(info[0].ToString());
// infoObj.OrderTrackNumber = info[1].ToString();
// infoObj.Quantity = Convert.ToInt32(info[2].ToString());
// infoObj.ProductName = info[3].ToString();
// infoObj.SpecialOffer = info[4].ToString();
// infoObj.UnitPrice = Convert.ToDecimal(info[5].ToString());
// infoObj.UnitPriceDiscount = Convert.ToDecimal(info[6].ToString());
// // Adding.
// lst.Add(infoObj);
// ctx.SalesOrderDetails.Add(infoObj);
//}
//ctx.SaveChanges();
// Closing.
//sr.Dispose();
//sr.Close();
}
catch (Exception ex)
{
// info.
Console.Write(ex);
}
// info.
return lst;
}
您可以在以下位置下载源代码 https://github.com/viethien/MVC5GoogleGraph