[英]Get data from database between two dates using Google Charts asp.net
I want to get data from database using two dates and get displayed in a chart. 我想使用两个日期从数据库中获取数据并显示在图表中。
I have used the link for the chart: https://www.aspsnippets.com/Articles/Google-Charts-in-ASPNet-MVC-Google-Pie-Doughnut-Chart-example-with-database-in-ASPNet-MVC.aspx 我已使用图表的链接: https : //www.aspsnippets.com/Articles/Google-Charts-in-ASPNet-MVC-Google-Pie-Doughnut-Chart-example-with-database-in-ASPNet-MVC .aspx
I have used the following video for the dates and getting data from the database: https://youtu.be/Rm4uiny5Ano 我将以下视频用作日期并从数据库中获取数据: https : //youtu.be/Rm4uiny5Ano
**CONTROLLER:**
public ActionResult Index()
{
mymodel db = new mymodel();
db.slips = AjaxMethod();
return View(db);
}
[HttpPost]
public JsonResult AjaxMethod(DateTime? start, DateTime? end)
{
string query = "SELECT [status], sum(total_amount) as Payment";
query += " FROM slips WHERE convert(varchar,date_paid, 101) BETWEEN '" + start + "' AND '" + end + "' and status='Paid' and inv_type='Valid' GROUP BY [status]";
string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"status", "Payment"
});
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
chartData.Add(new object[]
{
sdr["status"], sdr["Payment"]
});
}
}
con.Close();
}
}
return Json(chartData);
}
private DbSet<slip> AjaxMethod()
{
throw new NotImplementedException();
}
It should display data from a database using a chart. 它应该使用图表显示数据库中的数据。 But then there's an error that it throws on the "AjaxMethod()" method.
但是,这会在“ AjaxMethod()”方法上引发一个错误。 I don't know if my code on the "Index" it's correct.
我不知道我在“索引”上的代码是否正确。
There is much more to this but just for your question and comment, you can create a Payment
class and then a list of those and return that data. 除了要解决您的问题和意见,您还可以创建一个
Payment
类,然后创建一个清单并返回该数据。 This should all be in the proper files (per class) to do it right. 这应该全部放在正确的文件中(每个类)以正确进行。
How you use this list of payments
I will leave up to you but you can start with this perhaps. 您如何使用此
payments
清单,我将取决于您,但也许您可以从这里开始。 Your model for example might include the text for the column headers. 例如,您的模型可能包含列标题的文本。
using PaymentRepository;
public ActionResult Index()
{
PaymentModel db = new PaymentModel();
// the model might also do this directly
var chartData = PaymentRepository.GetPaidPaymentList();
db.slips = chartData;
return View(db);
}
[HttpPost]
public JsonResult GetPaymentList(DateTime start, DateTime end)
{
var chartData = PaymentRepository.GetPaymentByStatusList(start,end);
return Json(chartData);
}
// put in a class with using Payment
public class PaymentRepository
{
public static List<Payment> GetPaidPaymentList()
{
DateTime startDate = DateTime.MinValue;
DateTime endDate = DateTime.Now;
string status = "Paid";
return GetPaymentByStatusList(startDate, endDate, status = );
}
public static List<Payment> GetPaymentByStatusList(DateTime startDate, DateTime endDate, string status = "Paid")
{
List<Payment> payments = new List<Payment>();
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Constring"].ConnectionString))
{
var sqlQuery = @"
SELECT [status], SUM(total_amount) as Payment
FROM slips
WHERE WHERE date_paid >= @startDate AND date_paid < @endDate
AND status = @status'
AND inv_type = 'Valid'
GROUP BY [status];
";
connection.Open();
using (SqlCommand cmd = new SqlCommand(sqlQuery, connection))
{
cmd.Parameters.Add("@startDate", System.Data.SqlDbType.DateTime);
cmd.Parameters["@startDate"].Value = startDate;
cmd.Parameters.Add("@endDate", System.Data.SqlDbType.DateTime);
cmd.Parameters["@endDate"].Value = endDate;
cmd.Parameters.Add("@status", System.Data.SqlDbType.VarChar);
cmd.Parameters["@status"].Value = status;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var payment = new Payment()
{status = (string)reader["status"], payment = (decimal)reader["Payment"]};
payments.Add(payment);
}
}
}
connection.Close();
}
return payments;
}
}
public class Payment
{
public string status
{
get;
set;
}
public decimal payment
{
get;
set;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.