我对 C# 非常陌生,并且在 ASP.NET 中工作,我应该从我在 Visual Studio 中连接的本地数据库中的表中获取所有信息。我真正知道的是如何连接以及如何在网格视图中获取整个表格,但我会在网格视图的末尾,或者以其他方式显示每列的中间值。
这是基于单选按钮(值为 1 到 5)构建的调查的答案。所以我想从每个问题中获取媒介。在我的数据库中,我只有一列 id 和一列每个问题。
现在我有这个:
result.aspx 中的代码:
<asp:GridView ID="grdResult" CssClass="grid" runat="server">
</asp:GridView>
result.aspx.cs 中的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Uppgift_3
{
public partial class result : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (resultatEntities datakoppling = new resultatEntities())
{
var answers = from survey in datakoppling.surveyAnswers
select survey;
grdResult.DataSource = answers;
grdResult.DataBind();
}
}
}
}
当我将东西放入 default.aspx 中的数据库时:
protected void saveSurvey_Click(object sender, EventArgs e)
{
using (resultatEntities datakoppling = new resultatEntities())
{
var saveSurvey = new surveyAnswers();
saveSurvey.Question1 = RadioButtonQuestion1.SelectedItem.ToString();
saveSurvey.Question2 = RadioButtonQuestion2.SelectedItem.ToString();
saveSurvey.Question3 = RadioButtonQuestion3.SelectedItem.ToString();
saveSurvey.Question4 = RadioButtonQuestion4.SelectedItem.ToString();
saveSurvey.Question5 = RadioButtonQuestion5.SelectedItem.ToString();
saveSurvey.Question6 = RadioButtonQuestion6.SelectedItem.ToString();
saveSurvey.Question7 = RadioButtonQuestion7.SelectedItem.ToString();
saveSurvey.Question8 = RadioButtonQuestion8.SelectedItem.ToString();
saveSurvey.Question9 = RadioButtonQuestion9.SelectedItem.ToString();
saveSurvey.Question10 = RadioButtonQuestion10.SelectedItem.ToString();
datakoppling.surveyAnswers.AddObject(saveSurvey);
datakoppling.SaveChanges();
}
}
}
}