簡體   English   中英

使用Linq以C#Windows形式將數據從數據庫加載到RichTextBox

[英]Load data from database to richtextbox in c# windows form using Linq

在此處輸入圖片說明

我正在嘗試使用Linq在我的C#Windows Form項目中將數據從數據庫加載到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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM