使用 Linq 以 c# windows 形式将数据从数据库加载到 richtextbox
Load data from database to richtextbox in c# windows form using Linq
我正在尝试使用 Linq 在我的 c# windows 表单项目中将数据从数据库加载到 RichTextBox(如图所示)。
我不知道我做的对不对,因为数据没有加载到 RichTextBox。请帮忙。
我正在尝试这样做:
string idNr = txtIdcardNr.Text.Trim();
var CheckIfIdCardExist = (from u in db.Customer
where u.IdentityCardNr == idNr
select u).FirstOrDefault();
if(CheckIfIdCardExist != null)
{
String template =
@"Date\t\t{0}
Notes\t\t{1}
Staff\t\t{2}
*********\t\t{3}";
var notes = (from u in db.CustomerNotes
join em in db.Employee on u.StaffId equals em.EmployeeId
where u.CustomerId == CheckIfIdCardExist.CustomerId
select new {
Date = u.NoteDate,
notes = u.Notes,
employee = em.FirstName + " " + em.LastName
}).ToList();
foreach(var n in notes)
{
richTextBox1.Text = string.Format(template, n.Date, n.notes, n.employee);
}
我在这里迈出了一大步,我猜主要问题是您没有看到所有笔记,只看到最后一个。
var notes = (from u in db.CustomerNotes
join em in db.Employee on u.StaffId equals em.EmployeeId
where u.CustomerId == CheckIfIdCardExist.CustomerId
select new {
Date = u.NoteDate,
notes = u.Notes,
employee = em.FirstName + " " + em.LastName
});
StringBuilder sb = new StringBuilder();
foreach(var n in notes)
{
sb.AppendFormat(template, n.Date, n.notes, n.employee);
sb.Append("\n");
}
richTextBox1.Text = sb.ToString();
我正在尝试使用 Linq 在我的 c# windows 表单项目中将数据从数据库加载到 RichTextBox(如图所示)。
我不知道我做的对不对,因为数据没有加载到 RichTextBox。请帮忙。
我正在尝试这样做:
string idNr = txtIdcardNr.Text.Trim();
var CheckIfIdCardExist = (from u in db.Customer
where u.IdentityCardNr == idNr
select u).FirstOrDefault();
if(CheckIfIdCardExist != null)
{
String template =
@"Date\t\t{0}
Notes\t\t{1}
Staff\t\t{2}
*********\t\t{3}";
var notes = (from u in db.CustomerNotes
join em in db.Employee on u.StaffId equals em.EmployeeId
where u.CustomerId == CheckIfIdCardExist.CustomerId
select new {
Date = u.NoteDate,
notes = u.Notes,
employee = em.FirstName + " " + em.LastName
}).ToList();
foreach(var n in notes)
{
richTextBox1.Text = string.Format(template, n.Date, n.notes, n.employee);
}
我在这里迈出了一大步,我猜主要问题是您没有看到所有笔记,只看到最后一个。
var notes = (from u in db.CustomerNotes
join em in db.Employee on u.StaffId equals em.EmployeeId
where u.CustomerId == CheckIfIdCardExist.CustomerId
select new {
Date = u.NoteDate,
notes = u.Notes,
employee = em.FirstName + " " + em.LastName
});
StringBuilder sb = new StringBuilder();
foreach(var n in notes)
{
sb.AppendFormat(template, n.Date, n.notes, n.employee);
sb.Append("\n");
}
richTextBox1.Text = sb.ToString();